|
-
Mar 14th, 2012, 10:42 AM
#1
Thread Starter
New Member
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
-
Mar 14th, 2012, 11:06 AM
#2
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
-
Mar 14th, 2012, 11:43 AM
#3
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
 
-
Mar 14th, 2012, 02:17 PM
#4
Thread Starter
New Member
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
-
Mar 14th, 2012, 03:31 PM
#5
Re: Decimal to Binary
Thread moved to the Office Development forum.
-
Mar 14th, 2012, 04:05 PM
#6
Fanatic Member
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.
-
Mar 14th, 2012, 04:16 PM
#7
Thread Starter
New Member
Re: Decimal to Binary
How can I format my cells to text in vba?
Thanks
-
Mar 14th, 2012, 05:50 PM
#8
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|