我正在尝试使用此代码隐藏第15行到第59行的行,如果
sumi
从B15到K15,使用i
将行从15索引到59。请参见下面
在下面的sumi
行上,我得到
运行时错误424“需要对象”
,我看不出问题出在哪里。我相信这很简单。任何帮助将不胜感激。
Dim i As Integer
Dim sumi As Single
i = 15
Do Until i = 56
StartRow = i
EndRow = i
Application.EnableEvents = True
sumi = Aplication.WorksheetFunction.Sum(Range("B" & i & ":K" & i))
Application.EnableEvents = False
If sumi = "0" Then
Rows("StartRow:RowEnd").EntireRow.Hidden = True
ElseIf sumi > 0 Then
Rows("StartRow:RowEnd").EntireRow.Hidden = False
MsgBox " sumi = " & sumi
End If
Application.EnableEvents = True
i = i + 1
Loop
答案 0 :(得分:0)
除了注释中已指出的内容之外,还必须将Rows("StartRow:RowEnd")
更改为Rows(StartRow & ":" & RowEnd)
但是您可以将整个代码缩短为:
Dim i As Integer
For i = 15 To 56
Rows(i).Hidden = (Application.WorksheetFunction.Sum(Range("B" & i & ":K" & i)) = 0)
Next
如果相关范围内的值需要进行公式计算,则请禁用循环的自动计算,然后将其返回末尾:
Dim i As Integer
Application.Calculation = xlCalculationManual ' to prevent row height adjustement
For i = 15 To 56
Rows(i).Hidden = (Application.WorksheetFunction.Sum(Range("B" & i & ":K" & i)) = 0)
Next
Application.Calculation = xlCalculationAutomatic