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

logandiana

Board Regular
Joined
Feb 21, 2017
Messages
94
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?
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,973
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
Try...

Code:
LastRow = Columns(1).Find("*", , xlValues, , , xlPrevious).Row
 
Last edited:

Scott T

Well-known Member
Joined
Dec 14, 2016
Messages
2,616
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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:

logandiana

Board Regular
Joined
Feb 21, 2017
Messages
94
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.
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,973
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile

ADVERTISEMENT

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:

SpillerBD

Well-known Member
Joined
Jul 2, 2014
Messages
2,710
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.
 

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,973
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
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:

Watch MrExcel Video

Forum statistics

Threads
1,127,215
Messages
5,623,434
Members
415,974
Latest member
ZorroOP

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
Top