我正在尝试将一些代码放在一起,根据两个单元格的值隐藏行。
我的代码如下:
Sub hideSummaryDetailed()
Application.ScreenUpdating = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
'Hide all the Rows based on the selection for Summary/Detailed data linked to cell A1
If Cells(1, 1) = 0 Then
Rows("23:336").Select Selection.EntireRow.Hidden = True
ElseIf Cells(1, 1) = 1 And Cells(10, 5) = "All" Then
Rows("23:43").Select Selection.EntireRow.Hidden = False
Range("E11").Select
Rows("44:336").Select Selection.EntireRow.Hidden = True
ElseIf Cells(1, 1) = 2 And Cells(10, 5) = "All" Then
Rows("23:126").Select Selection.EntireRow.Hidden = False
Range("E11").Select
Rows("127:336").Select Selection.EntireRow.Hidden = True
ElseIf Cells(1, 1) = 1 And Cells(10, 5) = "Cardiff" Then
Rows("128:148").Select Selection.EntireRow.Hidden = False
Range("E11").Select
Rows("23:127, 149:336").Select Selection.EntireRow.Hidden = True
ElseIf Cells(1, 1) = 2 And Cells(10, 5) = "Cardiff" Then
Rows("128:232").Select Selection.EntireRow.Hidden = False
Range("E11").Select
Rows("23:127, 233:336").Select Selection.EntireRow.Hidden = True
ElseIf Cells(1, 1) = 1 And Cells(10, 5) = "Swansea" Then
Rows("233:253").Select Selection.EntireRow.Hidden = False
Range("E11").Select
Rows("23:232, 254:336").Select Selection.EntireRow.Hidden = True
ElseIf Cells(1, 1) = 2 And Cells(10, 5) = "Swansea" Then
Rows("233:336").Select Selection.EntireRow.Hidden = False
Range("E11").Select
Rows("23:232").Select Selection.EntireRow.Hidden = True
ElseIf Cells(1, 1) = 1 And Cells(10, 5) = "Both" Then
Rows("128:148, 233:253").Select Selection.EntireRow.Hidden = False
Range("E11").Select
Rows("23:127, 149:232, 254:336").Select Selection.EntireRow.Hidden = True
ElseIf Cells(1, 1) = 2 And Cells(10, 5) = "Both" Then
Rows("128:336").Select Selection.EntireRow.Hidden = False
Range("E11").Select
Rows("23:127").Select Selection.EntireRow.Hidden = True
End If
Application.ScreenUpdating = True
Application.EnableEvents = True
Range("E11").Select
End Sub
然而,当我运行它时,我一直收到一个错误,说错误的参数数量或无效的属性分配,当我点击确定它没有突出显示代码的任何特定部分,指向我的方向。
我对VBA很新,并在几个论坛上搜索了有关此错误的建议,但我尝试的任何内容都没有用,或者我还没有理解。
非常感谢任何帮助。
由于
答案 0 :(得分:5)
这不是有效的VBA代码:
Rows("23:43").Select Selection.EntireRow.Hidden = False
请记住,VBA中的单行是单个命令。上面的行是两个单独的命令。这是有效的VBA代码:
Rows("23:43").Select
Selection.EntireRow.Hidden = False
但是现在我们处理代码本身的低效率。您隐式使用ActiveSheet
,而您依赖Select
。为避免Select
和Activate
,我建议您从此处开始:How to avoid using Select in Excel VBA。
我们如何重构此代码?简单:
' Near the beginning of the module....
Dim Target as Worksheet
' Ideally, explicitly set this to the correct worksheet.
' For now, this will still use the Activesheet, it will just
' do it more explicitly and reliably since it won't potentially change as the code is running.
Set Target = ActiveSheet
' Later in the code....
' Notice how clean this is. We rely on Target instead of an implicit ActiveSheet, and we
' don't have to rely on the `EntireRow` property of `Selection` since we are explicitly accessing
' the rows.
Target.Rows("23:24").Hidden = False
这不仅解决了手头的错误,而且还可以帮助您避免许多常见错误,这些错误将导致无数小时的挫折和维护。
答案 1 :(得分:1)
以下是上述问题的解决方案。
我重新考虑了代码。
在下面的代码中,将SheetName“Sample”替换为工作簿中工作表的名称。
Sub hideSummaryDetailed()
Application.ScreenUpdating = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sample")
With ws
'Hide all the Rows based on the selection for Summary/Detailed data linked to cell A1
If .Cells(1, 1) = 0 Then
.Range("A23:A336").EntireRow.Hidden = False
ElseIf .Cells(1, 1) = 1 And .Cells(10, 5) = "All" Then
.Range("A23:A336").EntireRow.Hidden = False
.Range("A44:A336").EntireRow.Hidden = True
ElseIf .Cells(1, 1) = 2 And .Cells(10, 5) = "All" Then
.Range("A23:A336").EntireRow.Hidden = False
.Range("A127:3A36").EntireRow.Hidden = True
ElseIf .Cells(1, 1) = 1 And .Cells(10, 5) = "Cardiff" Then
.Range("A23:A336").EntireRow.Hidden = False
.Range("A23:A127, A149:A336").EntireRow.Hidden = True
ElseIf .Cells(1, 1) = 2 And .Cells(10, 5) = "Cardiff" Then
.Range("A23:A336").EntireRow.Hidden = False
.Range("A23:A127, A233:A336").EntireRow.Hidden = True
ElseIf .Cells(1, 1) = 1 And .Cells(10, 5) = "Swansea" Then
.Range("A23:A336").EntireRow.Hidden = False
.Range("A23:A232, A254:A336").EntireRow.Hidden = True
ElseIf .Cells(1, 1) = 2 And .Cells(10, 5) = "Swansea" Then
.Range("A23:A336").EntireRow.Hidden = False
.Range("A23:A232").EntireRow.Hidden = True
ElseIf .Cells(1, 1) = 1 And .Cells(10, 5) = "Both" Then
.Range("A23:A336").EntireRow.Hidden = False
.Range("A23:A127, A149:A232, A254:A336").EntireRow.Hidden = True
ElseIf .Cells(1, 1) = 2 And .Cells(10, 5) = "Both" Then
.Range("A23:A336").EntireRow.Hidden = False
.Range("A23:A127").EntireRow.Hidden = True
End If
End With
ws.Range("E11").Select
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub