[>999999999999]0.00,,,,"TB";[>999999999]0.00,,,"GB";0.00,,"MB"
Starting with Excel 2007, it is possible to use the conditional formatting wizard (see below) to create a conditional formats with more than three conditions. I am writing a separate article on this.
Public Function SizeA(ByVal parmValue)
Static lngPower As Long
If Abs(parmValue) < 1000 Then
SizeA = parmValue
Else
lngPower = Int(Log(parmValue) / Log(1000))
SizeA = parmValue / (1000 ^ lngPower) & " " & Array("KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")(lngPower - 1)
End If
End Function
Public Function SizeB(ByVal parmValue)
Static lngPower As Long
Const strSuffix As String = "KB^MB^GB^TB^PB^EB^ZB^YB"
Const strDelim As String = "^"
If Abs(parmValue) < 1000 Then
SizeB = parmValue
Else
lngPower = Int(Log(parmValue) / Log(1000))
SizeB = parmValue / (1000 ^ lngPower) & " " & Split(strSuffix, strDelim)(lngPower - 1)
End If
End Function
Public Function SizeAA(ByVal parmValue, ByVal parmSuffixes)
Static lngPower As Long
If Abs(parmValue) < 1000 Then
SizeAA = parmValue
Else
lngPower = Int(Log(parmValue) / Log(1000))
SizeAA = parmValue / (1000 ^ (lngPower)) & " " & parmSuffixes(lngPower - 1)
End If
End Function
Public Function SizeBB(ByVal parmValue)
Static lngPower As Long
If Abs(parmValue) < 1000 Then
SizeBB = parmValue
Else
lngPower = Int(Log(parmValue) / Log(1000))
SizeBB = parmValue / (1000 ^ lngPower) & " " & Split("KB^MB^GB^TB^PB^EB^ZB^YB", "^")(lngPower - 1)
End If
End Function
Public Function SizeBBB(ByVal parmValue)
Static lngPower As Long
If Abs(parmValue) < 1000 Then
SizeBBB = parmValue
Else
lngPower = Int(Log(parmValue) / Log(1000))
SizeBBB = parmValue / (1000 ^ lngPower) & " " & Split(gstrSuffix, gstrDelim)(lngPower - 1)
End If
End Function
Public Function SizeB2(ByVal parmValue)
Static lngPower As Long
Static strSuffix As String
Static strDelim As String
If Len(strSuffix) = 0 Then
strSuffix = "KB^MB^GB^TB^PB^EB^ZB^YB"
strDelim = "^"
End If
If Abs(parmValue) < 1000 Then
SizeB2 = parmValue
Else
lngPower = Int(Log(parmValue) / Log(1000))
SizeB2 = parmValue / (1000 ^ lngPower) & " " & Split(strSuffix, strDelim)(lngPower - 1)
End If
End Function
MIN MAX AVERAGE ROUTINE
0.0859375 0.09375 0.08828125 SizeA
0.0546875 0.0625 0.06171875 SizeAA
0.0625 0.0703125 0.06796875 SizeB
0.0625 0.078125 0.06875 SizeB2
0.0625 0.078125 0.0703125 SizeBB
0.0625 0.078125 0.06953125 SizeBBB
Option Explicit
Const gstrSuffix As String = "KB^MB^GB^TB^PB^EB^ZB^YB"
Const gstrDelim As String = "^"
Public Sub timeit()
Dim sngStart As Single
Dim lngLoop As Long
Dim strResult As String
Const lngCount As Long = 100000
Dim vSuffixes As Variant
vSuffixes = Array("KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
sngStart = Timer
For lngLoop = 1 To lngCount
strResult = SizeA(123456 + lngLoop)
Next
Debug.Print "SizeA", Timer - sngStart, strResult
sngStart = Timer
For lngLoop = 1 To lngCount
strResult = SizeAA(123456 + lngLoop, vSuffixes)
Next
Debug.Print "SizeAA", Timer - sngStart, strResult
sngStart = Timer
For lngLoop = 1 To lngCount
strResult = SizeB(123456 + lngLoop)
Next
Debug.Print "SizeB", Timer - sngStart, strResult
sngStart = Timer
For lngLoop = 1 To lngCount
strResult = SizeBB(123456 + lngLoop)
Next
Debug.Print "SizeBB", Timer - sngStart, strResult
sngStart = Timer
For lngLoop = 1 To lngCount
strResult = SizeBBB(123456 + lngLoop)
Next
Debug.Print "SizeBBB", Timer - sngStart, strResult
sngStart = Timer
For lngLoop = 1 To lngCount
strResult = SizeB2(123456 + lngLoop)
Next
Debug.Print "SizeB2", Timer - sngStart, strResult
End Sub
Here is a bas file you can import into your VBproject environment.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)