Select all cells NOT in a range

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to use SpecialCells to select all the non-blank cells in my range.
If my approach is correct please tell me how to complete my code, or if
there's a better way...

'my approach is to:
set Blanks=MyRange.SpecialCells(xlCellTypeBlanks)
'and then
set MyRange=intersect ( MyRange , [not blanks] )

'but I can't work out how to get [not blanks]

Thanks in advance
Rob
 
Dim rng as Range, rng1 as Range, rng2 as Range
Set rng = MyRange
set rng1 = rng.specialcells(xlFormulas)
set rng2 = rng.Specialcells(xlconstants)
if not rng1 is nothing and not rng2 is nothing then
set rng = Union(rng1,rng2)
elseif not rng1 is nothing then
set rng = rng1
elseif not rng2 is nothing then
set rng = rng2
Else
set rng = nothing
end if
msgbox rng.Address
 
Thanks Tom.

Tom Ogilvy said:
Dim rng as Range, rng1 as Range, rng2 as Range
Set rng = MyRange
set rng1 = rng.specialcells(xlFormulas)
set rng2 = rng.Specialcells(xlconstants)
if not rng1 is nothing and not rng2 is nothing then
set rng = Union(rng1,rng2)
elseif not rng1 is nothing then
set rng = rng1
elseif not rng2 is nothing then
set rng = rng2
Else
set rng = nothing
end if
msgbox rng.Address

--
Regards,
Tom Ogilvy

Rob said:
I'm trying to use SpecialCells to select all the non-blank cells in my range.
If my approach is correct please tell me how to complete my code, or if
there's a better way...

'my approach is to:
set Blanks=MyRange.SpecialCells(xlCellTypeBlanks)
'and then
set MyRange=intersect ( MyRange , [not blanks] )

'but I can't work out how to get [not blanks]

Thanks in advance
Rob
 
I'd really like this as a one liner, which would be posible if specialcells
didn't cause an error. Any ideas?

Union( MyRange.specialcells(xlFormulas) , Myrange.SpecialCells(xlconstants) )


Tom Ogilvy said:
Dim rng as Range, rng1 as Range, rng2 as Range
Set rng = MyRange
set rng1 = rng.specialcells(xlFormulas)
set rng2 = rng.Specialcells(xlconstants)
if not rng1 is nothing and not rng2 is nothing then
set rng = Union(rng1,rng2)
elseif not rng1 is nothing then
set rng = rng1
elseif not rng2 is nothing then
set rng = rng2
Else
set rng = nothing
end if
msgbox rng.Address

--
Regards,
Tom Ogilvy

Rob said:
I'm trying to use SpecialCells to select all the non-blank cells in my range.
If my approach is correct please tell me how to complete my code, or if
there's a better way...

'my approach is to:
set Blanks=MyRange.SpecialCells(xlCellTypeBlanks)
'and then
set MyRange=intersect ( MyRange , [not blanks] )

'but I can't work out how to get [not blanks]

Thanks in advance
Rob
 
No, you can't union with nothing.

I didn't make it multiline because I like to be verbose - in fact I left out
two critical lines

Dim rng as Range, rng1 as Range, rng2 as Range
Set rng = MyRange
On Error Resume Next
set rng1 = rng.specialcells(xlFormulas)
set rng2 = rng.Specialcells(xlconstants)
On Error goto 0
if not rng1 is nothing and not rng2 is nothing then
set rng = Union(rng1,rng2)
elseif not rng1 is nothing then
set rng = rng1
elseif not rng2 is nothing then
set rng = rng2
Else
set rng = nothing
end if


--
Regards,
Tom Ogilvy

Rob said:
I'd really like this as a one liner, which would be posible if specialcells
didn't cause an error. Any ideas?

Union( MyRange.specialcells(xlFormulas) ,
Myrange.SpecialCells(xlconstants) )
Tom Ogilvy said:
Dim rng as Range, rng1 as Range, rng2 as Range
Set rng = MyRange
set rng1 = rng.specialcells(xlFormulas)
set rng2 = rng.Specialcells(xlconstants)
if not rng1 is nothing and not rng2 is nothing then
set rng = Union(rng1,rng2)
elseif not rng1 is nothing then
set rng = rng1
elseif not rng2 is nothing then
set rng = rng2
Else
set rng = nothing
end if
msgbox rng.Address

--
Regards,
Tom Ogilvy

