If not intersect code

erutherford

Active Member
Joined
Dec 19, 2016
Messages
449
On this sheet there are two command buttons and code for SelectionChange. Here is the code for the SelectionChange:
Code:
 If Not Intersect(Target, Range("H:H")) Is Nothing Then
    If Target.Cells.Value = " " Or IsEmpty(Target) Then Exit Sub
    If Target.Value = "4" Then Target.Offset(0, 1).Select
    End If

This is what I am trying to do. If you enter a number 4 in column H, you are automatically taken to Col. I same row. I'll add a Msgbox telling the user to enter the required data, but for now I just trying to do the above.

I have this same code working fine on other sheets!

thanks
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H:H")) Is Nothing Then
    If Target.Cells.Value = " " Or IsEmpty(Target) Then Exit Sub
    If Target.Value = "4" Then Target.Offset(0, 1).Select
    End If
End Sub
 
Upvote 0
It worked fine, thank you. The two codes are the same, I just didn't include the header and End Sub lines in my example. I cut and pasted your sample in and perfect results. Wonder if I had left something behind that I could not see???
 
Upvote 0
Glad I was able to help you. Come back here to Mr. Excel next time you need additional assistance.
It worked fine, thank you. The two codes are the same, I just didn't include the header and End Sub lines in my example. I cut and pasted your sample in and perfect results. Wonder if I had left something behind that I could not see???
 
Upvote 0
I would note that either of these...
Rich (BB code):
'Target.Cells.Value = " "
'Target.Value = "4"
...will error out (13: Type Mismatch) if multiple cells in the range returned by Intersect are changed (i.e. - select several cells and press the Del key to clear content). I would first IF test to ensure Target.Count = 1.

Hope that helps,

Mark
 
Upvote 0
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H:H")) Is Nothing Then
    If Target.Count > 1 Then Exit Sub
    If Target.Cells.Value = " " Or IsEmpty(Target) Then Exit Sub
    If Target.Value = "4" Then Target.Offset(0, 1).Select
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,686
Messages
6,126,202
Members
449,298
Latest member
Jest

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