PDA

View Full Version : Convert Binary to decimal on and vice versa on VBA



MsPattie
10-30-2019, 01:54 PM
Dear All,
I need help with converting the decimal number 46 to Binary and converting the binary result back to decimal on VBA please. Thanks in advance

paulked
10-30-2019, 02:11 PM
A quick Google gives this https://excel.tips.net/T003063_Using_BIN2DEC_In_a_Macro.html

MsPattie
10-30-2019, 02:16 PM
This is what i saw. I am kind of confused:banghead:
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

paulked
10-30-2019, 02:29 PM
That function works, try it with


Sub TestB2D()
MsgBox Bin2Dec(1011)
End Sub

MsPattie
10-30-2019, 02:46 PM
Error message: sub or function not defined. Please can you help me with the exact number i wrote up there because I am still confused

paulked
10-30-2019, 02:50 PM
Copy this function you posted into a module



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


and then try it.

MsPattie
10-30-2019, 02:54 PM
I have copied it. WHere does the decimal 46 come into the program?

paulked
10-30-2019, 02:56 PM
It doesn't unless you put 101110 in the test!



Sub TestB2D()
MsgBox Bin2Dec(101110)
End Sub

paulked
10-30-2019, 02:57 PM
Why do you want this in VBA? There are hundreds of B to D converters out there.

MsPattie
10-30-2019, 03:03 PM
I just started self studying VBA so i dont really understand you. I am trying to understand step by step conversion of binary to decimal using VBA and your answers are confusing

paulked
10-30-2019, 03:13 PM
Then maybe you should ask the right questions!

MsPattie
10-30-2019, 03:18 PM
The question is convert the decimal 46 to binary using VBA code. So I need a step by step approach for a beginnner-

paulked
10-30-2019, 03:48 PM
I also need a lot of things, but I don't get them by demanding them.

Asking a bunch of volunteers to teach you VBA needs some input from you and some courtesy.

There are loads of people here willing to give up their time and experience to help out, but with the wrong attitude you won't get far.

Perhaps you could start by showing how far you have got with VBA. If I confused you with a function, then maybe you shouldn't be starting with conversions? Or is this a project set by a school?

Try recording some macros and seeing how they work first. Step through them in the VBA window and see if you understand what's going on, then ask us any questions about what you don't understand.

MsPattie
10-30-2019, 04:16 PM
I beg your pardon where did i make demands here? You go back and read my post .you would see the words please and thank you so what else do you want me to do? Lay myself on the ground for you? You were like me one day so why the pride? Besides you are not omini knowest. I just figured the program out and i was about to thank you for the help and saw this:doh:

SamT
10-30-2019, 05:27 PM
@ Paul,
Oooh, Karen is an entitled, bossy, victim, ain't she? :rofl:


@ MsPattie (http://www.vbaexpress.com/forum/member.php?75340-MsPattie) ,
If you are going to get into computers deeper than a data entry clerk, you really need to grow a thicker skin. May I suggest that you read some of https://www.reddit.com/r/talesfromtechsupport/? :friends:
:beerchug:


ps: Paul was not out of line; Perception is everything in IT. st

paulked
10-30-2019, 05:40 PM
:whistle: Thank you Sam. I don't hold grudges and am always willing to help, the 'thanks', or lack of them, mean little against the willingness to learn by helping others ;)
:beerchug: