|
|
|
|
|
|
Excel
|
Convert any Hex Number from 1 to FFFFFFFFFFFF1(plus 243 zeros) to Decimal
|
|
Ease of Use
|
Intermediate
|
Version tested with
|
2002, 2003
|
Submitted by:
|
Jacob Hilderbrand
|
Description:
|
This User Defined Function will convert a Hex number to Decimal.
|
Discussion:
|
Excel's HEX2Dec Worksheet Function will convert Hex numbers to Decimal, but only up to 746A528800. It cannot convert larger numbers. If you need to convert a number larger than 746A528800 to Decimal, then this macro is what you need.
Refer to the KB Entry (http://www.vbaexpress.com/kb/getarticle.php?kb_id=306) to reverse this process.
|
Code:
|
instructions for use
|
Option Explicit
Public Function HexToDec(Hex As String) As Double
Dim i As Long
Dim j As Variant
Dim k As Long
Dim n As Long
Dim HexArray() As Double
n = Len(Hex)
k = -1
ReDim HexArray(1 To n)
For i = n To 1 Step -1
j = Mid(Hex, i, 1)
k = k + 1
Select Case j
Case 0 To 9
HexArray(i) = j * 16 ^ (k)
Case Is = "A"
HexArray(i) = 10 * 16 ^ (k)
Case Is = "B"
HexArray(i) = 11 * 16 ^ (k)
Case Is = "C"
HexArray(i) = 12 * 16 ^ (k)
Case Is = "D"
HexArray(i) = 13 * 16 ^ (k)
Case Is = "E"
HexArray(i) = 14 * 16 ^ (k)
Case Is = "F"
HexArray(i) = 15 * 16 ^ (k)
End Select
Next i
HexToDec = Application.WorksheetFunction.Sum(HexArray)
End Function
|
How to use:
|
- Open Excel.
- Alt + F11 to open the VBE.
- Insert | Module.
- Paste the code from above in the Code Window that opens up.
- Close the VBE (Alt + Q or press the X in the top right corner).
|
Test the code:
|
- Option 1: In a worksheet cell input = HexToDec(#).
- Option 2: In a VBA macro input MyNum = HexToDec(#).
- Just replace # with your number.
|
Sample File:
|
HexToDec.zip 7.44KB
|
Approved by mdmackillop
|
This entry has been viewed 99 times.
|
|