Column Letters

mmetzinger

Board Regular
Joined
Dec 30, 2010
Messages
61
Ok, I know there have been like a billion questions about this but there doesn't seem to be a definitive answer and I have come to Mr. Excel to get it. I am using Office 2010 and am responsible for sorting out this huge census files we get from our customers. My issue is I write one Macro and then generalize it to be used on many different companies.

So to accomplish this I figured out how to find the last row which was easy with:
Dim LastRow as string
LastRow = "A" & Range("A1").End(xlDown).Select
'I just wrote that from memory so it may be a little wrong

But I need to be able to find the last column and store that in a variable that I can call later. What I have found is that you can get a column number with:
Range("A1").End(xlToRight).Select

But I can't figure out how to get the column letter so I can do something like:
Dim LastColumn as string
Lastcolumn = Range("A1").End(xlToRight).Select 'but column letter

My goal is to have it where in operations later I can just give it LastColumn instead of a arbitrary column that I choose myself like CC or something.

Thanks for any help.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
to get the last column


LCol = ActiveSheet.Range("IV1").End(xlToLeft).Column

then you can use it with
Cells(1, LCol)
 
Upvote 0
If you use Cells notation you can work with column numbers:

Code:
LC = Cells(1, Columns.Count).End(xlToLeft.Columns)
Cells(1, LC).Select
 
Upvote 0
I prefer to work with Column Letters so I use this
Code:
Sub LastColumn()
    Dim LC As String
    LC = ColumnLetter(Range("IV1").End(xlToLeft).Column)
End Sub
 
 
Private Function ColumnLetter(ByVal iCol As Long) As String
'=========================================
'Input column number, output column Letter
'=========================================
'A-Z
    If iCol <= 26 Then
        ColumnLetter = Chr(iCol + 64)
        Exit Function
    Else
        ColumnLetter = Chr(Int((iCol - 1) / 26) + 64) & _
                Chr(((iCol - 1) Mod 26) + 65)
    End If
End Function
John
 
Upvote 0
Yes, texasalynn is correct. The ColumnLetter Function posted works in pre 2007 Excel. Hence thr reference to
Range("IV1")
A Column Letter Function for Excel 2007 and later is
Code:
Function ColumnLetter(ColumnNumber As Long) As String
' From [URL]http://www.craigmurphy.com/blog/?p=150[/URL]
' Works in Excel 2007
Dim ColNum As Integer
Dim ColLetters As String
ColNum = ColumnNumber
ColLetters = ""
Do
ColLetters = Chr(((ColNum - 1) Mod 26) + 65) & ColLetters
ColNum = Int((ColNum - ((ColNum - 1) Mod 26)) / 26)
Loop While ColNum > 0
ColumnLetter = ColLetters
End Function
John
 
Upvote 0
To get the column letter(s) from column number, try
Code:
Sub letterfromnumber()
Dim colno&
colno = InputBox("Enter column number", 2)
MsgBox Split(Columns(colno).Address(0, 0), ":")(0)
End Sub
If you're using this as part of a larger code, it's obviously v. easy to incorporate this as a one-liner..., or use it as a user-defined function ... or whatever
 
Upvote 0
mmetzinger,


Rich (BB code):
Dim lngLastCol As Long
Dim strColName As String


''last used column in row 1
lngLastCol = Cells(1, Columns.Count).End(xlToLeft).Column

''Or:

''last used column in the active sheet
lngLastCol = Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column



strColName = Replace(Cells(1, lngLastCol).Address(0, 0), 1, "")

''Or:

strColName = Split(Columns(lngLastCol).Address, "$")(2)
 
Upvote 0

Forum statistics

Threads
1,215,389
Messages
6,124,662
Members
449,178
Latest member
Emilou

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