VBA:Excel循环 – 隐藏/取消隐藏工作表
Sub Hideall_butlast_10() Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets ws.Visible = xlSheetVisible Next ws i = Worksheets.Count For x = 10 To i Sheets(x).Select ActiveSheet.Visible = xlSheetHidden Next x End Sub
大家好
我试图编辑我的代码,以取消隐藏所有的表,然后隐藏所有表除了前5和最后10(10将改变) – 但前5总是显示
我的工作簿有50-300页(每天更改)
希望有人能指引我正确的方向
谢谢
Sub Hideall_butlast_10() Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets ws.Visible = xlSheetVisible Next ws Const lowerBound As Integer = 5 Const upperBound As Integer = 10 Dim i, x i = Worksheets.Count If (i > lowerBound + upperBound) Then For x = lowerBound + 1 To (i - upperBound) Sheets(x).Visible = xlSheetHidden Next x End If End Sub
这里是我刚刚写给你的顶级代码。
Public Enum ToggleSheets Show = 1 Hide = 2 Toggle = 3 End Enum Sub ToggleSheets(Optional ByVal startIndex As Integer = 1, Optional ByVal StopIndex As Integer = 0, Optional ByVal Toggle As Integer = Toggle) If StopIndex = 0 Then StopIndex = ThisWorkbook.Sheets.Count For i = startIndex To StopIndex Select Case Toggle Case Show ThisWorkbook.Sheets(i).Visible = xlSheetVisible Case Hide ThisWorkbook.Sheets(i).Visible = xlSheetHidden Case Toggle If ThisWorkbook.Sheets(i).Visible = 0 Then ToggleSheets i, i, Show Else ToggleSheets i, i, Hide End If End Select Next End Sub
示例呼叫:
ToggleSheets , , hide'hides all sheets ToggleSheets 1,5, show 'displays first 5 sheets. togglesheets workbooks.count - 9, , show ' shows last 10 sheets
我注意到,你只是select表单,而不是激活它。 您需要在使用ActiveSheet对象之前“激活”工作表。
以下是您的代码的修改版本:
Sub Hideall_butlast_10() Dim ws As Worksheet For Each ws In ActiveWorkbook.Worksheets ws.Visible = xlSheetVisible Next ws Dim i, x As Long i = Worksheets.Count For x = 1 To i - 10 Sheets(x).Activate ActiveSheet.Visible = xlSheetHidden Next x End Sub
以下是我将如何实现它:
Option Explicit Sub HideAllSheets() Dim wkbBook As Excel.Workbook Dim wksSheet As Excel.Worksheet Set wkbBook = Excel.ActiveWorkbook On Error Resume Next ' to suppress the error that turns up when trying to hide the last sheet For Each wksSheet In wkbBook.Worksheets wksSheet.Visible = xlSheetHidden Next wksSheet Err.Clear On Error GoTo 0 End Sub Sub UnhideAllSheets() Dim wkbBook As Excel.Workbook Dim wksSheet As Excel.Worksheet Set wkbBook = Excel.ActiveWorkbook On Error Resume Next For Each wksSheet In wkbBook.Worksheets wksSheet.Visible = xlSheetVisible Next wksSheet Err.Clear On Error GoTo 0 End Sub Sub UnhideFirstXSheets(ByVal lngX As Long) Dim wkbBook As Excel.Workbook Set wkbBook = Excel.ActiveWorkbook Dim wksSheet As Excel.Worksheet Dim lngCount As Long For lngCount = 1 To lngX Set wksSheet = wkbBook.Sheets(lngCount) wksSheet.Visible = xlSheetVisible Next lngCount End Sub Sub UnhideLastXSheets(ByVal lngX As Long) Dim wkbBook As Excel.Workbook Set wkbBook = Excel.ActiveWorkbook Dim wksSheet As Excel.Worksheet Dim lngCount As Long Dim lngSheetCount As Long lngSheetCount = wkbBook.Worksheets.Count For lngCount = 1 To lngX Set wksSheet = wkbBook.Sheets(lngSheetCount - lngCount + 1) wksSheet.Visible = xlSheetVisible Next lngCount End Sub Sub DoWhatYouWant() Application.ScreenUpdating = False Application.EnableEvents = False Call UnhideAllSheets 'Unhide all the sheets (not really necessary Call HideAllSheets 'Hide everything. Leaves the last sheet visible, because all the sheets in a workbook cannot be hidden Call UnhideFirstXSheets(5) 'Unhides the first X sheets Call UnhideLastXSheets(10) 'Unhides the last X sheets Application.EnableEvents = True Application.ScreenUpdating = False End Sub
运行“DoWhatYouWant”macros,应该这样做。 根据您的要求,随意更改5和10。