"AND" parameter for CASE STATEMENT??? WHEN, AND, THEN

Sphinx404

Board Regular
Joined
May 2, 2015
Messages
186
Office Version
  1. 365
Platform
  1. Windows
Code:
Sub Request2()


'THIS SUB: finds NONHAZ, UN2911, or UN3316 and if found populates No Action, No Error, or Compliant in columns AX:AZ


ActiveWorkbook.Sheets("HazShipper").Select
Application.ScreenUpdating = False
Application.DisplayAlerts = False


Dim mycell As Range
Dim mycell2 As Range
Dim myLastRow As Long
Dim myworksheet As Worksheet


Set myworksheet = Worksheets("HazShipper")
myLastRow = myworksheet.Cells(myworksheet.Rows.Count, "A").End(xlUp).row


For Each mycell In Range("C2", Range("C" & Rows.Count).End(xlUp))


Select Case True
    Case UCase(mycell.Value) Like "*NONHAZ*" Or mycell.Value Like "*UN2911*" Or mycell.Value Like "*UN3316*" [B][COLOR=#ff0000]and LEN(U2)=8 and W2 = "TRUE"[/COLOR][/B]
            mycell.Offset(, 47).Value = "No Action"
            mycell.Offset(, 48).Value = "No Error"
            mycell.Offset(, 49).Value = "Compliant"
                                    
End Select


Next mycell


End Sub

The code works fine. But when trying to add-in the highlighted RED parts I'm confusing myself. Do I need to create separate variables for LEN (U2) and the W2 values?

I don't know if there is a way to add "AND" to a CASE statement. I've read about using WHEN and AND which would go something like:
CASE WHEN var1 = "TRUE" AND var2 = "UN2911" THEN

but the article was referring to SQL
 
if column 41 is TRUE, then column 42 must have a value.

The entire macro is looking at certain cells in each row.... if all of these particular cells meet a certain criteria, then it will populate cells in that particular row with specific text.

i.e. columns 1, 4, 15, 17, 28 = true then this row is compliant.
 
Last edited:
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
if column 41 is TRUE, then column 42 must have a value.
When you say "it must have a value", are you saying:
1. To also check to see if it has a value?
- or -
2. To populate it with some value (if so, what)?

In the first case, that would just be another check, like all the ones you have done so far, i.e.
Code:
If Cells(r, 42) <> "" Then
or
Code:
If Len(Cells(r, 42)) > 0 Then

The first one literally means that the entry is "not nothing".
The second one literally means that the length of the entry is "greater than zero".
They both should do what you want, so pick whichever one you like better.

If it is the second case, then just populate the cell with whatever you want, i.e.
Code:
Cells(r, 42) = "some value I want to put in this cell"

I am guessing you are probably talking about the first here.
 
Upvote 0
My apologies for not being more specific. You're right, it is the first one. Added it in. Now I just have to figure out why it's throwing the "Loop without Do" error at me, and I'll be ready to roll. Thanks again!
 
Upvote 0
You are mixing up your loop types. There are FOR / NEXT loops, and DO / LOOP loops, but there are no FOR / LOOP loops!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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