gpt4 book ai didi

vba - Excel VBA 循环选择工作表

转载 作者:行者123 更新时间:2023-12-03 00:23:46 31 4
gpt4 key购买 nike

我正在尝试编写一个宏,它将循环遍历选定数量的工作表以隐藏每张工作表上的空行。在每个工作表的“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

最佳答案

试试这个:

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

主要问题是您没有告诉 Excel 要搜索哪个工作表,因此它仅搜索代码开始时的事件工作表。

通过将所有内容放入 With block 中并在所有范围对象前面使用 . 将告诉 excel 使用哪个工作表。

此外,关闭计算、屏幕更新和事件将有助于加快代码速度,因为它不会暂停执行这些操作。

关于vba - Excel VBA 循环选择工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34934933/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com