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:

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


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...

[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A
[/TD]
[TD]B
[/TD]
[TD]C
[/TD]
[TD]D
[/TD]
[TD]E
[/TD]
[TD]F
[/TD]
[TD]G
[/TD]
[TD]H
[/TD]
[TD]I
[/TD]
[/TR]
[TR]
[TD]1
[/TD]
[TD]Sam
[/TD]
[TD]10
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2
[/TD]
[TD]Dave
[/TD]
[TD]20
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3
[/TD]
[TD]Fred
[/TD]
[TD]30
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4
[/TD]
[TD]Harry
[/TD]
[TD]40
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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,223,268
Messages
6,171,099
Members
452,379
Latest member
IainTru

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