Cakkie
May 23rd, 2001, 09:32 AM
I have a problem I just can get figured out (maybe because it's so late).
I need to find a way to convert a column number to the character form.
Ok, I will give an example, 1 -> A, 2 -> B, 52 -> AZ, ... you get the point. So I came up with this brilliant idea (not really) to treat it in about the same way you would treat conversion from binary to hex, but using a 26-based-like system in stead of a 16-based. At first, all look good, but after some testing, I got some errors with multiples of 26 (not including 26 itself). I think the problem is that my system does not has a zero equvalent. After AZ must come BA, not B0 <- B-ZERO.
Any suggestions
public Function XLCol(byval iCol as Long) as string
Dim lvl as Integer
Dim r as Long
Dim cpos as string
Dim strABC as string
strABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lvl = 0
Do Until 26 ^ (lvl + 1) > iCol
r = iCol Mod (26 ^ (lvl + 1))
cpos = mid(strABC, r, 1) & cpos
iCol = iCol - r
lvl = lvl + 1
Loop
cpos = mid(strABC, iCol / (26 ^ lvl), 1) & cpos
XLCol = cpos
End Function
Tom Cannaerts
slisse@planetinternet.be
Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
I need to find a way to convert a column number to the character form.
Ok, I will give an example, 1 -> A, 2 -> B, 52 -> AZ, ... you get the point. So I came up with this brilliant idea (not really) to treat it in about the same way you would treat conversion from binary to hex, but using a 26-based-like system in stead of a 16-based. At first, all look good, but after some testing, I got some errors with multiples of 26 (not including 26 itself). I think the problem is that my system does not has a zero equvalent. After AZ must come BA, not B0 <- B-ZERO.
Any suggestions
public Function XLCol(byval iCol as Long) as string
Dim lvl as Integer
Dim r as Long
Dim cpos as string
Dim strABC as string
strABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lvl = 0
Do Until 26 ^ (lvl + 1) > iCol
r = iCol Mod (26 ^ (lvl + 1))
cpos = mid(strABC, r, 1) & cpos
iCol = iCol - r
lvl = lvl + 1
Loop
cpos = mid(strABC, iCol / (26 ^ lvl), 1) & cpos
XLCol = cpos
End Function
Tom Cannaerts
slisse@planetinternet.be
Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook