Hi all,
Tried to respond to your posts, but it keeps saying that my message is too short even though it is longer than the 10 characters required. Sorry.
:I got this vb conversion program from Tony who posted on Excel Forum. The program converts base 10 to base 36. The program works great. I just need it to add leading zeros in front of the base 36 number it generates. Does any one know how to do this? See the program below, thanks
Function Dec2Base(ByVal vDecimalNumber As String, ByVal vBase As Integer) As String ' convert a decimal to any base (up to 36) ' by tonywig Dim vRemainder As Integer Dim vRemainderChar As String Dim vLeftToDivide As Long Dim vDec2Base As String Dim vLooper As Integer If vBase < 2 Or vBase > 36 Then Err.Raise 5 vLeftToDivide = Val(vDecimalNumber) vDecToBase = "" vLooper = 1 While vLooper = 1 vRemainder = vLeftToDivide - Int(vLeftToDivide / vBase) * vBase vRemainderChar = Mid$("123456789ABCDEFGHIJKLMNPQRSTUVWXYZ0", vRemainder, 1) vDec2Base = Trim(vRemainderChar) & vDec2Base vLeftToDivide = Int(vLeftToDivide / vBase) If vLeftToDivide = 0 Then vLooper = 0 Wend Dec2Base = vDec2Base End Function
Last edited by norman.johnson; 07-07-2011 at 10:11 AM. Reason: Didn't follow forum rules
So much wrong with your post. Expect to get lit up by mods
Hi norman.johnson and welcome to the forum. Please take a moment to read the forum rules located here and amend your title as per Rule #1. Also, you must wrap your code in code tags as per Rule #3. Once you do that, someone will be able to help you.
Please leave a message after the beep!
I am not all that versed in the difference between Base 10 and Base 36. Just did a wikipedia search. I am not sure if the Base 36 by definition should have zeroes in front of it that is getting cut off or if you just want to add a zero in front of it. If the latter it might be a formatting issue or you could try the untested change below.
Change
To:Dec2Base = vDec2Base
Dec2Base = "0" & vDec2Base
Here's similar UDF with the ability to specify the length:
E.g., =DblToBase(1E15, 36, 12) returns 009UGXNORJLSFunction DblToBase(ByVal d As Double, _ iBase As Long, _ Optional iLen As Long = 0) As String ' shg 2007-0807 ' Returns the string conversion of positive Double d to the specified iBase (2..36) Const s As String = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z" Static sSym As Variant If IsEmpty(sSym) Then sSym = Split(s, ",") If iBase < 2 Or iBase > 36 Then DblToBase = "Invalid base!" ElseIf d < 0 Or d > 1E+15 Then DblToBase = "Invalid number!" Else Do DblToBase = sSym(d - Int(d / iBase) * iBase) & DblToBase d = Int(d / iBase) Loop While d If Len(DblToBase) < iLen Then DblToBase = String(iLen - Len(DblToBase), "0") & DblToBase End If End If End Function
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
If you are satisfied with the solution(s) provided, please mark your thread as Solved. Do not report the post, you can also give the person Rep points
How to mark a thread Solved
Go to the first post
Click edit
Click Go Advanced
Just below the word Title you will see a dropdown with the word No prefix.
Change to Solved
Click Save
Hope that helps.
RoyUK
--------
If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need
For Excel Tips & Solutions, free examples and tutorials why not check out my downloads
New members please read & follow the Forum Rules
Remember to mark your questions Solved and rate the answer(s)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks