我想隐藏B列和B列中值为零的行C.我的代码有效但仅在活动工作表上。我希望它循环遍历工作簿中的所有工作表。任何帮助表示赞赏
Private Sub CommandButton1_Click()
Dim M As Long, LastRow As Long
Dim ws As worksheet
For Each ws In ActiveWorkbook.Worksheets
LastRow = ws.Range("E65536").End(xlUp).Row
For M = LastRow To 7 Step -1
If Range("B" & M).Value = 0 And Range("C" & M).Value = 0 Then
Range("B" & M).EntireRow.Hidden = True
End If
Next M
Next ws
End Sub
答案 0 :(得分:0)
激活工作表。问题是Range正在处理当前活动工作表。如果你使用ws。范围的变化或激活下面的工作表。
For Each ws In ActiveWorkbook.Worksheets
添加
ws.Activate
答案 1 :(得分:0)
我发现这个主题,我希望它有所帮助:ExtendOffice
Sub Hide_rows()
Dim LastRow As Long
Dim Rng As Range
LastRow = Range("A65536").End(xlUp).Row '
Set Rng = Range("A1:A" & LastRow) '
Application.ScreenUpdating = False
For Each cell In Rng
If cell.Value = "0" Then
cell.EntireRow.Hidden = True
End If
Next cell
Application.ScreenUpdating = True
End Sub
答案 2 :(得分:0)
您需要将Range指定为工作表的成员,如下所示:
Private Sub CommandButton1_Click()
Dim M As Long, LastRow As Long
Dim ws As worksheet
For Each ws In ActiveWorkbook.Worksheets
LastRow = ws.Range("E65536").End(xlUp).Row
For M = LastRow To 7 Step -1
' Notice how 'ws' has been added before range
If ws.Range("B" & M).Value = 0 And ws.Range("C" & M).Value = 0 Then
ws.Range("B" & M).EntireRow.Hidden = True
End If
Next M
Next ws
End Sub