Created
September 1, 2015 10:19
-
-
Save pepoluan/16bd5c351e51862fda7c to your computer and use it in GitHub Desktop.
Byteify User-Defined Function -- convert "###.## KB/GB/MB/KiB/MiB/GiB" to values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' 1. Open Excel's VBA Editor using Alt-F11 | |
' 2. Right-Click on the "VBAProject (Spreadsheet_Name)" entry and choose "Insert > Module" | |
' 3. Paste the below code starting from the line beginning with "Function" up to and including the line "End Function" | |
' 4. Now you can use the User-Defined Function in your spreadsheet | |
' | |
' SYNTAX: | |
' =Byteify(Cell, [AssumeBinaryPrefix=False]) | |
' Cell : The Cell Reference (if actually a range, it will use the top left cell only) | |
' AssumeBinaryPrefix : KB, MB, GB will be assumed to be powers of 1024 (in other words, same as KiB, MiB, GiB) | |
' | |
Function Byteify(Cell As Range, Optional AssumeBinaryPrefix As Boolean = False) | |
Sarr = Split(Cell.Cells(1, 1).Value, " ") | |
Num = Sarr(0) | |
If UBound(Sarr) < 1 Then Suff = "B" Else Suff = UCase(Sarr(1)) | |
Pow = InStr("BKMGT", Left(Suff, 1)) - 1 | |
If Pow < 0 Then | |
Byteify = CVErr(xlErrValue) | |
Exit Function | |
End If | |
R = Mid(Suff, 2) | |
Select Case R | |
Case "IB": Base = 1024 | |
Case "B" | |
If AssumeBinaryPrefix Then Base = 1024 Else Base = 1000 | |
End Select | |
Byteify = Val(Num) * (Base ^ Pow) | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment