简体   繁体   English

运行宏以根据单元格值隐藏不同工作表中的行

[英]Running a Macro to hide rows in different sheet based on a cell value

I am running a VBA macro to hide rows in a different sheet based on a cell value. 我正在运行VBA宏,以根据单元格值隐藏其他工作表中的行。

What I did was to record a macro and hide rows which I wanted, and edited the generated VBA code to include an IF condition. 我要做的是记录一个宏并隐藏所需的行,然后编辑生成的VBA代码以包含IF条件。 Below is the code which I am working on currently. 以下是我目前正在处理的代码。

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

I receive an Argument Not Optional error. 我收到“ Argument Not Optional错误。 Let me know where I going wrong. 让我知道我要去哪里。 I have highlighted the line where I am getting this error. 我突出显示了我得到此错误的行。

If you still want to use a macro then it should be pretty short. 如果您仍然想使用宏,那么它应该很短。

' 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

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

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