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页(每日更改)
希望有人能指出我正确的方向
由于
答案 0 :(得分:4)
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
答案 1 :(得分:1)
这里有一些关于我刚刚写的顶级代码...
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
答案 2 :(得分:0)
我注意到您只选择了工作表,而没有激活它。在使用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'宏,应该这样做。根据您的要求,随意更改5和10。