简体   繁体   中英

VBA code for multiple if statements within an Array

I am trying to split the array in column B which is separated by commas and then look for each item and if the condition is satisfied then publish something in column C.

I have lot of scenarios with combination of both AND/OR conditions.
At the end if none of scenarios is satisfied then in column "C" it should be "not defined".

Dim Cl As Range
Dim Dic As Object
Dim Sp As Variant
Dim i As Long
  
Set Dic = CreateObject("Scripting.dictionary")
With Sheets("Analysis")
    For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
        Sp = Split(Cl.Offset(, 1).Value, ",")
        Select Case Cl.Offset(, 1).Value
            Case Is = " "
                C1.Offset(, 2).Value = " "
            Case Is = "Production"
                C1.Offset(, 2).Value = "Prod"
            Case Is = "Production" And "Development" Or "Training"
                C1.Offset(, 2).Value = "Dev/Prod"
        End Select
    Next Cl
End With
End Sub

Sample data where column A has Id and column B has category.

ID CATEGORY
131 Production
124 Production, Development, Staging, Test, Training, UserAcceptanceTest
283 Development, Test
1138 empty.

I am looking for below result in column "C" for the below mentioned scenarios.
If Category column is as below then Column "C" values should be the one after "-".

  1. empty - No
  2. Development - Dev
  3. Production - Prod
  4. Test - Test
  5. Staging - Staging
  6. Training - Training
  7. UserAcceptanceTest - UAT
  8. Development AND Test AND Production OR any other category - All
  9. Development AND Test OR any other category (Except Production) - Dev/Test
  10. Test AND Production OR any other category (Except Development) - Dev/Test
  11. Development AND any other category (Except Production and Test ) - Dev
  12. Production AND any other category (Except Development and Test ) - Prod
  13. Test AND any other category (Except Development and Production) - Test
  14. any other scenario - Not Defined

Using the function IsInArray from This Answer :

I filled it with some example cases to give you the idea how to do it.

Sub Category()

For Each cl In Range("A2:A" & Range("A2").End(xlDown).Row)

    Sp = Split(tempString , ", ")

    Select Case True    
        Case UBound(Sp) = -1
            cl.Offset(0, 2).Value = "No"
        Case UBound(Sp) = 0 And Sp(0) = " "
            cl.Offset(0, 2).Value = "No"
        Case UBound(Sp) = 0 And Sp(0) = "Development"
            cl.Offset(0, 2).Value = "Dev"
        Case IsInArray("Development", Sp) And IsInArray("Test", Sp) And IsInArray("Production", Sp)
            cl.Offset(0, 2).Value = "All"
        Case IsInArray("Development", Sp) And Not IsInArray("Production", Sp) And Not IsInArray("Test", Sp)
            cl.Offset(0, 2).Value = "Dev"
        Case Else
            cl.Offset(0, 2).Value = "Not Defined"
    End Select

Next cl
End Sub


Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
  IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM