我在代码中使用了嵌套的Case。有两个变量要检查,“ src”和“ dis”
第一种情况是检查src是否在列表中
src = Cells(ActiveCell.Row, "F").Value
dis = Cells(ActiveCell.Row, "G").Value
Select Case src
'Attribution Model 1
Case src Like "ARC", "BAC", "ICP", "IPRT", "JGRT", "KMG", "NAD", "NQS", "OMRT", "OSG*", "RCH", "ROPJG", "RTSUP", "SUP", "TIN*", "TLA*", "TRN", "WPR*"
Select Case dis
Case dis Like "", "ARC*", "BAC*", "ICP*", "IPRT*", "JGRT*", "KMG*", "NAD*", "NQS*", "OMRT*", "OSG*", "RCH*", "ROPJG*", "RTSUP*", "SUP*", "TIN*", "TLA*", "TRN*", "WPR*"
Cells(ActiveCell.Row, "AE").Value = "Y"
Case Else
'Attribution Model 2
Cells(ActiveCell.Row, "AE").Value = "N"
End Select
'Attribution Model 3
Case src Like "WEB*"
If Cells(ActiveCell.Row, "AD").Value > 0 Then
Select Case dis
Case dis Like "", "ARC*", "BAC*", "ICP*", "IPRT*", "JGRT*", "KMG*", "NAD*", "NQS*", "OMRT*", "OSG*", "RCH*", "ROPJG*", "RTSUP*", "SUP*", "TIN*", "TLA*", "TRN*", "WPR*"
Cells(ActiveCell.Row, "AE").Value = "Y"
Case Else
'Attribution Model 4
Cells(ActiveCell.Row, "AE").Value = "N"
End Select
Else '<--- If URL = N
Select Case dis
Case dis Like "ARC*", "BAC*", "ICP*", "IPRT*", "JGRT*", "KMG*", "NAD*", "NQS*", "OMRT*", "OSG*", "RCH*", "ROPJG*", "RTSUP*", "SUP*", "TIN*", "TLA*", "TRN*", "WPR*"
'Attribution Model 6
Cells(ActiveCell.Row, "AE").Value = "Y"
Case Else
'Attribution Model 5
Cells(ActiveCell.Row, "AE").Value = "N"
End Select
End If
Case src Like "ARC", "BAC", "ICP", "IPRT", "JGRT", "KMG", "NAD", "NQS", "OMRT", "OSG*", "RCH", "ROPJG", "RTSUP", "SUP", "TIN*", "TLA*", "TRN", "WPR*"
' This is blank as ignored
Case Else
Select Case dis
Case dis Like "ARC*", "BAC*", "ICP*", "IPRT*", "JGRT*", "KMG*", "NAD*", "NQS*", "OMRT*", "OSG*", "RCH*", "ROPJG*", "RTSUP*", "SUP*", "TIN*", "TLA*", "TRN*", "WPR*"
'Attribution Model 7
Cells(ActiveCell.Row, "AE").Value = "Y"
Case Else
End Select
End Select
ActiveCell.Offset(1, 0).Activate
Loop
我尝试更改所有空白值,其中选中“ dis”以表示某些内容,例如“ DONK”,然后即使我有“ Case dis like” DONK”,“ ARC”等,它仍会跳过结果1并想写结果2。
src中总是存在某些内容,因此应为“如果src = List,而dis = List或空白,则结果1,否则结果2”,但是,它似乎无视第二种情况的结果,并且始终给了我“ Case Else”结果。
答案 0 :(得分:2)
您必须重新编写SELECT CASE。 (src
位完全不需要LIKE,因为您正在检查确切的值。)
Sub x()
Dim src, dis
src = "ARC"
dis = "ARCH"
Select Case src
Case "ARC", "BAC", "TRN"
Select Case True
Case dis Like "DONK", dis Like "ARC*", dis Like "BAC*", dis Like "TRN*"
MsgBox "Result 1"
Case Else
MsgBox "Result 2"
End Select
End Select
End Sub
答案 1 :(得分:1)
在我看来,您可以从这样的帮助程序功能中受益:
Function EqualsOrLikes(valToCheck As String, equals As Variant, likes As Variant) As Boolean
Dim val As Variant
Select Case True
Case Not IsError(Application.Match(valToCheck, equals, 0))
EqualsOrLikes = True
Case Else
For Each val In likes
If valToCheck Like val Then
EqualsOrLikes = True
Exit For
End If
Next
End Select
End Function
要饲料使用:
src
或dis
代表valToCheck
equals
的完整匹配值数组
likes
例如:
Sub x()
Dim equals As Variant, likes As Variant
equals = Array("ARC", "BAC", "ICP", "IPRT", "JGRT", "KMG", "NAD", "NQS", "OMRT", "RCH", "ROPJG", "RTSUP", "SUP", "TRN")
likes = Array("OSG*", "TIN*", "TLA*", "WPR*")
Dim src As String, dis As String
src = Cells(ActiveCell.Row, "F").Value
dis = Cells(ActiveCell.Row, "G").Value
Dim okDisEqualsOrLikes As Boolean
okDisEqualsOrLikes = EqualsOrLikes(dis, equals, likes) ' this you can evaluate once for all subsequent checks
Select Case True
Case EqualsOrLikes(src, equals, likes)
Select Case True
Case dis = vbNullString Or okDisEqualsOrLikes
Cells(ActiveCell.Row, "AE").Value = "Y"
Case Else
'Attribution Model 2
Cells(ActiveCell.Row, "AE").Value = "N"
End Select
Case src Like "WEB*"
If Cells(ActiveCell.Row, "AD").Value > 0 Then
Select Case True
Case dis = vbNullString Or okDisEqualsOrLikes
Cells(ActiveCell.Row, "AE").Value = "Y"
Case Else
'Attribution Model 4
Cells(ActiveCell.Row, "AE").Value = "N"
End Select
Else '<--- If URL = N
Select Case True
Case okDisEqualsOrLikes
'Attribution Model 6
Cells(ActiveCell.Row, "AE").Value = "Y"
Case Else
'Attribution Model 5
Cells(ActiveCell.Row, "AE").Value = "N"
End Select
End If
Case Else
Select Case True
Case okDisEqualsOrLikes
'Attribution Model 7
Cells(ActiveCell.Row, "AE").Value = "Y"
End Select
End Select
End Sub
一个更简洁的版本(尽管不那么优雅,至少对我而言)如下:
Sub x()
Dim equals As Variant, likes As Variant
equals = Array("ARC", "BAC", "ICP", "IPRT", "JGRT", "KMG", "NAD", "NQS", "OMRT", "RCH", "ROPJG", "RTSUP", "SUP", "TRN")
likes = Array("OSG*", "TIN*", "TLA*", "WPR*")
Dim src As String, dis As String
src = Cells(ActiveCell.row, "F").value
dis = Cells(ActiveCell.row, "G").value
Dim okDisEqualsOrLikes As Boolean
okDisEqualsOrLikes = EqualsOrLikes(dis, equals, likes) ' this you can evaluate once for all subsequent checks
Select Case True
Case EqualsOrLikes(src, equals, likes)
Cells(ActiveCell.row, "AE").value = "N"
If dis = vbNullString Or okDisEqualsOrLikes Then Cells(ActiveCell.row, "AE").value = "Y"
Case src Like "WEB*"
Cells(ActiveCell.row, "AE").value = "N"
If Cells(ActiveCell.row, "AD").value > 0 Then
If dis = vbNullString Or okDisEqualsOrLikes Then Cells(ActiveCell.row, "AE").value = "Y"
Else '<--- If URL = N
If okDisEqualsOrLikes Then Cells(ActiveCell.row, "AE").value = "Y"
End If
Case Else
If okDisEqualsOrLikes Then Cells(ActiveCell.row, "AE").value = "Y"
End Select
End Sub