Last used column letter
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Last used column letter
I'm a little braindead this morning....
How can I get to the last used column letter in row 6?
How can I get to the last used column letter in row 6?
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78512
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Last used column letter
Sorry, using vba.
My end result will be:
Range("C22:X22").Formula = "=SUM(C11:C20)"
X being the last used column in this instance.
My end result will be:
Range("C22:X22").Formula = "=SUM(C11:C20)"
X being the last used column in this instance.
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78512
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Last used column letter
Here is a function you can use:
Use like this:
Dim strCol As String
strCol = LastCol(6)
or
Dim strCol As String
strCol = LastCol(6, "MySheet")
Code: Select all
Function LastCol(RowNum As Long, Optional SheetName As String) As String
Dim rngCell As Range
Dim strAddress As String
Dim intPos As Integer
If SheetName = "" Then
SheetName = ActiveSheet.Name
End If
With Worksheets(SheetName)
Set rngCell = .Cells(RowNum, .Columns.Count).End(xlToLeft)
End With
strAddress = rngCell.Address
intPos = InStr(2, strAddress, "$")
LastCol = Mid(strAddress, 2, intPos - 2)
End Function
Dim strCol As String
strCol = LastCol(6)
or
Dim strCol As String
strCol = LastCol(6, "MySheet")
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Last used column letter
Thanks Hans. I was sure that I had used a simpler method previously that I just found.
iCol = Mid(Cells(6, 256).End(xlToLeft).Columns.Address, 2, 1)
This works up until Z, but I guess that I will run into trouble thereafter.
iCol = Mid(Cells(6, 256).End(xlToLeft).Columns.Address, 2, 1)
This works up until Z, but I guess that I will run into trouble thereafter.
Nathan
There's no place like home.....
There's no place like home.....
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Last used column letter
Code: Select all
If Mid(Cells(6, 256).End(xlToLeft).Columns.Address, 4, 1) <> "$" Then
iCol = Mid(Cells(6, 256).End(xlToLeft).Columns.Address, 2, 1)
Else
iCol = Mid(Cells(6, 256).End(xlToLeft).Columns.Address, 2, 2)
End If
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78512
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Last used column letter
It wouldn't work if you had a lot of columns in Excel 2007 or later, where the columns go up to XFD.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Last used column letter
Hans, I have taken to using this function...HansV wrote:Here is a function you can use:
Use like this:Code: Select all
Function LastCol(RowNum As Long, Optional SheetName As String) As String Dim rngCell As Range Dim strAddress As String Dim intPos As Integer If SheetName = "" Then SheetName = ActiveSheet.Name End If With Worksheets(SheetName) Set rngCell = .Cells(RowNum, .Columns.Count).End(xlToLeft) End With strAddress = rngCell.Address intPos = InStr(2, strAddress, "$") LastCol = Mid(strAddress, 2, intPos - 2) End Function
Dim strCol As String
strCol = LastCol(6)
or
Dim strCol As String
strCol = LastCol(6, "MySheet")
How can I find the last used column BEFORE Column AC?
Nathan
There's no place like home.....
There's no place like home.....
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Last used column letter
This would be one of a few requirements, therefore would it be possible to pass AC as an argument?
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78512
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Last used column letter
Change the line
Set rngCell = .Cells(RowNum, .Columns.Count).End(xlToLeft)
to
Set rngCell = .Range("AC" & RowNum).End(xlToLeft)
Set rngCell = .Cells(RowNum, .Columns.Count).End(xlToLeft)
to
Set rngCell = .Range("AC" & RowNum).End(xlToLeft)
Best wishes,
Hans
Hans
-
- Administrator
- Posts: 78512
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Last used column letter
You added a new request while I posted my previous reply. Here goes.
Use like this:
lngLast = LastCol(37, "AC")
Code: Select all
Function LastCol(RowNum As Long, ColName As String, Optional SheetName As String) As String
Dim rngCell As Range
Dim strAddress As String
Dim intPos As Integer
If SheetName = "" Then
SheetName = ActiveSheet.Name
End If
With Worksheets(SheetName)
Set rngCell = .Range(ColName & RowNum).End(xlToLeft)
End With
strAddress = rngCell.Address
intPos = InStr(2, strAddress, "$")
LastCol = Mid(strAddress, 2, intPos - 2)
End Function
lngLast = LastCol(37, "AC")
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Last used column letter
Hans, how can I get a column letter from another workbook? Something like....
strCol1 = LastCol(5, "AC", wb1.Sheets("Report"))
Also (separate question)
This returns the last used column letter. How can I generate the next column letter (first unused)
TIA
strCol1 = LastCol(5, "AC", wb1.Sheets("Report"))
Also (separate question)
This returns the last used column letter. How can I generate the next column letter (first unused)
TIA
Nathan
There's no place like home.....
There's no place like home.....
-
- Administrator
- Posts: 78512
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Last used column letter
Here is a new and different version of the function:
Important: the third argument is no longer the name of a sheet, but a Worksheet object which can refer to a sheet in the active workbook or in another workbook. If you omit this argument, the active sheet in the active workbook is used.
The fourth argument is a column offset from the last used cell. If you omit it or specify 0, you get the column of the last used cell. To get the column of the next cell to the right, specify 1, and to get the column of the next cell to the left, specify -1.
Examples:
LastCol(5, "AC") returns the column name of the last used cell in row 5 to the left of column AC in the active sheet.
LastCol(5, "AC", , 1) returns the column name of the first unused cell in row 5 to the left of column AC in the active sheet.
LastCol(5, "AC", Worksheets)"Summary")) returns the column name of the last used cell in row 5 to the left of column AC in the Summary sheet in the active workbook.
LastCol(5, "AC", Workbooks("Invoices.xls").Worksheets("Summary"), 1) returns the column name of the first unused cell in row 5 to the left of column AC in the Summary sheet in the Invoices.xls workbook.
Code: Select all
Function LastCol(RowNum As Long, ColName As String, _
Optional Sheet As Worksheet, Optional Offs As Long) As String
Dim rngCell As Range
Dim strAddress As String
Dim intPos As Integer
If Sheet Is Nothing Then
Set Sheet = ActiveSheet
End If
With Sheet
Set rngCell = .Range(ColName & RowNum).End(xlToLeft).Offset(0, Offs)
End With
strAddress = rngCell.Address
intPos = InStr(2, strAddress, "$")
LastCol = Mid(strAddress, 2, intPos - 2)
End Function
The fourth argument is a column offset from the last used cell. If you omit it or specify 0, you get the column of the last used cell. To get the column of the next cell to the right, specify 1, and to get the column of the next cell to the left, specify -1.
Examples:
LastCol(5, "AC") returns the column name of the last used cell in row 5 to the left of column AC in the active sheet.
LastCol(5, "AC", , 1) returns the column name of the first unused cell in row 5 to the left of column AC in the active sheet.
LastCol(5, "AC", Worksheets)"Summary")) returns the column name of the last used cell in row 5 to the left of column AC in the Summary sheet in the active workbook.
LastCol(5, "AC", Workbooks("Invoices.xls").Worksheets("Summary"), 1) returns the column name of the first unused cell in row 5 to the left of column AC in the Summary sheet in the Invoices.xls workbook.
Best wishes,
Hans
Hans
-
- 5StarLounger
- Posts: 1185
- Joined: 24 Jan 2010, 12:02
- Location: Wales, UK.
Re: Last used column letter
It must be said, you are an absolute DIAMOND!
Nathan
There's no place like home.....
There's no place like home.....