提问人:Nicole Torres 提问时间:10/31/2023 最后编辑:Mayukh BhattacharyaNicole Torres 更新时间:11/1/2023 访问量:57
基于列表隐藏工作表
Hiding Sheets based on a list
问:
我是VBA(Visual Basic for Applications)的新手,我正在开发一个代码,旨在根据预定义的列表隐藏特定的工作表。从本质上讲,我想隐藏此列表中不存在名称的任何工作表。(列表中的名称等于工作表中的名称)
我已经编写了一些代码,但我不确定如何实现我想要的结果。我要做的是将“可见”的使用替换为对名为“可见”的工作表中单元格列表的引用。
我需要将“可见”替换为工作表中名为“可见”的单元格列表。执行代码时,它应隐藏在此列表中找不到其名称的任何工作表。
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
有人可以帮我吗?
答:
1赞
taller
10/31/2023
#1
sList
将是格式为 /Sheet12/Sheet2/Sheet3/ 的字符串InStr
用于与 进行比较,并且需要 用于防止不匹配。ws.name
sList
MARKER
例如:ws.Name = "Sheet1"
InStr(1, sList, ws.Name, vbTextCompare)
返回值 1 - 它与Sheet12
InStr(1, sList, MARKER & ws.Name & MARKER, vbTextCompare)
返回 0 - 不在/Sheet1/
sList
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
评论
0赞
VBasic2008
10/31/2023
这里吹毛求疵:标记更安全的选择是工作表名称中不允许使用的字符,例如斜杠。/
0赞
taller
10/31/2023
是的,你是对的。感谢
0赞
VBasic2008
10/31/2023
#2
隐藏/显示不在列表中的工作表
Sub HideShowSheets()
Const PROC_TITLE As String = "Hide/Show Sheets"
On Error GoTo ClearError
Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
' Return the values from the single-column list
' in a 2D one-based single-column array ('Data').
Dim ws As Worksheet: Set ws = wb.Sheets("Visible")
Dim Data() As Variant, rCount As Long
With ws.Range("A2")
rCount = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row - .Row + 1
Select Case rCount
Case Is < 1:
MsgBox "The list is empty!", vbExclamation, PROC_TITLE
Exit Sub
Case 1: ReDim Data(1 To 1, 1 To 1): Data(1, 1) = .Value
Case Else: Data = .Resize(rCount).Value
End Select
End With
' Return the unique values from the array in the keys
' of a dictionary ('dict'), excluding errors and blanks.
Dim dict As Object: Set dict = CreateObject("Scripting.Dictionary")
dict.CompareMode = vbTextCompare
Dim r As Long, rValue As Variant
For r = 1 To rCount
rValue = Data(r, 1)
If Not IsError(rValue) Then
If Len(rValue) > 0 Then dict(rValue) = Empty
End If
Next r
If dict.Count = 0 Then
MsgBox "The list has no names!", vbExclamation, PROC_TITLE
Exit Sub
End If
' Loop through the sheets in the workbook and toggle the visibility
' of the sheets whose names are not on the list,
' applying the following logic:
' - if the sheet is visible, hide it.
' - if the sheet is not visible, unhide it.
Dim sh As Object ' sheets = worksheets + charts
For Each sh In wb.Sheets
If Not dict.Exists(sh.Name) Then ' not in the list
If sh.Visible = xlSheetVisible Then ' is visible
sh.Visible = xlSheetHidden ' xlSheetVeryHidden
Else ' is not visible
' If you remove the following line,
' the sheets will never become visible.
sh.Visible = xlSheetVisible
End If
'Else ' is on the list; do nothing
End If
Next sh
' Inform.
MsgBox "The sheets' visibility was toggled.", vbInformation, PROC_TITLE
ProcExit:
Exit Sub
ClearError:
MsgBox "Run-time error '" & Err.Number & "':" & vbLf & vbLf _
& Err.Description, vbCritical, PROC_TITLE
Resume ProcExit
End Sub
下一个:VBA:无法获取单元格的值
评论