You could for example hide a worksheet by having the workbook read data from another workbook either open or closed There are two common scenarios:
If the source workbook is already open, VBA can easily access its sheets directly.
Sub HideSheetsBasedOnOpenWorkbook()
Dim srcWB As Workbook
Dim wsName As String
Dim i As Long
' Set reference to the open workbook
Set srcWB = Workbooks("SourceWorkbook.xlsx") ' Change name as needed
' Loop through list of sheet names in column A of Sheet1
With srcWB.Sheets("Sheet1")
i = 1
Do While .Cells(i, 1).Value <> ""
wsName = .Cells(i, 1).Value
On Error Resume Next
ThisWorkbook.Sheets(wsName).Visible = xlSheetVeryHidden
On Error GoTo 0
i = i + 1
Loop
End With
End Sub
If the source workbook is closed, you can still read from it using ADO or Excel formulas. Here's an ADO example:
Add a reference: Tools > References > Microsoft ActiveX Data Objects x.x Library.
File must not be password-protected or filtered.
Sub HideSheetsFromClosedWorkbook()
Dim cn As Object, rs As Object
Dim wbPath As String
Dim sql As String
Dim sheetName As String
wbPath = "C:\Path\To\Your\SourceWorkbook.xlsx" ' <<-- Update this
' Setup connection
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & wbPath & ";" & "Extended Properties='Excel 12.0 Xml;HDR=YES';"
sql = "SELECT [Sheet1$].[SheetName] FROM [Sheet1$]"
rs.Open sql, cn
Do Until rs.EOF
sheetName = rs.Fields(0).Value
On Error Resume Next
ThisWorkbook.Sheets(sheetName).Visible = xlSheetVeryHidden
On Error GoTo 0
rs.MoveNext
Loop
rs.Close: cn.Close
Set rs = Nothing: Set cn = Nothing
End Sub
Again, a reasonably competent person will overcome this issue in time.
You can prompt the User to choose the source workbook at runtime. This makes your code dynamic and user-friendly, with no hardcoding of the name required. This concept will fail if there's a requirement to multi read from the source workbook.
Sub HideSheets_FromUserSelectedWorkbook()
Dim srcWB As Workbook
Dim fd As FileDialog
Dim filePath As String
Dim wsName As String
Dim i As Long
' Ask user to select the source workbook
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.Title = "Select Source Workbook"
.Filters.Clear
.Filters.Add "Excel Files", "*.xls; *.xlsx; *.xlsm; *.xlsb"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub ' User cancelled
filePath = .SelectedItems(1)
End With
' Open the source workbook read-only
Set srcWB = Workbooks.Open(filePath, ReadOnly:=True)
' Loop through list of sheet names in column A of Sheet1
With srcWB.Sheets(1) ' or use .Sheets("Sheet1")
i = 1
Do While .Cells(i, 1).Value <> ""
wsName = Trim(.Cells(i, 1).Value)
On Error Resume Next
ThisWorkbook.Sheets(wsName).Visible = xlSheetVeryHidden
On Error GoTo 0
i = i + 1
Loop
End With
' Close the source workbook without saving
srcWB.Close SaveChanges:=False
MsgBox "Specified sheets have been hidden based on " & vbCrLf & filePath, vbInformation
End Sub
However, over time a competant person will eventually over come this issue as well. Microsoft clearly made the decision to enhance ease of use over security when the designing things for Excel.