Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the last column with data?

Tags:

excel

vba

I've found this method for finding the last data containing row in a sheet:

ws.Range("A65536").End(xlUp).row 

Is there a similar method for finding the last data containing column in a sheet?

like image 668
Neat Machine Avatar asked Aug 13 '12 00:08

Neat Machine


People also ask

How do I find the last column of data in Excel?

Locate the last cell that contains data or formatting on a worksheet. To locate the last cell that contains data or formatting, click anywhere in the worksheet, and then press CTRL+END.

How do you go to the last column of data?

1. Select the Last Used Cell. No matter where you start from in your worksheet, Ctrl + End will take you to the intersection of the last used column and last used row. Sometimes, when you use this shortcut, Excel will move your selection so that is farther to the right or farther down than the data range you can see.

How do I select the last row in Excel with data?

Ctrl-Down -- The shortcut moves the cursor to the last row with data before the first blank row that is encountered; this may be the last row in the table ideally, but only if there are not any blank rows in the table. Ctrl-Up -- The shortcut moves the cursor to the first row with data before a blank row.


1 Answers

Lots of ways to do this. The most reliable is find.

Dim rLastCell As Range  Set rLastCell = ws.Cells.Find(What:="*", After:=ws.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _ xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False)  MsgBox ("The last used column is: " & rLastCell.Column) 

If you want to find the last column used in a particular row you can use:

Dim lColumn As Long  lColumn = ws.Cells(1, Columns.Count).End(xlToLeft).Column 

Using used range (less reliable):

Dim lColumn As Long  lColumn = ws.UsedRange.Columns.Count 

Using used range wont work if you have no data in column A. See here for another issue with used range:

See Here regarding resetting used range.

like image 197
Reafidy Avatar answered Sep 24 '22 13:09

Reafidy