ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   finding last used column (https://www.excelbanter.com/excel-programming/359773-finding-last-used-column.html)

Craig[_24_]

finding last used column
 
Hi

I'm looking for a way to find the last used column in a given row.
I've tried a couple methods without much luck. I've tried to replicate
the

lastrow = Cells(Rows.Count, "c").End(xlUp).Row

for rows with

lastcolumn = cells("5", columns).End(xlLeft).Column

but this never seems to work for me, one problem i think i might be
having is that i can't reference a specific row number, as i'm using a
variable to identify the row name as well. any suggestions?


Norman Jones

finding last used column
 
Hi Craig,

Try something like:

'=============
Public Sub Tester()
Dim rw As Long
Dim lastCol As Long

rw = 2 '<<==== CHANGE

lastCol = Cells(rw, Columns.Count).End(xlToLeft).Column

MsgBox lastCol
End Sub
'<<=============


---
Regards,
Norman


"Craig" wrote in message
oups.com...
Hi

I'm looking for a way to find the last used column in a given row.
I've tried a couple methods without much luck. I've tried to replicate
the

lastrow = Cells(Rows.Count, "c").End(xlUp).Row

for rows with

lastcolumn = cells("5", columns).End(xlLeft).Column

but this never seems to work for me, one problem i think i might be
having is that i can't reference a specific row number, as i'm using a
variable to identify the row name as well. any suggestions?




NickH

finding last used column
 
Hi Craig,

Try this...

Public Function LastColumn(r As Long)
If Cells(r, Columns.Count).Value = "" Then
LastColumn = Cells(r, Columns.Count).End(xlToLeft).Column
Else
LastColumn = Columns.Count
End If
End Function

You can call this function from a worksheet formula or a subroutine.

NickH



All times are GMT +1. The time now is 11:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com