How to apply loop and condition statement in visible cells?

dmadhup

Board Regular
Joined
Feb 21, 2018
Messages
146
Office Version
  1. 365
hello,

So far I have this code. but not working for visible cells. Any idea, please!

Code:
For Each j In Range("A2:FL" & lastRow).SpecialCells(xlCellTypeVisible)
    ii = j.Row
    If Rows(ii).EntireRow.Visible = True Then
        If Range("V") <> "" Or Range("V") <> vbNullString Then _
            If UCase(Range("V" & ii)) Like "*360-*" _
                Or UCase(Range("V" & ii)) Like "*AMG-*" _
                Or UCase(Range("V" & ii)) Like "*BNK-*" _
                Or UCase(Range("V" & ii)) Like "*BRU-*" _
                Or UCase(Range("V" & ii)) Like "*EUG-*" _
                Or UCase(Range("V" & ii)) Like "*FIN-*" _
                Or UCase(Range("V" & ii)) Like "*KLA-*" _
                Or UCase(Range("V" & ii)) Like "*MIA-*" _
                Or UCase(Range("V" & ii)) Like "*VIA-*" _
                Range("FK" & ii) = "EM"


            If UCase(Range("V" & ii)) Like "*G-*" Then _
                Range("FK" & ii) = "FRE"
    End If
Next j
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
You need to add the row references on this line
Code:
If Range("V") <> "" Or Range("V") <> vbNullString Then _
the same way you have in the rest of the code.
 
Upvote 0
Sorry, I have added like this, still no success

If Range("V" & ii) <> "" Or Range("V" & ii) <> vbNullString Then _
 
Upvote 0
Is this what you want?
Code:
For Each j In Range("v2:v" & 783).SpecialCells(xlCellTypeVisible)
   ii = j.Row
   If Range("V" & ii) <> "" Or Range("V" & ii) <> vbNullString Then
      If UCase(Range("V" & ii)) Like "*360-*" _
            Or UCase(Range("V" & ii)) Like "*AMG-*" _
            Or UCase(Range("V" & ii)) Like "*BNK-*" _
            Or UCase(Range("V" & ii)) Like "*BRU-*" _
            Or UCase(Range("V" & ii)) Like "*EUG-*" _
            Or UCase(Range("V" & ii)) Like "*FIN-*" _
            Or UCase(Range("V" & ii)) Like "*KLA-*" _
            Or UCase(Range("V" & ii)) Like "*MIA-*" _
            Or UCase(Range("V" & ii)) Like "*VIA-*" Then
         Range("FK" & ii) = "EM"
      ElseIf UCase(Range("V" & ii)) Like "*G-*" Then
            Range("FK" & ii) = "FRE"
      End If
   End If
Next j
 
Upvote 0
Fluff:
I am checking the word that contains AMG, BNK, etc. in column V. If found then write EM in column FK.
Similarly, If contains G-45464 or G-958585, etc then write FRE in column FK
 
Upvote 0
Yes, but does the modified code work?
 
Upvote 0
Nope.
It works if I break the code using two for loop. Don't know why...

For Each j In Range("v2:v" & 783).SpecialCells(xlCellTypeVisible)
ii = j.Row
If Range("V" & ii) <> "" Or Range("V" & ii) <> vbNullString Then
If UCase(Range("V" & ii)) Like "*360-*" _
Or UCase(Range("V" & ii)) Like "*AMG-*" _
Or UCase(Range("V" & ii)) Like "*BNK-*" _
Or UCase(Range("V" & ii)) Like "*BRU-*" _
Or UCase(Range("V" & ii)) Like "*EUG-*" _
Or UCase(Range("V" & ii)) Like "*FIN-*" _
Or UCase(Range("V" & ii)) Like "*KLA-*" _
Or UCase(Range("V" & ii)) Like "*MIA-*" _
Or UCase(Range("V" & ii)) Like "*VIA-*" Then
Range("FK" & ii) = "EM"
End If
Next j


For Each j In Range("v2:v" & 783).SpecialCells(xlCellTypeVisible)
ii = j.Row
If Range("V" & ii) <> "" Or Range("V" & ii) <> vbNullString Then
If UCase(Range("V" & ii)) Like "*G-*" Then _
Range("FK" & ii) = "FRE"


End If
Next j
 
Last edited:
Upvote 0
What should the expected outcome for cases like "*EUG-*" & "*AMG-*"
They fit both patterns
 
Upvote 0
Hi Fluff,

In that column, the data is like EUG-785463, EUG-3654, AMG-8745, AMG-1234.

For this reason, I just look for initial 3 letters. If matches then tell the program to do this....
 
Upvote 0
Should those values put "EM", or "FRE" in col FK, as they match both "*EUG-*" and "*G-*"
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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