Excess ElseIF statements

EdwardL95

New Member
Joined
Aug 17, 2021
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Please note I'm still relatively new to VBA and have limited knowledge.

Please see example of data below:

58697fe9bc066968a4f7999f9265d456.png


I'm trying to write code to determine what RESULTS would be based on the possible combinations between columns A-E, thus causes my issue of too many ELSEIF statements . Is there a way to do this more efficiently and not have to write out every single combination?

My conditions are:

- MS has be below 35 else it is considered a fail
- If any columns between A-E are between 0 to 30 then it would be considered positive for that specific column and thus transcribed into Results


Below is an example of my code just for these possible outcomes in the dataset above.

VBA Code:
For J = 2 To 8

    P = Cells(J, "A").Value
    M = Cells(J, "B").Value
    K = Cells(J, "C").Value
    R = Cells(J, "D").Value
    B = Cells(J, "E").Value
    Ms = Cells(J, "F").Value
    
    If _
        Ms > 38 Then
        Cells(J, "G").Value = "Fail"
        
    ElseIf _
        P < 35 And _
        M < 35 And _
        K < 35 And _
        R < 35 And _
        B < 35 Then _
        Cells(J, "G").Value = "P M K R B"
        
    ElseIf _
        M < 35 And _
        K < 35 And _
        R < 35 And _
        B < 35 Then _
        Cells(J, "G").Value = "M K R B"
        
    ElseIf _
        K < 35 And _
        R < 35 And _
        B < 35 Then _
        Cells(J, "G").Value = "K R B"
        
    ElseIf _
        R < 35 And _
        B < 35 Then _
        Cells(J, "G").Value = "R B"
        
    ElseIf _
        B < 35 Then _
        Cells(J, "G").Value = "B"

End If

Next J

Thanks,
Ed
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
don't use ELSEIF
use SELECT CASE statement. Easier to read:

Code:
select case true
case        Ms > 38
        Cells(J, "G").Value = "Fail"
       
case    P < 35 And _
        M < 35 And _
        K < 35 And _
        R < 35 And _
        B < 35

        Cells(J, "G").Value = "P M K R B"
end select
 
Upvote 0
This was my version I came up with similar to @ranman256's suggestion:

VBA Code:
    For J = 2 To 8
         P = Cells(J, "A").Value
         M = Cells(J, "B").Value
         K = Cells(J, "C").Value
         R = Cells(J, "D").Value
         B = Cells(J, "E").Value
        Ms = Cells(J, "F").Value
'
        Select Case True
            Case Ms > 38: Cells(J, "G").Value = "Fail"
'
            Case P < 35 And M < 35 And K < 35 And R < 35 And B < 35: Cells(J, "G").Value = "P M K R B"
'
            Case M < 35 And K < 35 And R < 35 And B < 35: Cells(J, "G").Value = "M K R B"
'
            Case K < 35 And R < 35 And B < 35: Cells(J, "G").Value = "K R B"
'
            Case R < 35 And B < 35: Cells(J, "G").Value = "R B"
'
            Case B < 35: Cells(J, "G").Value = "B"
'
            Case Else: Cells(J, "G").Value = "Negative"
        End Select
    Next J

Edit: Forgot the Negative test
 
Last edited:
Upvote 0
@ranman256 and @johnnyL thanks for the suggestions however my problem isn't so much writing 7 elseif or 7 cases. It's the issue of there being approx. 120 combinations with P, M,K, R & B.

etc. M K R B, M K R, MK ,M , M R, M R B, MKB.... and the list goes on with more combinations.

I do not want to brute force write every single combination be it elseif or select case and was wondering if there was a way around this.

The dataset I showed was only an example of what my data looks like but doesn't show all the possible outcomes.

Apologies if I wasn't descriptive enough.

Thanks, Ed
 
Upvote 0
How about:

VBA Code:
Sub test1()
Dim hdraddr As String, rowaddr As String, str1 As String, r As Long

    hdraddr = Range("A1:E1").Address
    For r = 2 To 8
        rowaddr = Cells(r, "A").Resize(, 5).Address
        str1 = Evaluate("TEXTJOIN("" "",1,IF((" & rowaddr & ">0)*(" & rowaddr & "<=30)," & hdraddr & ",""""))")
        If Len(str1) = 0 Then str1 = "Negative"
        If Cells(r, "F") > 30 Then str1 = "FAIL"
        Cells(r, "G") = str1
    Next r
    
End Sub
 
Upvote 0
Solution
Book1
ABCDEFG
1PMKRBMSResults
2343434343438P M K R B
3-3434343438M K R B
4--34343438K R B
5---343438R B
6----3438B
7-----40Fail
8-----38Negative
Sheet1


Hey there @Eric W, I can't test the TEXTJOIN because I don't have the proper excel to do so. :( But I think you need to correct a few values in your code.

<= 30 should be <= 34 Or just < 35

If Cells(r, "F") > 30 should be If Cells(r, "F") > 38
 
Last edited:
Upvote 0
<= 30 should be <= 34 Or just < 35

If Cells(r, "F") > 30 should be If Cells(r, "F") > 38

My conditions are:

- MS has be below 35 else it is considered a fail
- If any columns between A-E are between 0 to 30 then it would be considered positive for that specific column and thus transcribed into Results

Yes, it appears that I may have some wrong values, but it also seems there's some ambiguity in the requirements. I'll wait for the OP to chime in.

Also, you might not have TEXTJOIN, but I could replace that line with a short loop, which might even be better. I'll wait to see on that too.
 
Upvote 0
Ahh. I see the ambiguity now. The original code is not consistent with the original text. My apologies, if required.
 
Upvote 0
@Eric W and @johnnyL Thanks for this, didn't think to do it like that.

Apologies for the ambiguity haha, I can change the values myself.

Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

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