How do I prevent blank cells from being included in a dynamic range?

logandiana

Board Regular
Joined
Feb 21, 2017
Messages
107
I have a situation where I am copying one set of cells that are all formulas and pasting special values into another worksheet.
For example:

A
B
C
D
E
F
G
H
I
1
Sam
10
=if(A1="","",A1)
=if(A1="","",B1)
2
Dave
20
=if(A2="","",A2)
=if(A2="","",B2)
3
Fred
30
=if(A3="","",A3)
=if(A3="","",B3)
4
Harry
40
=if(A4="","",A4)
=if(A4="","",B4)
5
=if(A5="","",A5)
=if(A5="","",B5)
6
=if(A6="","",A6)
=if(A6="","",B6)
7
=if(A7="","",A7)
=if(A7="","",B7)
8
=if(A8="","",A8)
=if(A8="","",B8)
9
=if(A9="","",A9)
=if(A9="","",B9)

<tbody>
</tbody>


I copy columns E:F (or I copy using a named range with a Counta formula to find the last row). When I Paste Special Values in to the new worksheet in A1 I get the following. Everything looks right from here...

A
B
C
D
E
F
G
H
I
1
Sam
10
2
Dave
20
3
Fred
30
4
Harry
40
5
6
7
8
9

<tbody>
</tbody>


Now using LastRow = Cells(Rows.Count,1).End(xlUp).Row

The LastRow shows to be 9...

But I am scratching my head here.
I thought the way the LastRow worked was to go down to bottom of the worksheet and then back up to the last row where there's something in the cell.
It seems to me that it should have come back up to Row 4 (which is where I want it to be) since there's nothing in cells 5-9.
Can someone explain this and help me figure out how I can get this to work for me?
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try...

Code:
LastRow = Columns(1).Find("*", , xlValues, , , xlPrevious).Row
 
Last edited:
Upvote 0
Even though row 5-9 look blank excel does not see them as blank. The copy past as values is putting something in the cells that excel sees.
 
Last edited:
Upvote 0
Thank you Mark858, this works!

Can you or anyone explain the 'why' behind this though?

And from what Scott T says: Excel does not see them as blank...

I'd like to understand the mechanics behind Excel on this one.
 
Upvote 0
Put the formula =ISBLANK(A9) in a spare cell and what do you get?
A blank cell is a cell that is total empty so a cell with a Formula returning "" isn't blank, nor is a cell which has a space or any other non-printing character.

Edit...

You can also post what the formula =CODE(A9) gives you.
 
Last edited:
Upvote 0
Mark is checking the last value in Column A ( column 1) only.
A better dynamic range is actually the Table method. This also gives you a better object to work with or using the Current Range.
 
Upvote 0
If you want to check the entire sheet (not the OP's question as the OP was only looking at column A with their last row code) then you just change Columns(1) to Cells which is more reliable than the current region as it doesn't rely on having contiguous data and in the OP's case it would probably/will still see row 9 rather than 4.

As for Tables it is a good solution if you like them.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,974
Members
448,537
Latest member
Et_Cetera

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