Option Explicit
Sub 工作表顯示隱藏控制()
Application.ScreenUpdating = False
Dim N%, i&, xR, V, Y, Z, No, G1, G2, G3
No = InputBox("請輸入 0~4 數字!", "工作表顯示隱藏控制", 0)
If No < 0 Or No > 4 Then Exit Sub
Set Y = CreateObject("Scripting.Dictionary")
V = ",工作表1,工作表2,工作表3,工作表4,工作表5,工作表6,工作表7,工作表8,工作表9,工作表10,工作表11,工作表12,工作表13,工作表14,工作表15"
V = Split(V, ",")
For Each xR In [A1:E1]
Z = Split(xR.Address, "$")(1)
Y(Z) = Array(V(1 + N), V(2 + N), V(3 + N))
N = N + 3
Next
For Each xR In ActiveWorkbook.Worksheets
xR.Visible = True
Next
If No = 0 Then GoTo 111
Select Case No
Case 1: G1 = Y("C"): G2 = Y("D"): G3 = Y("E")
Case 2: G1 = Y("B"): G2 = Y("D"): G3 = Y("E")
Case 3: G1 = Y("B"): G2 = Y("C"): G3 = Y("E")
Case 4: G1 = Y("B"): G2 = Y("C"): G3 = Y("D")
End Select
Sheets(G1).Visible = False
Sheets(G2).Visible = False
Sheets(G3).Visible = False
111
ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
Set Y = Nothing
Erase V
End Sub
'執行下列程式碼可取得 15個工作表名的字串
Sub 收集15個工作表名()
Dim i&, Na$
For i = 1 To 15
Na = Na & "," & Sheets(i).Name
Next
Workbooks.Add
[A1] = "V = """ & Na & """"
End Sub
'把 15個工作表名的字串 與上方紅字置換掉作者: Andy2483 時間: 2022-12-20 10:40
感謝Andy2483先進的解答
以上VBA程式碼基本上可以使用,有將V替換掉,輸入0、4正常運作。
V = ",工作表1,工作表2,工作表3,工作表4,工作表5,工作表6,工作表7,工作表8,工作表9,工作表10,工作表11,工作表12,工作表13,工作表14,工作表15"
配合 Sub 收集15個工作表名() 替換成
V = [A1]
'執行下列程式碼可取得 15個工作表名的字串
Sub 收集15個工作表名()
Dim i&, Na$
For i = 1 To 15
Na = Na & "," & Sheets(i).Name
Next
Workbooks.Add <<測試時無使用新增工作表,在原活頁簿新增 [工作表1] 使用遇以上問題。
[A1] = "V = """ & Na & """"
End Sub
'把 15個工作表名的字串 與上方紅字置換掉
Option Explicit
Sub TEST_20221220()
Dim A(100), i&, Na$, N%
For i = 1 To 15
Na = Na & "," & Sheets(i).Name
Next
A(N) = "Sub 工作表顯示隱藏控制()": N = N + 1
A(N) = "Application.ScreenUpdating = False": N = N + 1
A(N) = "Dim N%, i&, xR, V, Y, Z, No, G1, G2, G3": N = N + 1
A(N) = "No = InputBox(""請輸入 0~4 數字!"", ""工作表顯示隱藏控制"", 0)": N = N + 1
A(N) = "If No < 0 Or No > 4 Then Exit Sub": N = N + 1
A(N) = "Set Y = CreateObject(""Scripting.Dictionary"")": N = N + 1
A(N) = "V = """ & Na & """": N = N + 1
A(N) = "V = Split(V, "","")": N = N + 1
A(N) = "For Each xR In [A1:E1]": N = N + 1
A(N) = " Z = Split(xR.Address, ""$"")(1)": N = N + 1
A(N) = " Y(Z) = Array(V(1 + N), V(2 + N), V(3 + N))": N = N + 1
A(N) = " N = N + 3": N = N + 1
A(N) = "Next": N = N + 1
A(N) = "For Each xR In ActiveWorkbook.Worksheets": N = N + 1
A(N) = " xR.Visible = True": N = N + 1
A(N) = "Next": N = N + 1
A(N) = "If No = 0 Then GoTo 111": N = N + 1
A(N) = "Select Case No": N = N + 1
A(N) = "Case 1: G1 = Y(""C""): G2 = Y(""D""): G3 = Y(""E"")": N = N + 1
A(N) = "Case 2: G1 = Y(""B""): G2 = Y(""D""): G3 = Y(""E"")": N = N + 1
A(N) = "Case 3: G1 = Y(""B""): G2 = Y(""C""): G3 = Y(""E"")": N = N + 1
A(N) = "Case 4: G1 = Y(""B""): G2 = Y(""C""): G3 = Y(""D"")": N = N + 1
A(N) = "End Select": N = N + 1
A(N) = "Sheets(G1).Visible = False": N = N + 1
A(N) = "Sheets(G2).Visible = False": N = N + 1
A(N) = "Sheets(G3).Visible = False": N = N + 1
A(N) = "": N = N + 1
A(N) = "111": N = N + 1
A(N) = "ActiveWindow.ScrollWorkbookTabs Position:=xlFirst": N = N + 1
A(N) = "Set Y = Nothing": N = N + 1
A(N) = "Erase V": N = N + 1
A(N) = "End Sub" ': N = N + 1
Workbooks.Add
[A1].Resize(N + 1) = Application.Transpose(A())
End Sub作者: goner 時間: 2022-12-20 16:54
Option Explicit
Sub 工作表顯示隱藏控制()
Application.ScreenUpdating = False
'↑螢幕暫不跟隨程序變化執行結果
Dim N%, No%, i&, V, xR, Y, G1, G2, G3
'↑宣告變數:(xR ,V, Y, G1, G2, G3)是通用型變數
'(N,No)是短整數變數,i是長整數
No = InputBox("請輸入 0~4 數字!", "工作表顯示隱藏控制", 1)
'↑令No是 InputBox 函式,1是預先置入的數字
'https://learn.microsoft.com/zh-tw/office/vba/language/reference/user-interface-help/inputbox-function
If No < 0 Or No > 4 Then Exit Sub
'↑令如果No這短整數值不是0,1,2,3,4之一!,就結束程式執行
Set Y = CreateObject("Scripting.Dictionary")
'↑令Y是 字典
V = Sheets(1).[A1]
'↑令V這通用型變數是 第一個工作表[A1]儲存格值
V = Split(V, ",")
'↑令V這通用型變數變為 將自己被","符號分割的一維陣列
For Each xR In [{"A","B","C","D","E"}]
'↑設順迴圈!令xR這通用型變數是 一維陣列裡的一個值,從前面輪到最後
Y(xR) = Array(V(1 + N), V(2 + N), V(3 + N))
'↑令xR迴圈值是key,Item是一維陣列,裡面有3個值,
'N是短整數,初始值是0,所以一開始的3個值是[A1]儲存格值被分割後的索引號1,2,3個陣列值
N = N + 3
'↑令N累加 3
Next
For Each xR In ActiveWorkbook.Worksheets
'↑設順迴圈!令xR這通用型變數變為此工作中活頁部的工作表之一,從前面輪到最後
xR.Visible = True
'↑令xR迴圈工作表顯示
Next
If No = 0 Then GoTo 111
'↑如果No這短整數值是 0,就跳到 111的位置繼續執行
Select Case No
'↑使用 Select Case 陳述式,隨著No這短整數值對照此陳述式內容
'https://learn.microsoft.com/zh-tw/office/vba/language/concepts/getting-started/using-select-case-statements
Case 1: G1 = Y("C"): G2 = Y("D"): G3 = Y("E")
'↑如果No這短整數值是 1,就令G1這通用型變數是 以"C"字元查字典的到的一維陣列,
'G2這通用型變數是 以"D"字元查字典的到的一維陣列,
'G3這通用型變數是 以"E"字元查字典的到的一維陣列
Case 2: G1 = Y("B"): G2 = Y("D"): G3 = Y("E")
'↑類推
Case 3: G1 = Y("B"): G2 = Y("C"): G3 = Y("E")
'↑類推
Case 4: G1 = Y("B"): G2 = Y("C"): G3 = Y("D")
'↑類推
End Select
Sheets(G1).Visible = False
'↑令G1這通用型變數所帶的陣列名工作表隱藏
Sheets(G2).Visible = False
'↑類推
Sheets(G3).Visible = False
'↑類推
111
ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
'↑工作表索引標籤移到最前方
Application.Goto Sheets(1).[A1]
'↑儲存格游標跳到第一個工作表[A1]
Set Y = Nothing
Erase V
'↑釋放變數
End Sub
Sub UnhideAllSheets()
Dim UAS As Worksheet
For Each UAS In ActiveWorkbook.Worksheets
UAS.Visible = xlSheetVisible
Next UAS
End Sub
'執行下列程式碼可取得 15個工作表名的字串
Sub 收集15個工作表名()
Dim i&, Na$
For i = 1 To 15
Na = Na & "," & Sheets(i).Name
Next
'Workbooks.Add
Sheets(1).[A1] = Na
'第一個工作表[A1]帶入 15個工作表名的字串
End Sub
'把 15個工作表名的字串 與上方紅字置換掉作者: goner 時間: 2022-12-21 15:40