Reference material for If Not/Intersect/Nothing Then

julhs

Active Member
Joined
Dec 3, 2018
Messages
454
Office Version
  1. 2010
Platform
  1. Windows
Can someone point me in the right direction for a good source for this.

Many thanks
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
There isn't really a source for that specifically that I am aware of as it's just a combination of things. Intersect either returns a Range (where the provided ranges overlap) or Nothing (if there is no overlap. Generally in the kind of code I assume you are talking about, you don't want to do anything if there is no overlap, so you need to check that the result of Intersect is not Nothing. You could do that like this:

VBA Code:
If Intersect(range1, range2) Is Nothing then
 ' don't do anything
Else
  ' run your code here because there is an overlap
End If

but given that the first (True) part of the if usually doesn't involve any code, most people prefer to negate the test so you don't need an else:

VBA Code:
If Not Intersect(range1, range2) Is Nothing then
  ' run your code here because there is an overlap
End If
 
Upvote 0
Thanks Rory
I bumbled my way through a previous problem and came up with the solution below that achieved what I wanted:
VBA Code:
If Not Intersect(Target, Range("ChemicalSuppliersList_Condensed")) Is Nothing Then
LastRow = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
Call ReFormatRange
End If
'       Named Range "ChemicalSuppliersList_Condensed" = $A$8:$A$26
'       LastRow gave me last used row on the Sheet
To do something else I need to find the LastRow of NamedRange "ChemicalSuppliersList_Condensed" and NOT LastRow of the sheet.
If it makes any difference, this will all be part of a single Change Event procedure.
But thanks to utter brain fog I can’t figure out how to structure it!!
 
Upvote 0
Thanks Rory
I bumbled my way through a previous problem and came up with the solution below that achieved what I wanted:
VBA Code:
If Not Intersect(Target, Range("ChemicalSuppliersList_Condensed")) Is Nothing Then
LastRow = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
Call ReFormatRange
End If
'       Named Range "ChemicalSuppliersList_Condensed" = $A$8:$A$26
'       LastRow gave me last used row on the Sheet
To do something else I need to find the LastRow of NamedRange "ChemicalSuppliersList_Condensed" and NOT LastRow of the sheet.
If it makes any difference, this will all be part of a single Change Event procedure.
But thanks to utter brain fog I can’t figure out how to structure it!!
Replace your "LastRow =" with these, depending on what your need. And replace "TEST" with the name of your range of course.

VBA Code:
    namedRangeCount = Range("TEST").Rows.Count() 'The number of rows in a named range [INTEGER]
    LastRow = Range("TEST").Rows(namedRangeCount).Row 'The absolute number (relative to sheet)  of the last row in the named range [INTEGER]
    LastRowAsRange = Rows(LastRow).Cells 'The entire last row of the named range as a range [RANGE]
 
Upvote 0
Thank you both for the advice.
Rory; example you gave made me realise I could put multiple target ranges into one Intersect argument that all Call the same Sub.
Egburg; sorry I’ve not done you justice by not testing it yet, needed to finish off other things, when I’ve finished I will give feedback.
Many thanks
Julhs
 
Upvote 0

Forum statistics

Threads
1,221,572
Messages
6,160,570
Members
451,656
Latest member
SBulinski1975

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