luk*_*uke 2 excel vba excel-vba
我使用此代码创建一个新工作表并列出工作簿中的所有工作表名称,它们之间有空行,然后它隐藏工作表名称之间的所有空行.
但它接管一分钟完成是否有更有效的方法来做到这一点?
Sub ListAllSheetNames()
'Disabling the following to speed up the vba code
ActiveSheet.DisplayPageBreaks = False
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'code to create new sheet and list all sheet names in workbook
Dim xWs As Worksheet
On Error Resume Next
xTitleId = "All Sheet Names"
Application.Sheets(xTitleId).Delete
Application.Sheets.Add.Index
Set xWs = Application.ActiveSheet
xWs.Name = xTitleId
For i = 2 To Application.Sheets.Count
'Edit this to adjust the row spacing, number after *
xWs.Range("A" & ((i - 2) * 18) + 1) = Application.Sheets(i).Name
Next
'Hides all empty rows
Set Rng = Range("A1", Range("A15000").End(xlUp))
For Each cel In Rng
If Not cel.Value > 0 Then
cel.EntireRow.Hidden = True
End If
Next cel
Range("A1").Select
'UnDisabling
ActiveSheet.DisplayPageBreaks = True
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Run Code Online (Sandbox Code Playgroud)
而不是蛮力的方法:
For Each cel In Rng
If Not cel.Value > 0 Then
cel.EntireRow.Hidden = False
End If
Next cel
Run Code Online (Sandbox Code Playgroud)
你应该能够做到:
Rng.SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True
Run Code Online (Sandbox Code Playgroud)
使用SpecialCells(xlCellTypeBlanks)应该几乎是瞬时的(尽管在我的测试中,只需要几秒钟来进行强力迭代).