VBA代码中的参数数量错误或属性分配错误

1 excel vba excel-vba

希望有人可以帮助我.

我正在尝试将一些代码放在一起,根据两个单元格的值隐藏行.

我的代码如下:

 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
Run Code Online (Sandbox Code Playgroud)

然而,当我运行它,我不断收到一个错误说参数或无效属性赋值的错误的号码,当我点击确定它没有突出任何代码的特定部分,以点我的方向.

我对VBA很新,并在几个论坛上搜索了这个错误的建议,但我尝试过的任何东西都没有用,或者我没有理解它.

任何帮助将非常感谢.

谢谢

Bra*_*ney 5

这不是有效的VBA代码:

Rows("23:43").Select Selection.EntireRow.Hidden = False
Run Code Online (Sandbox Code Playgroud)

请记住,VBA中的单行是单个命令.上面的行是两个单独的命令.这是有效的VBA代码:

Rows("23:43").Select 
Selection.EntireRow.Hidden = False
Run Code Online (Sandbox Code Playgroud)

但是现在我们处理代码本身的低效率.您正在ActiveSheet隐式使用,而您正依赖Select.为了避免Select,Activate我建议从这里开始:如何避免在Excel VBA中使用Select.

我们如何重构这段代码?简单:

' 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
Run Code Online (Sandbox Code Playgroud)

这不仅解决了手头的错误,而且还可以帮助您避免许多常见错误,这些错误将导致无数小时的挫折和维护.