Write a VBA code to convert an arbitrary binary INTEGER number to equivalent decimal number. Specific requirements: 1. Use InputBox function to request the input for an arbitrary binary number; 2. Convert the binary number to equivalent decimal integer number; 3. Return the decimal number in a message box.
Below functions may be useful in your VBA code if you input binary number as string: Cstr( ): can convert a number in ( ) to a string Len( ): count the length of the string in ( ) Val( ): can convert a string number in ( ) to numerical number Mid(xxx,x,x): can pick up one or more characters from the string variable in ( ) starting from the left end of the string.
Expert Answer
Copyable Code
Option Explicit
Sub decimaltoBinary()
‘declare dec to store decimal integer input
Dim dec As Integer
‘declare bin to store converted binary
Dim bin As String
‘bit to store the bit generated by the algorithm in each iteration
Dim bit As Integer
‘prompt user to input the decimal value
dec = InputBox(“Enter decimal integer value “)
‘initialize bin to empty string
bin = “”
‘To get a bit of a dec ,get remainder by dividing the dec by 2
‘Get next bit by updating the dec to its half (divide dec by 2 ) untill dec becomes 0
Do While dec <> 0
bit = dec Mod 2 ‘get the bit
bin = CStr(bit) & bin ‘add this bit to the left of the binary calculated so far
dec = Int(dec / 2) ‘convert decimal into integer
Loop ‘iterate over while loop till dec becomes 0
MsgBox bin ‘show the binary in msgbox
End Sub
SCREEN SHOTS OF EXECUTION
Input
OUTPUT