-
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
-
Re: Decimal to Binary
-
Re: Decimal to Binary
Ask a moderator via PM to move this thread to the VBA section. It certainly isn't .NET.
-
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
-
Re: Decimal to Binary
Thread moved to the Office Development forum.
-
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.
-
Re: Decimal to Binary
How can I format my cells to text in vba?
Thanks
-
Re: Decimal to Binary
Sheet1.Cells(13 + i, 4).Numberformat = "@"