Rob said:
I'm trying to use SpecialCells to select all the non-blank cells in my range.
If my approach is correct please tell me how to complete my code, or if
there's a better way...

'my approach is to:
set Blanks=MyRange.SpecialCells(xlCellTypeBlanks)
'and then
set MyRange=intersect ( MyRange , [not blanks] )

'but I can't work out how to get [not blanks]

Thanks in advance
Rob
 
ok thanks very much, I'll do it as a seperate function. Sorry to have called
you verbose :-)

Tom Ogilvy said:
No, you can't union with nothing.

I didn't make it multiline because I like to be verbose - in fact I left out
two critical lines

Dim rng as Range, rng1 as Range, rng2 as Range
Set rng = MyRange
On Error Resume Next
set rng1 = rng.specialcells(xlFormulas)
set rng2 = rng.Specialcells(xlconstants)
On Error goto 0
if not rng1 is nothing and not rng2 is nothing then
set rng = Union(rng1,rng2)
elseif not rng1 is nothing then
set rng = rng1
elseif not rng2 is nothing then
set rng = rng2
Else
set rng = nothing
end if


--
Regards,
Tom Ogilvy

Rob said:
I'd really like this as a one liner, which would be posible if specialcells
didn't cause an error. Any ideas?

Union( MyRange.specialcells(xlFormulas) ,
Myrange.SpecialCells(xlconstants) )
Tom Ogilvy said:
Dim rng as Range, rng1 as Range, rng2 as Range
Set rng = MyRange
set rng1 = rng.specialcells(xlFormulas)
set rng2 = rng.Specialcells(xlconstants)
if not rng1 is nothing and not rng2 is nothing then
set rng = Union(rng1,rng2)
elseif not rng1 is nothing then
set rng = rng1
elseif not rng2 is nothing then
set rng = rng2
Else
set rng = nothing
end if
msgbox rng.Address

--
Regards,
Tom Ogilvy

I'm trying to use SpecialCells to select all the non-blank cells in my
range.
If my approach is correct please tell me how to complete my code, or if
there's a better way...

'my approach is to:
set Blanks=MyRange.SpecialCells(xlCellTypeBlanks)
'and then
set MyRange=intersect ( MyRange , [not blanks] )

'but I can't work out how to get [not blanks]

Thanks in advance
Rob
 
You didn't call me verbose - that is my term. I don't like having to do it
that way either, but I have never figured out a better way. Someone else
may have a more exciting approach. <g>

--
Regards,
Tom Ogilvy

Rob said:
ok thanks very much, I'll do it as a seperate function. Sorry to have called
you verbose :-)

Tom Ogilvy said:
No, you can't union with nothing.

I didn't make it multiline because I like to be verbose - in fact I left out
two critical lines

Dim rng as Range, rng1 as Range, rng2 as Range
Set rng = MyRange
On Error Resume Next
set rng1 = rng.specialcells(xlFormulas)
set rng2 = rng.Specialcells(xlconstants)
On Error goto 0
if not rng1 is nothing and not rng2 is nothing then
set rng = Union(rng1,rng2)
elseif not rng1 is nothing then
set rng = rng1
elseif not rng2 is nothing then
set rng = rng2
Else
set rng = nothing
end if


--
Regards,
Tom Ogilvy

Rob said:
I'd really like this as a one liner, which would be posible if specialcells
didn't cause an error. Any ideas?

Union( MyRange.specialcells(xlFormulas) ,
Myrange.SpecialCells(xlconstants) )
:

Dim rng as Range, rng1 as Range, rng2 as Range
Set rng = MyRange
set rng1 = rng.specialcells(xlFormulas)
set rng2 = rng.Specialcells(xlconstants)
if not rng1 is nothing and not rng2 is nothing then
set rng = Union(rng1,rng2)
elseif not rng1 is nothing then
set rng = rng1
elseif not rng2 is nothing then
set rng = rng2
Else
set rng = nothing
end if
msgbox rng.Address

--
Regards,
Tom Ogilvy

I'm trying to use SpecialCells to select all the non-blank cells in my
range.
If my approach is correct please tell me how to complete my code,
or
if
there's a better way...

'my approach is to:
set Blanks=MyRange.SpecialCells(xlCellTypeBlanks)
'and then
set MyRange=intersect ( MyRange , [not blanks] )

'but I can't work out how to get [not blanks]

Thanks in advance
Rob
 

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

Back
Top