2017-09-07 51 views
0

我創建了一個腳本,在行的「 - 」行中隱藏了活動工作表中的行。我想將其應用於其他工作表(即Sheet-ABC,Sheet-DEF)。我嘗試使用數組,但不成功。Excel VBA,更新特定工作表,不僅僅是活動工作表

任何幫助表示讚賞。

Sub hideRows() 
Application.ScreenUpdating = False 
Application.EnableEvents = False 
Application.Calculation = xlCalculationManual 
Application.DisplayAlerts = False 


Dim cell, cell2 As Range, hRws As Range 
Set Rng = Sheet15.Range("A52:L359") 

Rng.EntireRow.Hidden = False 

For Each cell In Range("A52:L359").SpecialCells(xlBlanks) 
If cell = "-" And cell.Offset(-1, 0) = "-" Then 
If hRws Is Nothing Then 
Set hRws = Range(cell, cell.Offset(1, 0)) 

Else 
Set hRws = Union(hRws, Range(cell, cell.Offset(1, 0))) 
End If 
End If 
Next 

If Not hRws Is Nothing Then hRws.EntireRow.Hidden = True 

Application.Calculation = xlCalculationAutomatic 
Application.DisplayAlerts = True 
Application.ScreenUpdating = True 
Application.EnableEvents = True 

End Sub 
+0

嗯 - 如果你是通過空白單元格循環,多少都會有'一個值「 - 」'? – YowE3K

回答

0

你可以在for循環中調用你的hideRows()方法。事情是這樣的:

Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets hideRows() Next

+0

這將無法正常工作(a)直到'hideRows'中的問題得到解決(例如,它將行隱藏在一個工作表上,並將它們隱藏在可能不同的表單上,它循環遍歷尋找「 - 」的空白單元格等) b)因爲你不讓'hideRows'知道要處理的工作表(c)OP只想處理某些工作表而不是所有的工作表(即它可能需要'For Each ws In ActiveWorkbook.Worksheets(Array( 「Sheet-ABC」,「Sheet-DEF」))')。 – YowE3K

+1

好點的YowE3K,稍後會在我回家時嘗試編輯。 – Kevin

+0

我試過YowE3K的建議沒有成功。活動工作表是唯一受影響的工作表。 – user328533

相關問題