指定多个 Sheets/String 数组?

Specifying Multiple Sheets/String Array?

我正在使用一些旨在 Hide/Unhide 工作表的代码,这些代码基于下拉菜单中的选择。设置是:

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = Range("SelectCP").Address Then
    Select Case Target.Value
      Case "All"
        ShowAllSheets
      Case ""
        'do nothing
      Case Else
        ShowSelSheets
        'do nothing
    End Select
  End If
End Sub

其中函数 ShowAllSheets 和 ShowSelSheets 定义为:

Sub ShowAllSheets()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
  ws.Visible = xlSheetVisible
Next ws
End Sub


Sub ShowSelSheets()
Dim ws As Worksheet
Dim strType As String
strType = Worksheets("Counterparty Overview").Range("SelectCP").Value
For Each ws In ActiveWorkbook.Sheets
  If InStr(1, ws.Name, strType) > 0 Then
    ws.Visible = xlSheetVisible
  Else
    If ws.Name <> "Counterparty Overview" Then
      ws.Visible = xlSheetHidden
    End If
  End If
Next ws
End Sub

我遇到的问题是这个位:

Else
If ws.Name <> "Counterparty Overview" Then
  ws.Visible = xlSheetHidden

除了 "Counterparty Overview" 之外,我还想指定另外两个工作表始终显示。我试图将 strType 作为变量数组启动,但出现错误。有什么想法吗?

试试这个:

Dim SomeSheet as String
SomeSheet = "Other Sheet Name"

If ws.Name <> "Counterparty Overview" Then
  ws.Visible = xlSheetHidden
  Sheets(SomeSheet).Visible = xlVisible
  'or
  Sheets("Other Sheet Name").Visible = xlVisible

End If