VBA to Convert column number to letter

Whisperer14

Well-known Member
Joined
Nov 6, 2002
Messages
589
What is the efficient way to convert a known column number to its corresponding letter - with acknowledgements to Aladin for the R1C1 formula...

Lastcol is the known column number, iStr a string variable
Code:
    Cells(1, LastCol).FormulaR1C1 = "=SUBSTITUTE(ADDRESS(1,COLUMN(),4),""1"","""")"
    iStr = Cells(1, LastCol).Value
    Cells(1, LastCol).ClearContents  'just to tidy up the sheet
    Columns("E" & iStr).Copy
    etc...

TIA GT
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Why do you need to as a matter of interest?
 
Upvote 0
Why do you need the letter for the column?

Can't you work with the number?
 
Upvote 0
Hi Whisperer.

As said above you should be able to use the number but 'just for fun':

Code:
Function ColLet(x As Integer) As String
With ActiveSheet.Columns(x)
    ColLet = Left(.Address(False, False), InStr(.Address(False, False), ":") - 1)
End With
End Function

and to test it

Code:
Sub test()
MsgBox ColLet(35)
End Sub
 
Upvote 0
Hi All,
Thanks 4 the replies but when I went for the line:=

Columns(5,Lastcol).Copy - it threw a wobbly so I decided to go for the letters, so....

Reasking the question - What VBA should I have used to select the columns between 5 and lastcol?
 
Last edited:
Upvote 0
Re: VBA to Convert column number to letter CLOSED

As it says in my signature block - Never too old to learn - basics doooohhhh, where is that embarassed icon..... got it - :oops:

Thanks for the refresher

GT
 
Upvote 0
No need to be embarrassed. In my opinion that syntax is far from intuitive considering that the following works with rows:

Code:
Rows("5:" & lastrow).Copy
 
Upvote 0
Hi Whisperer.

As said above you should be able to use the number but 'just for fun':

Code:
Function ColLet(x As Integer) As String
With ActiveSheet.Columns(x)
    ColLet = Left(.Address(False, False), InStr(.Address(False, False), ":") - 1)
End With
End Function

and to test it

Code:
Sub test()
MsgBox ColLet(35)
End Sub


to make fun a bit more fun....



Private Sub CommandButton1_Click()
'assuming the row in question is row(1) and the column needs to be determined and the next free cell in that row needs to be used... ( ofcourse you'd remove the 'offset' statement if the last used cell needs to be referenced)

lstcol = Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).Column
colrng = (Mid(Cells(1, lstcol).Address, 2, 1) & 1)
MsgBox (colrng)
Range(colrng) = "this is the next available cell in this row

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top