This article was previously published under Q213392
This article has been archived. It is offered "as is" and will no longer be updated.
This article contains a sample Microsoft Visual Basic for Applicationsfunction that converts an integer number to any base less than 10.
The following function is a sample function called "baseconv" that takes twoarguments. The first argument, InputNum, is an integer number to beconverted. The second argument, BaseNum, is the number of the base toconvert InputNum to.
Sample Visual Basic Procedure
Function baseconv(InputNum, BaseNum) Dim quotient, remainder As Single Dim answer As String quotient = InputNum ' Set quotient to number to convert. remainder = InputNum ' Set remainder to number to convert. answer = "" Do While quotient <> 0 ' Loop while quotient is not zero. ' Store the remainder of the quotient divided by base number in a ' variable called remainder. remainder = quotient Mod BaseNum ' Reset quotient variable to the integer value of the quotient ' divided by base number. quotient = Int(quotient / BaseNum) ' Reset answer to contain remainder and the previous answer. answer = remainder & answer ' Convert answer variable to a number. Loop baseconv = Val(answer)End Function
The function should be typed in a worksheet cell as follows:
For example, the following call to the baseconv function
returns the following value in the cell:
For more information about creating function procedures in Microsoft Excel, click Microsoft Visual Basic Help on the Help menu, type Writing a Function Procedure in the Office Assistant or the Answer Wizard, and then click Search to view the topic.
To access Visual Basic Help:
On the Tools menu, point to Macro, and then click Visual Basic Editor.
On the Help menu, click Microsoft Visual Basic Help.