Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel 97-Convert number to column letter

Status
Not open for further replies.

Hawkide

Technical User
Joined
Oct 8, 2003
Messages
159
Location
US
In an Excel 97 VBA procedure, I have two integers representing columns. For example, integers are 1 and 5 are referring to columns A and E respectively. From the two integers, I would like to create a string representing the column letters.

Basically,
1 and 5 should result in "A:E"
2 and 6 should result in "B:F"
3 and 27 should result in "C:AA"

I will be using the string to reference a range of columns...Range("A:E"). Any ideas would be greatly appreciated. TIA...
 
Here's a little function that converts a column number to the corresponding letter designation.
[blue][tt]
' ============================================================================
' Return the letter designation for an Excel column given the column number.
' ============================================================================
Private Function CL(ByVal Col As Integer) As String
Const AB As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
If Col <= 26 Then
CL = Mid$(AB, Col, 1)
Else
If Col Mod 26 <> 0 Then
CL = Mid$(AB, Int(Col / 26), 1) & Mid$(AB, (Col Mod 26), 1)
Else
CL = Mid$(AB, Int(Col / 26), 1) & Mid$(AB, 26, 1)
End If
End If
End Function
[/tt][/blue]
 
I was hoping to do it w/o creating a custom function, but your function works nicely, so I will go ahead and plagiarize it. Thanks for your help.
 
Range(Cells(1, i), Cells(1, j)).EntireColumn
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top