VBA InStr() Function Nested Or/And Issue

nerdalert22

New Member
Joined
Apr 19, 2019
Messages
5
I can't seem to get the syntax right on my nested OR/ANDs section so I keep getting an error - please help! I'm sure there is a better way to do this than having all the InStr() OR InStr() but I'm not comfortable in VBA at all so I dont' know very much

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">'Loop search code
For i =2To LastRow

</code>
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">'Check if columns do not match
If Alpha.Range("G"& i)<> Alpha.Range("H"& i)Then

'Pull out mismatches if contain specific strings
SelectCaseTrue
</code>​
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">'Search for specific strings
Case((InStr(1, Alpha.Range("H"& i),"E08.3")>0)Or _
(InStr(1, Alpha.Range("H"& i),"E08.4")>0)Or _
(InStr(1, Alpha.Range("H"& i),"E08.5")>0)Or _
(InStr(1, Alpha.Range("H"& i),"E08.6")>0)Or _
(InStr(1, Alpha.Range("H"& i),"E08.8")>0)Or _
(InStr(1, Alpha.Range("H"& i),"E08.9")>0))Or _
(InStr(1, Alpha.Range("H"& i),"E09")>0)Or _
(InStr(1, Alpha.Range("H"& i),"E10")>0)Or _
(InStr(1, Alpha.Range("H"& i),"E11")>0)Or _
(InStr(1, Alpha.Range("H"& i),"E13.0")>0)Or _
(InStr(1, Alpha.Range("H"& i),"E13.1")>0)Or _
(InStr(1, Alpha.Range("H"& i),"E13.2")>0)Or _
(InStr(1, Alpha.Range("H"& i),"E13.3")>0))And _
((InStr(1, Alpha.Range("K"& i),"J0718")>0)Or _
(InStr(1, Alpha.Range("K"& i),"J2778")>0)Or _
(InStr(1, Alpha.Range("K"& i),"J9035")>0)Or _
(InStr(1, Alpha.Range("K"& i),"J7312")>0)Or _
(InStr(1, Alpha.Range("K"& i),"C9257")>0))

'look for UniqueID(colA) in TrackSheet to avoid duplicate copy
DuplicateCheck
= Application.Match(Alpha.Range("A"& i).Value,_
TrackSheet
.Columns(1),0)
If IsError(DuplicateCheck)Then
'If no match on UniqueID, copy values
TrackSheet
.Cells(Rows.Count,1).End(xlUp)._
Offset
(1,0).EntireRow.Value = Alpha.Rows(i).Value
EndIf

</code>​
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">EndSelect
EndIf

</code>​
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Next i</code>
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
This is what happens when you've been staring at your code for too long! extra "(" after the 6th InStr(). But if anyone knows of a better way to perform that function with so many possible strings, that would be very helpful! As, this isn't even the full list of strings I need to check for
 
Upvote 0
I might consider storing all the possible value you are checking for in a list on sheet somewhere, or in an array (and iterating through the array).
That would cut down your code considerably, and allow you to make changes to the list in the future without having to add/remove a bunch of lines of code (you would just need to edit the list or array).

Here is what an array option might look like: http://www.java2s.com/Code/VBA-Exce...Basics/Loopingthroughtheelementsinanarray.htm
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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