Bru*_*ce 2 excel vba excel-vba
我正在尝试编写一个宏,它将遍历选定数量的工作表以隐藏每个工作表上的空行.在每个工作表的"A"列中包含1或0.如果它是0我想隐藏该行.
这是我从各个网站一起报废的代码.我最大的挑战是知道我需要操纵哪些物体.
enter code here
Public Sub HideRows()
Dim beginRow As Double
Dim endRow As Double
Dim ChkCol As Double
Dim RowCnt As Double
Dim ws As Worksheet
Dim ArrayOne As Variant
Dim InxW As Long
beginRow = 10
endRow = 185
ChkCol = 1
ArrayOne = Array("GB", "Adj. B", "Adj. F", "JC-Results", "PI-Results", "MK-Results", "TD-Results")
For InxW = LBound(ArrayOne) To UBound(ArrayOne)
For RowCnt = beginRow To endRow
If Cells(RowCnt, ChkCol).Value = 0 Then
Cells(RowCnt, ChkCol).EntireRow.Hidden = True
Else
Cells(RowCnt, ChkCol).EntireRow.Hidden = False
End If
Next RowCnt
Next
End Sub
Run Code Online (Sandbox Code Playgroud)
试试这个:
Public Sub HideRows()
Dim beginRow As Double
Dim endRow As Double
Dim ChkCol As Double
Dim RowCnt As Double
Dim ws As Worksheet
Dim ArrayOne As Variant
Dim InxW As Long
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
beginRow = 10
endRow = 185
ChkCol = 1
ArrayOne = Array("GB", "Adj. B", "Adj. F", "JC-Results", "PI-Results", "MK-Results", "TD-Results")
For InxW = LBound(ArrayOne) To UBound(ArrayOne)
With Sheets(ArrayOne(InxW))
For RowCnt = beginRow To endRow
If .Cells(RowCnt, ChkCol).Value = 0 Then
.Rows(RowCnt).Hidden = True
Else
.Rows(RowCnt).Hidden = False
End If
Next RowCnt
End With
Next InxW
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
Run Code Online (Sandbox Code Playgroud)
主要问题是您没有告诉Excel要搜索哪个工作表,因此它只搜索代码开头的活动工作表.
通过将所有内容放在一个With
块中并使用.
所有范围对象的前面将告诉excel要使用哪个工作表.
此外,关闭计算,屏幕更新和关闭事件将有助于加快代码速度,因为它不会暂停执行这些操作.