Dec2Bin, Conver Decimal to Binary

mosses

New Member
Joined
May 23, 2008
Messages
2
=Dec2Bin(), I just get "#NUM!" Error = there is a problem with the number used in this formula. This happens with numbers over 1000. how can I get a convention over 1000 ?

Many thanks
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
This UDF by Rob Bovey shows how to exceed the 512 limitation, without a need for the Analysis ToolPak to be installed.

Example, with number to be converted in A1:
=vdecimaltobinary(A1) returns 0000000000001010 (twelve zeros and 1010)

Code:
Option Explicit
'Function by Rob Bovey
Function vDecimalToBinary( _
   ByVal lDecimal As Long, _
   Optional ByVal bReturnArray As Boolean = False, _
   Optional ByVal b32Bit As Boolean = False) As Variant
    Const l16_BITS As Long = 15
    Const l32_BITS As Long = 31
    
    Dim dPlaceArray() As Double
    Dim lCount As Long
    Dim lNumBits As Long
    Dim lReturn() As Long
    Dim szReturn As String
    If b32Bit Then lNumBits = l32_BITS Else lNumBits = l16_BITS
    ReDim dPlaceArray(0 To lNumBits)
    ReDim lReturn(0 To lNumBits)
    For lCount = LBound(dPlaceArray) To UBound(dPlaceArray)
        dPlaceArray(lCount) = 2 ^ lCount
    Next lCount
    For lCount = UBound(dPlaceArray) To LBound(dPlaceArray) Step -1
    
        If CBool(dPlaceArray(lCount) And lDecimal) Then
            If bReturnArray Then
                lReturn(lCount) = 1
            Else
                szReturn = szReturn & Chr$(49)
            End If
        Else
            If bReturnArray Then
                lReturn(lCount) = 0
            Else
                szReturn = szReturn & Chr$(48)
            End If
        End If
        
    Next lCount
    
    If bReturnArray Then
        vDecimalToBinary = lReturn()
    Else
        vDecimalToBinary = szReturn
    End If
    
End Function
 
Upvote 0

Forum statistics

Threads
1,223,990
Messages
6,175,817
Members
452,672
Latest member
missbanana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top