Convert MB, GB, KB to bytes

revelation.now

New Member
Joined
Dec 11, 2009
Messages
10
Hi everyone. One of our partners don't understand how to usefully present data, and their equipment is generating data usage reports into csv/xls, however are displaying data values in a mix of GB's, MB's and KB's.

We need to sort and compare this data, and its absolutely useless in this format. We need need all values in a comparative metric, like bytes.

Is there a built in function to accomplish this, or does someone have an idea of what kind of parsing function I would need to run on this? There are multiple thousands of values that need to be converted per sheet.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Number conversions can be done. The tricky part is the sting part and if commas exist. Please post all the possible entry string values.
 
Upvote 0
Here is our 'bytes column'

This would be an easier task, except there is a decimal point in the middle of the data value, otherwise I could do a text search and replace. There is also a Host column, and that contains decimal points that can't be parsed out, so I can't even search and replace out the decimal points.



Bytes
36.96 GB
731 KB
1.74 GB
6.74 GB
1.67 GB
16.1 MB
1.22 GB
106.87 MB
895.69 MB
41.99 MB
2.76 MB
176.7 MB
310.9 MB
423.93 KB
5.84 MB
390.41 KB
494.92 MB
44.86 MB
2.65 GB
3.56 GB
46.37 MB
180.77 MB
41.75 MB
7.73 MB

<tbody>
</tbody>
 
Upvote 0
Is the data that "appears" to be on 2 lines, actually that way ??

36.96
GB
7.73
MB

If so, is it separated by ALT Enter ?
Can you please clarify ?

I'd also suggest a helper column might do the trick with the formula, depending how accurate you want it.
Generally accepted as 1000 for 1 kilobyte but is actually 1024, but you can change the formula to suit
Code:
=IF(RIGHT(A2,2)="GB",LEFT(A2,FIND(" ",A2,1))*1000000000,IF(RIGHT(A2,2)="MB",LEFT(A2,FIND(" ",A2,1))*1000000,LEFT(A2,FIND(" ",A2,1))*1000))
 
Upvote 0
In a Module, paste this. Use as call as show in the Test sub or as a UDF as commented.
Code:
Sub Test_ConvertToBytes()
  MsgBox ConvertToBytes("36.96 GB")
  MsgBox ConvertToBytes("36.96 GB", 0.01)
End Sub

'=ConvertToBytes(A1)
'=ConvertToBytes(A1, 0.01)
Function ConvertToBytes(aString As String, _
  Optional iRound As Double = 1) As Double
  
  Dim d As Double
  d = NumberPart(aString)
  If Len(CStr(d)) = Len(aString) Then
    ConvertToBytes = -1
    Exit Function
  End If
  
  Select Case True
    Case InStr(aString, "KB") > 0
      d = d * 2 ^ 10
    Case InStr(aString, "MB") > 0
      d = d * 2 ^ 20
    Case InStr(aString, "GB") > 0
      d = d * 2 ^ 30
    Case InStr(aString, "TB") > 0
      d = d * 2 ^ 40
    Case InStr(aString, "PB") > 0
      d = d * 2 ^ 50
    Case InStr(aString, "EB") > 0
      d = d * 2 ^ 60
    Case InStr(aString, "ZB") > 0
      d = d * 2 ^ 70
    Case InStr(aString, "YB") > 0
      d = d * 2 ^ 80
    Case Else
      d = -1
  End Select
  
  ConvertToBytes = WorksheetFunction.mRound(d, iRound)
End Function


Sub Test_NumberPart()
  MsgBox NumberPart("36.96 KB")
End Sub

' formula, http://office.microsoft.com/en-us/excel-help/extracting-numbers-from-alphanumeric-strings-HA001154901.aspx
' Modified by Kenneth Hobson
Function NumberPart(aString As String) As Double
  Dim s As String, i As Integer, mc As String, mc2 As String
  aString = Replace(aString, ",", "")
  For i = 1 To Len(aString)
    mc = Mid(aString, i, 1)
    mc2 = ""
    If i <> Len(aString) Then mc2 = Mid(aString, i + 1, 1)
    If Not IsNumeric(mc2) Then mc2 = ""
    If Asc(mc) >= 48 And Asc(mc) <= 57 _
      Or (mc = "-" And mc2 <> "") _
      Or (mc = "." And mc2 <> "") _
      Then s = s & mc
  Next i
  NumberPart = s
End Function
 
Upvote 0
Here is another possible option:


Excel 2012
AB
136.96 GB39685497815
2731 KB748544
31.74 GB1868310774
46.74 GB7237019894
51.67 GB1793148846
616.1 MB16882073.6
71.22 GB1309965025
8106.87 MB112061317.1
9895.69 MB939199037.4
1041.99 MB44029706.24
112.76 MB2894069.76
12176.7 MB185283379.2
13310.9 MB326002278.4
14423.93 KB434104.32
155.84 MB6123683.84
16390.41 KB399779.84
17494.92 MB518961233.9
1844.86 MB47039119.36
192.65 GB2845415834
203.56 GB3822520893
2146.37 MB48622469.12
22180.77 MB189551083.5
2341.75 MB43778048
247.73 MB8105492.48
Sheet1
Cell Formulas
RangeFormula
B1=LEFT(A1,FIND(" ",A1))*1024^LOOKUP(RIGHT(A1,2),{"GB","KB","MB"},{3,1,2})
 
Upvote 0
This is probably better than my first suggestion.

=LEFT(A1,FIND(" ",A1))*1024^MATCH(RIGHT(A1,2),{"KB","MB","GB"},0)

FormR, sir, you are a genius!

Kenneth was also on the right track. Those were all individual cells that somehow got wrapped when I posted the data, but inline beats macros.

Thanks heaps everyone!
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top