by Allen Wyatt
(last updated August 1, 2016)
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3063) applies to Microsoft Excel 97, 2000, 2002, and 2003.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Unprotecting a single worksheet is relatively easy. Unprotecting a whole lot of worksheets is harder. Here's how you can make ...Discover More
Paste Special is a great tool that allows you to modify the values in a range of cells in your worksheets. You may want, ...Discover More
You can hide a bunch of worksheets at the same time, but Excel makes it impossible to unhide a bunch at once. You can, ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.