我是 VBA 新手,尝试编写一个基于列表的代码,将隐藏不在列表中的工作表(列表中的名称等于工作表中的名称)
我有一个代码,但我不知道如何按照我想要的方式执行。 我需要将“可见”替换为工作表中名为“可见”的单元格列表,当代码运行时,它将隐藏不在此列表中的名称。
Sub ocultarPlanilhas()
Dim ws As Worksheet
Sheets().Select
For Each ws In Worksheets
If ws.Name <> "Visible" Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
有人可以帮助我吗?
请尝试一下。
Option Explicit
Sub HiddenSheet()
Dim ws As Worksheet
Dim rngList As Range, sList As String
Const MARKER = "|"
Const SheetName = "Sheet1" ' Modify as needed
With Sheets(SheetName)
Set rngList = .Range("Visible")
' Optional: Show at least one sheet before hidden sheets
'Sheets(rngList.Cells(1).Value).Visible = xlSheetVisible
sList = MARKER & Join(Application.Transpose(.Value), MARKER) & MARKER
End With
For Each ws In Worksheets
If InStr(1, sList, MARKER & ws.Name & MARKER, vbTextCompare) = 0 Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub