Using VBA to select a range within a range.

G

GoBow777

I’m trying to write code to select a range based on the visible data i
the range (D4:D203) and ignore the blank cells. In other words, I hav
formulas that populate the range to produce the data, so if I have te
items of data, the first ten rows (D4 to D13) will show that data an
the remaining rows will be blank. I just want to select the range o
visible data and not the blank cells.

Code
 
J

JLatham

You said that D4:D204 have formulas in them, and the .End() method considers
formulas to be something, not empty cells even if there is no result
displayed. Usually such a formula has some test to return "" (zero-length
string) if something wasn't as expected. So I'm assuming your formulas do
that in some fashion, like
=IF(E4>0,E4,"") in D4 or
=IF(ISNA(VLOOKUP(E4,F1:G99,2,FALSE)),"",VLOOKUP(E4,F1:G99,2,FALSE))
or some similar formula with "" involved in it.

If that's the case then this line of code should select what you want.

Range("D4:" & Range("D4").Offset( _
Application.WorksheetFunction.Match("", _
Range("D4:D204"), 0) - 2, 0).Address).Select
 
G

GoBow777

JLatham;878699 said:
You said that D4:D204 have formulas in them, and the .End() metho
considers
formulas to be something, not empty cells even if there is no result
displayed. Usually such a formula has some test to return "
(zero-length
string) if something wasn't as expected. So I'm assuming your formula
do
that in some fashion, like
=IF(E4>0,E4,"") in D4 or
=IF(ISNA(VLOOKUP(E4,F1:G99,2,FALSE)),"",VLOOKUP(E4,F1:G99,2,FALSE))
or some similar formula with "" involved in it.

If that's the case then this line of code should select what you want.

Range("D4:" & Range("D4").Offset( _
Application.WorksheetFunction.Match("", _
Range("D4:D204"), 0) - 2, 0).Address).Select




:
-

JLatham:

Yes you are correct. A poor explanation on my part.

Very cool, it works perfectly, thank you so much
 
J

JLatham

Glad I got it right. You're welcome.

GoBow777 said:
JLatham:

Yes you are correct. A poor explanation on my part.

Very cool, it works perfectly, thank you so much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top