VBA - Trying to find my error

nick1408

Board Regular
Joined
Jan 18, 2010
Messages
82
I have the following code:

Code:
Debug.Print InStr(1, ActiveCell.Offset(0, 14).Value, "1", vbTextCompare) = 0
Debug.Print InStr(1, ActiveCell.Offset(0, 14).Value, "2", vbTextCompare) = 0
Debug.Print InStr(1, ActiveCell.Offset(0, 2).Value, "3", vbTextCompare) = 0
Debug.Print InStr(1, ActiveCell.Offset(0, 2).Value, "4", vbTextCompare) = 0
Debug.Print InStr(1, ActiveCell.Offset(0, 2).Value, "5", vbTextCompare) = 0
Debug.Print InStr(1, ActiveCell.Offset(0, 2).Value, "6", vbTextCompare) = 0
Debug.Print InStr(1, ActiveCell.Offset(0, 2).Value, "7", vbTextCompare) = 0
Debug.Print InStr(1, ActiveCell.Offset(0, 2).Value, "8", vbTextCompare) = 0
Debug.Print InStr(1, ActiveCell.Offset(0, 5).Value, 999, vbTextCompare) = 0

Debug.Print InStr(1, ActiveCell.Offset(0, 14).Value, "1", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 14).Value, "2", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "3", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "4", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "5", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "6", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "7", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "8", vbTextCompare) = 0 _
And InStr(1, ActiveCell.Offset(0, 5).Value, 999, vbTextCompare) = 0

Essentially this is an IF statement that I am trying to get to work but I am debugging to see where I am failing. What I want to work out is if all lines are TRUE except the last line why does the bottom statement come out as true? I would have expected a FALSE return if activecell.offset(0,5).value does not equal 999
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Rich (BB code):
Debug.Print InStr(1, ActiveCell.Offset(0, 14).Value, "1", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 14).Value, "2", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "3", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "4", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "5", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "6", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "7", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "8", vbTextCompare) = 0 _
And InStr(1, ActiveCell.Offset(0, 5).Value, 999, vbTextCompare) = 0

Essentially this is an IF statement that I am trying to get to work but I am debugging to see where I am failing. What I want to work out is if all lines are TRUE except the last line why does the bottom statement come out as true? I would have expected a FALSE return if activecell.offset(0,5).value does not equal 999
The And operator has precedence over the Or operator, so it "binds" with the next to last line to produce effectively what this code shows below (note the red parentheses)...
Rich (BB code):
Debug.Print InStr(1, ActiveCell.Offset(0, 14).Value, "1", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 14).Value, "2", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "3", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "4", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "5", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "6", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "7", vbTextCompare) = 0 _
Or (InStr(1, ActiveCell.Offset(0, 2).Value, "8", vbTextCompare) = 0 _
And InStr(1, ActiveCell.Offset(0, 5).Value, 999, vbTextCompare) = 0
what you need is this which forces the last line to stand alone against all of the Or'ed line of code (again, note the red parentheses)...
Rich (BB code):
Debug.Print (InStr(1, ActiveCell.Offset(0, 14).Value, "1", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 14).Value, "2", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "3", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "4", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "5", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "6", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "7", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "8", vbTextCompare) = 0) _
And InStr(1, ActiveCell.Offset(0, 5).Value, 999, vbTextCompare) = 0
 
Last edited:
Upvote 0
So, would a better structure be:

Code:
Debug.Print InStr(1, ActiveCell.Offset(0, 5).Value, 999, vbTextCompare) <> 0 _
And (InStr(1, ActiveCell.Offset(0, 14).Value, "1", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 14).Value, "2", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "3", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "4", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "5", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "6", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "7", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "8", vbTextCompare) = 0)
 
Last edited:
Upvote 0
So, would a better structure be:

Code:
Debug.Print InStr(1, ActiveCell.Offset(0, 5).Value, 999, vbTextCompare) <> 0 _
And (InStr(1, ActiveCell.Offset(0, 14).Value, "1", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 14).Value, "2", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "3", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "4", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "5", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "6", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "7", vbTextCompare) = 0 _
Or InStr(1, ActiveCell.Offset(0, 2).Value, "8", vbTextCompare) = 0)
It makes no difference. If you think you can visualize the relationship more easily, though, then go with it.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

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