使用链接到带有计数器的 FOR 循环的多个 IF 语句设置一个变量

Set up a variable with multiple IF statements linked to a FOR loop with a counter

我有 3 个 sheet。在 sheet "Manager" 中,有 7 个条件下拉列表:H5H7H9H11H13 , H15, H17。一旦条件被 selected 并且用户点击按钮 "COPY",宏就会在 sheet "Data" 列 A:G 中搜索匹配 selected 标准。然后它复制匹配行的范围 P:W 并将其粘贴到 sheet "Quote" 从第 11 行开始。重要的是要注意用户没有 select 条件对于任何下拉列表,该条件将被忽略(请参阅代码中的 VbNullString

到目前为止,宏在公司下拉列表 (H5) 的多个条件 selection 和其他条件 ([=12=) 的单个条件 selection 下运行良好], H9, H11, H13, H15, H17).

Sub Quote()

Dim Source As Worksheet
Dim Target As Worksheet
Dim Manager As Worksheet
Dim Multiple () As String 'Here
Dim InfoA As String
Dim InfoB As String
Dim InfoC As String
Dim ProductType As String
Dim SalesStatus As String
Dim finalrow As Integer
Dim counter As Integer
Dim I As Integer

Set Source = Worksheets("Data")
Set Target = Worksheets("Quote")
Set Manager = Worksheets("Manager")
If Worksheets("Manager").Range("H5").Value <> vbNullString Then 'Here
Multiple = Split(Worksheets("Manager").Range("H5").Value, ",") 'Here
   If Worksheets("Manager").Range("H13").Value <> vbNullString Then 'Modified
   Multiple = Split(Worksheets("Manager").Range("H13").Value, ",") 'Here

      Else 'Here
      Multiple = Split("", "") 'Here
   End If 'Here
End If 'Here
InfoA = Worksheets("Manager").Range("H7").Value
InfoB = Worksheets("Manager").Range("H9").Value
InfoC = Worksheets("Manager").Range("H11").Value
ProductType = Worksheets("Manager").Range("H15").Value
SalesStatus = Worksheets("Manager").Range("H17").Value
finalrow = Source.Cells(Rows.Count, 1).End(xlUp).Row


For counter = 0 To UBound(Multiple) 'Here
lookupMult = Trim(Multiple(counter)) 'Here

For I = 2 To finalrow

    thisComp = Source.Cells(I, 1)
    thisInfA = Source.Cells(I, 2)
    thisInfB = Source.Cells(I, 3)
    thisInfC = Source.Cells(I, 4)
    thisProd = Source.Cells(I, 5)
    thisType = Source.Cells(I, 6)
    thisSale = Source.Cells(I, 7)

    If (thisComp = lookupMult Or lookupMult = vbNullString) Then 'Here
        If (thisInfA = InfoA Or InfoA = vbNullString) Then
            If (thisInfB = InfoB Or InfoB = vbNullString) Then
                If (thisInfC = InfoC Or InfoC = vbNullString) Then
                    If (thisProd = lookupMult Or lookupMult = vbNullString) Then 'Here
                        If (thisType = ProductType Or ProductType = vbNullString) Then
                            If (thisSale = SalesStatus Or SalesStatus = vbNullString) Then
                    Source.Range(Source.Cells(I, 16), Source.Cells(I, 23)).Copy Target.Range("A200").End(xlUp).Offset(1, 0).Resize(1, 8)
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End If
Next I
Next counter

End Sub

除了 H5 的多个条件 selection 之外,我还需要为产品 (H13) 启用它。为此,我尝试使用更详细的 IF 语句修改变量 Company。图中,sheet "Quote" 是我应该得到的结果。但实际上没有任何内容是复制粘贴的,我无法弄清楚我做错了什么。我在此处添加了一些注释,以显示我修改了代码的哪一部分。提前感谢您的指导。

我找到了解决问题的方法。它不是灵丹妙药,但至少可以正常工作。之后,如果有人知道在 SQL 查询和结构化表之外优化代码的方法,请随时分享,我会尝试。请注意,我相信 SQL 查询可能是一个更好的选择,但这意味着我必须重新编写几乎所有代码并使用我(还)不知道的方法。待会儿再研究,以后更新。

问题是单词 "counter" 可能是一个保留变量。因此,我无权在共享类似功能的循环中添加另一个 FOR。由于我按字母更改了 "counter" 变量,我现在可以为其他下拉列表进行多个条件选择。在下面的示例中,为了保持清晰,我只是为 H5 和 H13 制作了它。

Sub Quote()

Dim Source As Worksheet
Dim Target As Worksheet
Dim Manager As Worksheet
Dim Company () As String
Dim InfoA As String
Dim InfoB As String
Dim InfoC As String
Dim Product () As String
Dim ProductType As String
Dim SalesStatus As String
Dim finalrow As Integer
Dim I As Integer
Dim J As Integer
Dim K As Integer

Set Source = Worksheets("Data")
Set Target = Worksheets("Quote")
Set Manager = Worksheets("Manager")
If Worksheets("Manager").Range("H5").Value <> vbNullString Then 
Company= Split(Worksheets("Manager").Range("H5").Value, ",") 
Else 
Company = Split("", "") 
End If 
InfoA = Worksheets("Manager").Range("H7").Value
InfoB = Worksheets("Manager").Range("H9").Value
InfoC = Worksheets("Manager").Range("H11").Value
If Worksheets("Manager").Range("H13").Value <> vbNullString Then 
Product = Split(Worksheets("Manager").Range("H13").Value, ",") 
Else 
Product = Split("", "") 
End If 
ProductType = Worksheets("Manager").Range("H15").Value
SalesStatus = Worksheets("Manager").Range("H17").Value
finalrow = Source.Cells(Rows.Count, 1).End(xlUp).Row


For K = 0 To UBound(Company) 
lookupComp = Trim(Company(K))

For J = 0 To UBound(Product) 
lookupProd = Trim(Product(J))  

For I = 2 To finalrow

thisComp = Source.Cells(I, 1)
thisInfA = Source.Cells(I, 2)
thisInfB = Source.Cells(I, 3)
thisInfC = Source.Cells(I, 4)
thisProd = Source.Cells(I, 5)
thisType = Source.Cells(I, 6)
thisSale = Source.Cells(I, 7)

If (thisComp = lookupComp Or lookupComp = vbNullString) Then
    If (thisInfA = InfoA Or InfoA = vbNullString) Then
        If (thisInfB = InfoB Or InfoB = vbNullString) Then
            If (thisInfC = InfoC Or InfoC = vbNullString) Then
                If (thisProd = lookupProd Or lookupProd = vbNullString) Then
                    If (thisType = ProductType Or ProductType = vbNullString) Then
                        If (thisSale = SalesStatus Or SalesStatus = vbNullString) Then
                Source.Range(Source.Cells(I, 16), Source.Cells(I, 23)).Copy Target.Range("A200").End(xlUp).Offset(1, 0).Resize(1, 8)
                        End If
                    End If
                End If
            End If
        End If
    End If
End If
Next I
Next J
Next K

End Sub