Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the last Row used in VBA including blank spaces in between [duplicate]

Tags:

excel

vba

How can I determine the last row in an Excel sheet, including some blank lines in the middle?

With this function:

Function ultimaFilaBlanco(col As String) As Long                 Dim lastRow As Long         With ActiveSheet             lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, col).End(xlUp).row         End With                     ultimaFilaBlanco = lastRow           End Function 

And this data:

Row 1 : Value Row 2 : Value Row 3 : Value Row 4 : Value Row 5 : Value Row 6 : White Row 7 : Value Row 8 : Value Row 9 : Value Row 10  : White Row 11  : White Row 12  : White Row 13  : Value Row 14  : White 

The function returns 5, and I need 13. Any idea how to do it?

like image 808
Jonathan Raul Tapia Lopez Avatar asked Dec 03 '12 15:12

Jonathan Raul Tapia Lopez


People also ask

How do I find the last blank row in Excel VBA?

Example #2 – Using Range and SpecialCellsStep 1: Define a variable again as Long. Step 2: Start storing the value to the variable Last_Row using the assignment operator. Step 3: Start Typing Range(“A:A”). Step 4: Use the SpecialCells function to find out the last non-empty cell.

How do I find the last row 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.

What is end XLUP in Excel VBA?

The VBA snippet End(xlup). Row will find the last used row in an Excel range. Knowing the last row in Excel is useful for looping through columns of data.


1 Answers

The best way to get number of last row used is:

ActiveSheet.UsedRange.Rows.Count for the current sheet.

Worksheets("Sheet name").UsedRange.Rows.Count for a specific sheet.

To get number of the last column used:

ActiveSheet.UsedRange.Columns.Count for the current sheet.

Worksheets("Sheet name").UsedRange.Columns.Count for a specific sheet.

I hope this helps.

Abdo

like image 169
Abdelhameed Mahmoud Avatar answered Oct 26 '22 19:10

Abdelhameed Mahmoud