Code is executed even when certain criterias are not met

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
I have this code that adds 5 new rows after any row that contains Solmax or AGRU in column 11, also contain Liner in column 11, and contain Sanitary in column 15
the code is working fine when these criterias are met...the problem is it adds these 5 rows even when column 15 does not contain Sanitary.....

see file below, column 15 or column O, does not contain Sanitary
1672976381658.png


But 5 new rows got added anyways....
1672976254004.png


code below, any help is greatly appreciated

VBA Code:
Sub AddRowForSanLinerItems() 
    Set rng2 = Range("A1").CurrentRegion, i As Long
    lr8 = rng2.Cells(Rows.Count, "K").End(3).Row
   
    For i = lr8 To 2 Step -1     
        If rng2.Cells(i, 11) Like "*Solmax*" Or _
            rng2.Cells(i, 11) Like "*AGRU*" And _
            rng2.Cells(i, 15) Like "*Sanitary*" And _
            rng2.Cells(i, 11) Like "*Liner*" Then
                rng2.Cells(i, 4).Offset(1).EntireRow.Insert 
                rng2.Cells(i, 3).Offset(1).Resize(1, 9).Value = _
                Array("1", "F03957", "No", "", "", "0", "Purchased", "0", "LINER") 
                rng2.Cells(i, 1).Offset(1).Resize(1, 2).Value = rng2.Cells(i, 1).Resize(1, 2).Value 
                rng2.Cells(i, 4).Offset(2).EntireRow.Insert  
                rng2.Cells(i, 3).Offset(2).Resize(1, 9).Value = _
                Array("1", "F03962", "No", "", "", "0", "Purchased", "0", "LINER FIELD SEAM")
                rng2.Cells(i, 1).Offset(2).Resize(1, 2).Value = rng2.Cells(i, 1).Resize(1, 2).Value
                rng2.Cells(i, 4).Offset(3).EntireRow.Insert 
                rng2.Cells(i, 3).Offset(3).Resize(1, 9).Value = _
                Array("1", "F03903", "No", "", "", "0", "Purchased", "0", "LINER CONE")
                rng2.Cells(i, 1).Offset(3).Resize(1, 2).Value = rng2.Cells(i, 1).Resize(1, 2).Value
                rng2.Cells(i, 4).Offset(4).EntireRow.Insert  
                rng2.Cells(i, 3).Offset(4).Resize(1, 9).Value = _
                Array("1", "F03915", "No", "", "", "0", "Purchased", "0", "LINER TURNBACK")
                rng2.Cells(i, 1).Offset(4).Resize(1, 2).Value = rng2.Cells(i, 1).Resize(1, 2).Value
                rng2.Cells(i, 4).Offset(5).EntireRow.Insert 
                rng2.Cells(i, 3).Offset(5).Resize(1, 9).Value = _
                Array("1", "F03916", "No", "", "", "0", "Purchased", "0", "LINER INSTALLATION FOR BRICKWORK")
                rng2.Cells(i, 1).Offset(5).Resize(1, 2).Value = rng2.Cells(i, 1).Resize(1, 2).Value
        End If
    Next i
End Sub
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
What about a smallish set of suitable sample data that we can copy to test with (XL2BB)?
Just give us columns K:O
 
Upvote 0
Try adding some brackets

Rich (BB code):
        If (rng2.Cells(i, 11) Like "*Solmax*" Or _
            rng2.Cells(i, 11) Like "*AGRU*") And _
            rng2.Cells(i, 15) Like "*Sanitary*" And _
            rng2.Cells(i, 11) Like "*Liner*" Then
 
Upvote 0
Solution
May be:
VBA Code:
If rng2.Cells(i, 11) Like "*Solmax*" Or _
            rng2.Cells(i, 11) Like "*AGRU*" And _
            rng2.Cells(i, 15) Like "*Sanitary*" And _
            rng2.Cells(i, 11) Like "*Liner*" Then
The condition statement is mixed of OR and AND. try to put 1st OR inside a pair of brackets
VBA Code:
If (rng2.Cells(i, 11) Like "*Solmax*" Or  rng2.Cells(i, 11) Like "*AGRU*") And _
            rng2.Cells(i, 15) Like "*Sanitary*" And _
            rng2.Cells(i, 11) Like "*Liner*" Then
BTW, I think the origin code should be upgraded. Try using array, instead of working with sheet range.
 
Upvote 0
May be:
VBA Code:
If rng2.Cells(i, 11) Like "*Solmax*" Or _
            rng2.Cells(i, 11) Like "*AGRU*" And _
            rng2.Cells(i, 15) Like "*Sanitary*" And _
            rng2.Cells(i, 11) Like "*Liner*" Then
The condition statement is mixed of OR and AND. try to put 1st OR inside a pair of brackets
VBA Code:
If (rng2.Cells(i, 11) Like "*Solmax*" Or  rng2.Cells(i, 11) Like "*AGRU*") And _
            rng2.Cells(i, 15) Like "*Sanitary*" And _
            rng2.Cells(i, 11) Like "*Liner*" Then
BTW, I think the origin code should be upgraded. Try using array, instead of working with sheet range.
Thank you Bebo, I am writing this down, I will need to start using more array
This works great
 
Upvote 0
Try adding some brackets

Rich (BB code):
        If (rng2.Cells(i, 11) Like "*Solmax*" Or _
            rng2.Cells(i, 11) Like "*AGRU*") And _
            rng2.Cells(i, 15) Like "*Sanitary*" And _
            rng2.Cells(i, 11) Like "*Liner*" Then
Alex, thank you, this indeed fixed the issue.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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