Results 1 to 8 of 8

Thread: Decimal to Binary

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2012
    Posts
    3

    Decimal to Binary

    hello,
    i am trying to do Decimal to 7bit binary conversion, my function is working fine when i use it in excel, but when i include it to a sub routine, i only shows 1 digit,


    Function dectobin1(DeciValue As Long, Optional NoOfBits As Integer = 7) As String
    Dim i As Integer

    Do While DeciValue > (2 ^ NoOfBits) - 1
    NoOfBits = NoOfBits + 8
    Loop
    dectobin1 = vbNullString
    For i = 0 To (NoOfBits - 1)
    dectobin1 = CStr((DeciValue And 2 ^ i) / 2 ^ i) & dectobin1
    Next i


    End Function
    Sub generate()

    Dim dec(1 To 100) As Integer
    Dim bin(1 To 100) As String
    Dim i As Long
    For i = 1 To 100 Step 1
    Sheet1.Cells(13 + i, 1).Value = i
    bin(i) = dectobin1(i)
    Sheet1.Cells(13 + i, 4).Value = bin(i)
    Next i

    End Sub

  2. #2
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: Decimal to Binary

    I think this is VBA
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Decimal to Binary

    Ask a moderator via PM to move this thread to the VBA section. It certainly isn't .NET.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2012
    Posts
    3

    Re: Decimal to Binary

    Hello,
    i am sorry i posted this in the wrong forums, please if there is moderator who can move it.

    Thanks

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Decimal to Binary

    Thread moved to the Office Development forum.

  6. #6
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: Decimal to Binary

    There is nothing wrong with your code that I can see. You need to format your cells to text or excel will remove the leading zeroes.

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2012
    Posts
    3

    Re: Decimal to Binary

    How can I format my cells to text in vba?


    Thanks

  8. #8
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: Decimal to Binary

    Sheet1.Cells(13 + i, 4).Numberformat = "@"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width