问题
I have 3 sheets. In the sheet "Manager", there are 7 dropdown lists for criteria: H5
, H7
, H9
, H11
, H13
, H15
, H17
. Once the criteria are selected and the user clicks on the button "COPY", the macro searches in the sheet "Data" columns A:G
the rows matching the selected criteria. Then it copies the range P:W
for the matching rows and pastes it in sheet "Quote" starting from row 11. It is important to note when the user does not select a criterion for any of the dropdown list, then that criterion is just ignored (see VbNullString
in the code)
By now, the macro runs fine with multiple criteria selection for the Company dropdown list (H5
) and single criterion selection for the others (H7
, 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
In addition to the multiple criteria selection for H5
, I need also to enable it for the Product (H13
). To do so, I tried to modify the variable Company using a more elaborated IF statement. In the picture, the sheet "Quote" is the result I should get. But in fact nothing is copy-pasted and I cannot figure out what I'm doing wrong. I added some comments 'Here to show what part of the code I modified. By advance thanks for any guidance.
回答1:
I found a way to solve my issue. It is not a silver bullet, but at least it works as it should. After, if anyone knows some way to optimize the code, outside of SQL queries and structured tables, feel free to share and I will try. Note I believe SQL queries is probably a better option, but it means I have to rework almost all my code and use methods I do not know (yet). I will study it later for a future update.
The problem is the word "counter" might be a reserved variable. So, I was not authorized to add another FOR in my loop sharing similar features. Since I changed the "counter" variable by letters, I'm now able to do multiple criteria selection for other dropdown lists. In the example below, I just made it for H5 and H13 in order to keep it clear.
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
来源:https://stackoverflow.com/questions/55516736/set-up-a-variable-with-multiple-if-statements-linked-to-a-for-loop-with-a-counte