简体   繁体   English

高级筛选器不适用于嵌套的For Next循环的第二次迭代VBA Excel

[英]Advanced Filter not working on second iteration of nested For Next loop vba excel

I am writing a macro that will take a large data sheet and filter it onto another sheet, with the first criteria being based on a variable (Degree Level) determined by the iteration of the for next loop based on m, and the second criteria based on a variable (Major) determined by the iteration of the nested for next loop based on i. 我正在编写一个宏,该宏将使用较大的数据表并将其过滤到另一张表上,第一个条件基于变量(度数级别),该变量由基于m的for next循环的迭代确定,第二个条件基于在由基于i的下一个循环的嵌套迭代确定的变量(主要)上。

The filter works on the first iteration of m but not on the second (there are only three iterations). 过滤器在m的第一个迭代上起作用,但在第二个迭代上不起作用(只有三个迭代)。 Not sure what would cause the bug, but it is on the line below 'Populate filtered data sheet...' Below is my code. 不确定会导致该错误的原因,但是它位于“填充过滤后的数据表...”下面的行上,这是我的代码。 Thanks in advance! 提前致谢!

Public SelColCode as string 
Sub FilterGrads()
Dim DegreeLevel as string

SelColCode = "H"

For m = 1 To 3
If m = 1 Then
    DegreeLevel = "B"
    Number_Of_Majors = Sheets("MajorList").Range("AC1").Value '16 in this case
ElseIf m = 2 Then
    DegreeLevel = "M"
    Number_Of_Majors = Sheets("MajorList").Range("AE1").Value '12 in this case
ElseIf m = 3 Then
    DegreeLevel = "D"
    Number_Of_Majors = Sheets("MajorList").Range("AE1").Value '8 in this case
End If

'Look through all the majors and fill in each row one at a time on the report
For i = 5 To 4 + Number_Of_Majors * 2 Step 2
'Select the Report Tab and select the major
    Sheets(SelColCode & " " & DegreeLevel & " Report").Select
    Major = Range("B" & i)

'Unhide the advance filter tab and then input the Degree level and major from above
ThisWorkbook.Worksheets("Advance Filter").Visible = True
ThisWorkbook.Worksheets("Advance Filter").Select
Range("A6:AA15").ClearContents
Range("E6").Value = DegreeLevel & ".*"
Range("F6").Value = Major

'Check if the "Filter Data" Tab exists and delete it then add a blank sheet with the same name.
Application.DisplayAlerts = False
On Error Resume Next
ThisWorkbook.Sheets("Filter Data").Delete
On Error GoTo 0
Application.DisplayAlerts = True
Sheets.Add.Name = "Filter Data"
Sheets("Filter Data").Select
Sheets("Filter Data").Move After:=Sheets("Merged Data")

'Populate filtered data sheet using the advance filter function
Sheets("Merged Data").Range(Data_Range).AdvancedFilter Action:=xlFilterCopy, _
    CriteriaRange:=Sheets("Advance Filter").Range("A5:AB6"), CopyToRange:=Range("A1"), Unique:=True

'Length of filtered data minus one to not include the variable titles is the number of graduates for that major
Grads = ActiveSheet.Cells(Rows.count, "A").End(xlUp).Row - 1

'do some stuff, then loop

Next 'i, major
Next 'm, degreelevel
End Sub

So I'm not sure why this worked, but I made a filtered data tab for each degree level and then a separate If statement for each degree level option. 因此,我不确定为什么这样做,但是我为每个学位级别制作了一个过滤后的数据标签,然后为每个学位级别选项制作了单独的If语句。 The following is within each If level. 以下是每个If级别内的内容。 Thanks for trying @everyonewhohelped 感谢您尝试@everyonewhohelped

'Check if the "Filter Data" Tab exists and delete it then add a blank sheet with the same name.
Application.DisplayAlerts = False
On Error Resume Next
ThisWorkbook.Sheets("Filter Data B").Delete
On Error GoTo 0
Application.DisplayAlerts = True
Sheets.Add.Name = "Filter Data B"
Sheets("Filter Data B").Range("A1").Select

'Populate filtered data sheet using the advance filter function
Sheets("Merged Data").Range("A1").CurrentRegion.AdvancedFilter _
Action:=xlFilterCopy, _
CriteriaRange:=Sheets("Advance Filter").Range("A5:AB7"), _
CopyToRange:=Range("A1"), _
Unique:=False

'Length of filtered data minus one to not include the variable titles is the number of graduates for that major
Grads = ActiveSheet.Cells(Rows.count, "A").End(xlUp).Row - 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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