请以最清晰的版本编写此代码吗?我有1-20的表格和1-20的值,如果我打开工作簿,则需要对每张表格进行检查。
Private Sub Workbook_Open()
If ThisWorkbook.Sheets("OPERACE_EXIST").Cells(2, 2).Value = True Then
Sheets("TP_OP_010").Visible = True
Else
Sheets("TP_OP_010").Visible = xlSheetVeryHidden
End If
If ThisWorkbook.Sheets("OPERACE_EXIST").Cells(3, 2).Value = True Then
Sheets("TP_OP_020").Visible = True
Else
Sheets("TP_OP_020").Visible = xlSheetVeryHidden
End If
If ThisWorkbook.Sheets("OPERACE_EXIST").Cells(4, 2).Value = True Then
Sheets("TP_OP_030").Visible = True
Else
Sheets("TP_OP_030").Visible = xlSheetVeryHidden
End If
答案 0 :(得分:2)
这样的事情(不确定你的工作表命名,所以你可能需要调整代码的那一部分)
Private Sub Workbook_Open()
Dim x As Long, c As Range
Set c = Me.Sheets("OPERACE_EXIST").Columns(2)
For x = 1 To 20
Me.Sheets("TP_OP_" & Format(x, "000")).Visible = _
IIf(c.Cells(x + 1).Value, True, xlSheetVeryHidden)
Next x
End Sub
答案 1 :(得分:1)
如果工作表命名约定一致:
Private Sub Workbook_Open()
Dim i As Integer
Dim SheetName As String
With ThisWorkbook.Sheets("OPERACE_EXIST")
For i = 2 To 31
SheetName = "TP_OP_" & Format((i - 1) * 10,"000")
ThisWorkbook.Sheets(SheetName).Visible = IIf(.Cells(i, 2).Value, xlSheetVisible, xlSheetVeryHidden)
Next
End With
End Sub
或使用工作表名称数组:
Private Sub Workbook_Open()
Dim i As Integer
Dim SheetName As Variant
i = 1
With ThisWorkbook.Sheets("OPERACE_EXIST")
For Each SheetName In Array("TP_OP_010", "TP_OP_020", "TP_OP_030", "TP_OP_040", "TP_OP_050", "TP_OP_060", "TP_OP_070", "TP_OP_080", "TP_OP_090", "TP_OP_100", "TP_OP_110", "TP_OP_120", "TP_OP_130", "TP_OP_140", "TP_OP_150", "TP_OP_160", "TP_OP_170", "TP_OP_180", "TP_OP_190", "TP_OP_200")
i = i + 1
ThisWorkbook.Sheets(SheetName).Visible = IIf(.Cells(i, 2).Value, xlSheetVisible, xlSheetVeryHidden)
Next
End With
End Sub