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:

  1. Open Excel.
  2. Alt + F11 to open the VBE.
  3. Insert | Module.
  4. Paste the code from above in the Code Window that opens up.
  5. Close the VBE (Alt + Q or press the X in the top right corner).
 

Test the code:

  1. Option 1: In a worksheet cell input = HexToDec(#).
  2. Option 2: In a VBA macro input MyNum = HexToDec(#).
  3. Just replace # with your number.
 

Sample File:

HexToDec.zip 7.44KB 

Approved by mdmackillop


This entry has been viewed 99 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express