Using BIN2DEC In a Macro

Written by Allen Wyatt (last updated January 2, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003


1

Most of Excel's worksheet functions can be accessed in VBA by using the WorksheetFunction object. Some functions may not seem to be available, however. One such function is BIN2DEC, which converts a binary value to a decimal value. The reason it isn't available is that BIN2DEC isn't really an Excel worksheet function—it is part of the Analysis ToolPak add-in.

That being the case, you have two options: you can either load the VBA equivalent of the Analysis ToolPak, or you can create your own BIN2DEC function in VBA. To do the first, make sure that in Excel you install the Analysis ToolPak — VBA add-in. If it is not listed in the available add-ins, use Windows to search for the file ATPVBAEN.XLA. (If you are using a language version of Excel other than English, then the "EN" portion of the file will be different.) This is the actual add-in you want to enable.

Once you've enabled the add-in, display the VBA Editor and choose Tools | References to display the References dialog box. Make sure the atpvbaen.xla reference is selected. Close the dialog box, and you can then use BIN2DEC just like you would any other worksheet function.

The other option is to create your own BIN2DEC function. The following is an example of a function that accepts a string that contains the binary digits and returns a numeric value that represents the decimal value of that string.

Function Bin2Dec(sMyBin As String) As Long
    Dim x As Integer
    Dim iLen As Integer

    iLen = Len(sMyBin) - 1
    For x = 0 To iLen
        Bin2Dec = Bin2Dec + _
          Mid(sMyBin, iLen - x + 1, 1) * 2 ^ x
    Next
End Function

This function actually doesn't have the same limitations as the BIN2DEC worksheet function; it will work with binary numbers containing more than 10 digits.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3063) applies to Microsoft Excel 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Inserting a File

Need to combine several files into a single document? You can do it by inserting one file into another, as outlined in ...

Discover More

Different Layouts for Footnotes

If you want to have footnotes appear in a different number of columns than what your text appears in, you may be out of ...

Discover More

Opening Multiple Workbooks at Once

Need to open a bunch of workbooks from within Excel? It's easy to do when you construct a selection set in the Open ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (menu)

Finding the Last-Used Cell in a Macro

Ever wonder what the macro-oriented equivalent of pressing Ctrl+End is? Here's the code and some caveats on using it.

Discover More

Converting Text Case

Ever notice that if someone types in all CAPS, it appears they are shouting? If your worksheets include lots of text, you ...

Discover More

Converting Text to Numbers

Import information from a program external to Excel, and your numbers may be treated as text because of the way that the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 5?

2021-01-02 10:46:42

Rick Rothstein

Not that anyone would ever need to handle the monstrous numbers my function is capable of, I thought I would post it anyway.

' BinaryString argument can be a maximum of 96 digits (either 0's or 1's)
Function BinToDec(BinaryString As String) As Variant
Dim X As Integer
Const TwoToThe48 As Variant = 281474976710656#
For X = 0 To Len(BinaryString) - 1
If X > 48 Then
BinToDec = CDec(BinToDec) + Val(Mid(BinaryString, Len(BinaryString) - X, 1)) * TwoToThe48 * CDec(2 ^ (X - 48))
Else
BinToDec = CDec(BinToDec) + Val(Mid(BinaryString, Len(BinaryString) - X, 1)) * CDec(2 ^ X)
End If
Next
If Len(BinToDec) > 10 Then BinToDec = CStr(BinToDec)
End Function

And in case you should need the inverse for this function...

' The DecimalIn argument is limited to 79228162514264337593543950266
' (approximately 96-bits) - large numerical values must be entered
' as a String value to prevent conversion to scientific notation.
Function DecToBin(ByVal DecimalIn As Variant, Optional NumberOfBits As Variant) As String
DecToBin = ""
DecimalIn = CDec(DecimalIn)
Do While DecimalIn <> 0
DecToBin = Trim$(Str$(DecimalIn - 2 * Int(DecimalIn / 2))) & DecToBin
DecimalIn = Int(DecimalIn / 2)
Loop
If Not IsMissing(NumberOfBits) Then
If Len(DecToBin) > NumberOfBits Then
DecToBin = "Error - Number too large for bit size"
Else
DecToBin = Right$(String$(NumberOfBits, "0") & DecToBin, NumberOfBits)
End If
End If
End Function


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.