我有sheet1(Netmark Inc 8-28-2014)的数据和sheet2,前端有复选框,基于复选框选中过滤器应该应用于sheet1所以对于应用过滤器再次有一些更多的子检查如果我选择子复选框,则需要在第一个选定的过滤器范围内应用文件管理器,在该主要类别下的框下。
示例
这里我收到范围类失败的错误消息autofilter方法
以下是第一个复选框的代码
Dim My_Range As Range Dim CalcMode As Long Dim ViewMode As Long Dim FilterCriteria As String Dim CCount As Long Dim WSNew As Worksheet Dim sheetName As String Dim rng As Range Private Sub CheckBox1_Click() 'Set filter range on ActiveSheet: A1 is the top left cell of your filter range 'and the header of the first column, D is the last column in the filter range. 'You can also add the sheet name to the code like this : 'Worksheets("Sheet1").Range("A1:D" & LastRow(Worksheets("Sheet1"))) 'No need that the sheet is active then when you run the macro when you use this. Set My_Range = Sheets("Netmark Inc 8-28-2014").Range("A1:A404") My_Range.Parent.Select If ActiveWorkbook.ProtectStructure = True Or _ My_Range.Parent.ProtectContents = True Then MsgBox "Sorry, not working when the workbook or worksheet is protected", _ vbOKOnly, "Copy to new worksheet" Exit Sub End If 'Change ScreenUpdating, Calculation, EnableEvents, .... With Application CalcMode = .Calculation .Calculation = xlCalculationManual .ScreenUpdating = False .EnableEvents = False End With ViewMode = ActiveWindow.View ActiveWindow.View = xlNormalView ActiveSheet.DisplayPageBreaks = False If (CheckBox1.Value = "True") Then 'Firstly, remove the AutoFilter My_Range.Parent.AutoFilterMode = False 'Filter and set the filter field and the filter criteria : 'This example filter on the first column in the range (change the field if needed) 'In this case the range starts in A so Field 1 is column A, 2 = column B, ...... 'Use "<>Netherlands" as criteria if you want the opposite My_Range.AutoFilter Field:=1, Criteria1:=CheckBox1.Caption Else My_Range.Parent.AutoFilterMode = False End If Sheets("Questionnaire").Select End Sub
End Sub &GT; 块引用
下面是第二个复选框的代码。
私人Sub CheckBox2_Click()
Set My_Range = Sheets("Netmark Inc 8-28-2014").Range("G1:G404") My_Range.Parent.Select My_Range.AutoFilter Field:=7, Criteria1:=CheckBox2.Caption Sheets("Questionnaire").Select End Sub
答案 0 :(得分:1)
在复选框2中,您的范围是G1:G404,因此您的范围仅为1列宽,但您尝试在第7列上进行过滤。 Field参数与您使用的范围有关,而不是工作表中的整个列。
My_Range.AutoFilter Field:=1, Criteria1:=CheckBox2.Caption