Convert KB, MB, GB, TB to GB

Sub ConvertByteTags()
' This code will navigate down column B, starting at row 2
' and replace every GB, KB and MB value with its equivalent extended value
Dim iRow As Integer
Dim strValue As String
Dim strSizeType As String
Dim dblNewValue As Double

Const ConversionFactor As Integer = 1024 ' 1024

iRow = 2
Do
strValue = Cells(iRow, 2).Value
If strValue = "" Then
Exit Do
End If

strSizeType = Right(strValue, 2)
Select Case strSizeType
Case "TB" ' terrabytes
dblNewValue = Val(strValue) * ConversionFactor
Case "GB" ' gigabytes
dblNewValue = Val(strValue) * 1
Case "MB" ' megabytes
dblNewValue = Val(strValue) / ConversionFactor
Case "KB" ' kilobytes
dblNewValue = Val(strValue) / ConversionFactor  / ConversionFactor
Case Else
dblNewValue = Val(strValue) / ConversionFactor / ConversionFactor  / ConversionFactor
End Select
Cells(iRow, 3).Value = dblNewValue
iRow = iRow + 1
Loop
MsgBox "Cells converted into GB", vbOKOnly Or vbInformation
End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *