我正在运行VBA宏来根据单元格值隐藏不同工作表中的行。
我所做的是录制宏并隐藏我想要的行,并编辑生成的VBA代码以包含IF条件。以下是我目前正在处理的代码。
Sub OpenQuestionnaire()
With ThisWorkbook.Worksheets("C. Questionnaire")
.Visible=xlSheetVisible
.Activate
If Range("XFD3").Value="Offsite - Lite" Then
Sheets("C. Questionnaire").Select
Columns("G").Hidden=True
ActiveWindow.SmallScroll Down:=7
Range("H166:I181,67:67,68:68,69:69,70:70").Select
Range("A70").Activate
ActiveWindow.SmallScroll Down:=14
Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82").Select
Range("A82").Activate
ActiveWindow.SmallScroll Down:=5
Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91").Select
Range("A91").Activate
ActiveWindow.SmallScroll Down:=18
Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103").Select
Range("A103").Activate
ActiveWindow.SmallScroll Down:=9
Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114").Select
Range("A114").Activate
ActiveWindow.SmallScroll Down:=7
Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114,121:121").Select
Range("A121").Activate
ActiveWindow.SmallScroll Down:=4
Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114,121:121,124:124,127:127").Select
Range("A127").Activate
ActiveWindow.SmallScroll Down:=5
Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114,121:121,124:124,127:127,128:128,129:129,130:130,134:134").Select
Range("A134").Activate
ActiveWindow.SmallScroll Down:=8
Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114,121:121,124:124,127:127,128:128,129:129,130:130,134:134,138:138").Select
Range("A138").Activate
ActiveWindow.SmallScroll Down:=8
Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114,121:121,124:124,127:127,128:128,129:129,130:130,134:134,138:138,146:146,147:147").Select
Range("A147").Activate
ActiveWindow.SmallScroll Down:=2
Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114,121:121,124:124,127:127,128:128,129:129,130:130,134:134,138:138,146:146,147:147,152:152").Select
Range("A152").Activate
ActiveWindow.SmallScroll Down:=25
Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114,121:121,124:124,127:127,128:128,129:129,130:130,134:134,138:138,146:146,147:147,152:152,172:172,173:173").Select
Range("A173").Activate
ActiveWindow.SmallScroll Down:=3
Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114,121:121,124:124,127:127,128:128,129:129,130:130,134:134,138:138,146:146,147:147,152:152,172:172,173:173,176:176").Select
Range("A176").Activate
ActiveWindow.SmallScroll Down:=9
Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114,121:121,124:124,127:127,128:128,129:129,130:130,134:134,138:138,146:146,147:147,152:152,172:172,173:173,176:176,185:185,187:187").Select
Range("A187").Activate
ActiveWindow.SmallScroll Down:=7
Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114,121:121,124:124,127:127,128:128,129:129,130:130,134:134,138:138,146:146,147:147,152:152,172:172,173:173,176:176,185:185,187:187,191:191,192:192").Select
Range("A192").Activate
ActiveWindow.SmallScroll Down:=1
Error here:>>> Union(Range("H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114,121:121,124:124,127:127,128:128,129:129,130:130,134:134,138:138,146:146,147:147,152:152,172:172,173:173,176:176,185:185,187:187,191:191,192:192,194:194,195:195")).Select
Range("A195").Activate
ActiveWindow.SmallScroll Down:=4
Union(Range("200:200,H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114,121:121,124:124,127:127,128:128,129:129,130:130,134:134,138:138,146:146,147:147,152:152,172:172,173:173,176:176,185:185,187:187,191:191,192:192,194:194"), Rows("195:195")).Select
Range("A200").Activate
ActiveWindow.SmallScroll Down:=7
Union(Range("200:200,208:208,211:211,H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114,121:121,124:124,127:127,128:128,129:129,130:130,134:134,138:138,146:146,147:147,152:152,172:172,173:173,176:176,185:185,187:187,191:191"), Range("192:192,194:194,195:195")).Select
Range("A211").Activate
ActiveWindow.SmallScroll Down:=20
Union(Range("200:200,208:208,211:211,228:228,229:229,231:231,232:232,H166:I181,67:67,68:68,69:69,70:70,80:80,82:82,87:87,91:91,103:103,113:113,114:114,121:121,124:124,127:127,128:128,129:129,130:130,134:134,138:138,146:146,147:147,152:152,172:172,173:173"), Range("176:176,185:185,187:187,191:191,192:192,194:194,195:195")).Select
Range("A232").Activate
Selection.EntireRow.Hidden=True
ElseIf Range("XFD3").Value="Onsite - Full" Then
Columns("G").Hidden=True
End If
If Range("XFD1").Value="No" Then
Range("H166").Select
ActiveCell.FormulaR1C1="Not Applicable"
Range("I166").Select
ActiveCell.FormulaR1C1="Not Applicable"
Range("H166").Select
Selection.AutoFill Destination:=Range("H166:H181"), Type:=xlFillDefault
Range("H166:H181").Select
Selection.AutoFill Destination:=Range("H166:I181"), Type:=xlFillDefault
Range("H166:I181").Select
Rows("163:181").EntireRow.Hidden=True
ElseIf Range("XFD1").Value="Yes" Then
Rows("163:181").EntireRow.Hidden=False
End If
If Range("XFD2").Value="No" Then
Range("H216").Select
ActiveCell.FormulaR1C1="Not Applicable"
Range("I216").Select
ActiveCell.FormulaR1C1="Not Applicable"
Range("H216").Select
Selection.AutoFill Destination:=Range("H216:H232"), Type:=xlFillDefault
Range("H216:H232").Select
Selection.AutoFill Destination:=Range("H216:I232"), Type:=xlFillDefault
Range("H216:I232").Select
Rows("213:233").EntireRow.Hidden=True
ElseIf Range("XFD2").Value="Yes" Then
Rows("213:233").EntireRow.Hidden=False
End If
End With
End Sub
我收到Argument Not Optional
错误。让我知道我哪里错了。我已经突出显示了我收到此错误的行。
答案 0 :(得分:0)
如果您仍想使用宏,那么它应该很短。
' get the number of rows by looking at column A (only works if you have no gaps in column A
intLastRow = Cells(Rows.Count, "A").End(xlUp).Row
' loop through all rows
For intRow = 1 To intLastRow
' test if the current row matches your criteria. Put your own IF condition here
' cells(row number, column number) 1=A, 2=B, 3=C .....
If Cells(intRow, 2) = "yes" And Cells(intRow, 3) = "no" Then
'hide the current row if it matches the criteria
Cells(intRow, 1).EntireRow.Hidden = True
End If
Next