VBA - Find last row with data

jgscanlon

New Member
Joined
Jan 8, 2014
Messages
46
I have a worksheet that has formulas in columns A, B, and C in rows 1-10000. If there is no value as a result of the formula, it enters a "" (blank) instead of returning "#N?A".

How do I use VBA to select only the cells that have actual values, and are not blank? In this case, I have 2493 of 10000 rows that actually have data.

Using
Code:
Range("A1", Selection.End(xlDown)).Select
does not work, as it selects all 10000 rows; I only want the first, non-blank, 2493 rows.

Thank you!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Code:
lastRow = Range("A" & Rows.Count).End(xlup).Row
Do until Range("A" & lastRow).value <> ""
     lastRow = lastRow - 1
Loop
Range("A1:A" & lastRow).Select
I might only thing that might be wrong with the code above is Range("A1:A" & lastRow).Select
You might have to figure it out if it doesn't work but the Do Loop finds that last row that isn't blank.
 
Upvote 0
I have a worksheet that has formulas in columns A, B, and C in rows 1-10000. If there is no value as a result of the formula, it enters a "" (blank) instead of returning "#N?A".

How do I use VBA to select only the cells that have actual values, and are not blank? In this case, I have 2493 of 10000 rows that actually have data.

Using
Code:
Range("A1", Selection.End(xlDown)).Select
does not work, as it selects all 10000 rows; I only want the first, non-blank, 2493 rows.
Try it like this...

Code:
LastRowWithValueInColumnA = Columns("A").Find("*", , xlValues, , xlRows, xlPrevious).Row
 
Upvote 0
Try it like this...

Code:
LastRowWithValueInColumnA = Columns("A").Find("*", , xlValues, , xlRows, xlPrevious).Row
I just noticed you said you wanted to select the cells...

Range("A1:A" & Columns("A").Find("*", , xlValues, , xlRows, xlPrevious).row).Select

The above code line assumes there are no blank cells within your data itself with the only blank cells appearing after the last displayed value.
 
Last edited:
Upvote 0
I just noticed you said you wanted to select the cells...

Range("A1:A" & Columns("A").Find("*", , xlValues, , xlRows, xlPrevious).row).Select

The above formula assumes there are no blank cells within your data itself with the only blank cells appearing after the last displayed value.

That assumption is correct - the blank cells are all after the last displayed value.

Thank you!
 
Upvote 0
Follow-up question. I've copied the previously selected data and pasted the values into a new sheet. Now how to locate the next empty/blank cell in column A of the new sheet (after pasting starting in A1). The number of records is dynamic, and will change each day.
 
Upvote 0
Follow-up question. I've copied the previously selected data and pasted the values into a new sheet. Now how to locate the next empty/blank cell in column A of the new sheet (after pasting starting in A1). The number of records is dynamic, and will change each day.
If it is only values in the cells (no formulas), you should be able to use the standard method of finding the last cell in a column and then offsetting one row from it. It is not clear what you want returned... the row number or to select the cell itself (I'll assume the latter)...

Cells(Rows.Count, "A").End(xlUp).Offset(1).Select
 
Upvote 0
If it is only values in the cells (no formulas), you should be able to use the standard method of finding the last cell in a column and then offsetting one row from it. It is not clear what you want returned... the row number or to select the cell itself (I'll assume the latter)...

Cells(Rows.Count, "A").End(xlUp).Offset(1).Select

That is amazing.

THANK YOU!!
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,399
Members
448,957
Latest member
Hat4Life

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