Hide/Show worksheet based on portion of filepath

George1972

New Member
Joined
Sep 9, 2014
Messages
2
I would like to use VBA to hide/show a worksheet based on a portion of the filepath name.

For example:
If the filepath was D:\Data\My Documents\Projects\excelfile.xlsm

I want the worksheet to be visible if the beginning part of the path is D:\Data\My Documents, but then hide if the user saves the file to another location and the path changes

I am sure this can be done was hoping someone could help me out.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Welcome to MrExcel forums. Put this code in the ThisWorkbook module:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    Dim SaveAs As FileDialog
 
    'The next If statement means the code below it is run only if File - Save As is clicked.
    'Remove this If statement if you want the code to also run when the Save icon (or File - Save) is clicked
    
    If Not SaveAsUI Then Exit Sub
    
    Set SaveAs = Application.FileDialog(msoFileDialogSaveAs)
    With SaveAs
        .Title = "Save Workbook"
        .InitialFileName = ThisWorkbook.FullName
        .InitialView = msoFileDialogViewDetails
        If .Show Then
            
            'Hide the sheet named "Sheet1" if workbook is not saved in "D:\Data\My Documents\" or a subfolder thereof
            
            If InStr(1, .SelectedItems(1), "D:\Data\My Documents\", vbTextCompare) = 0 Then
                ThisWorkbook.Worksheets("Sheet1").Visible = False
            End If
            
            Application.DisplayAlerts = False
            Application.EnableEvents = False
            .Execute
            Application.EnableEvents = True
            Application.DisplayAlerts = True
        End If
    End With
    
    Cancel = True
    
End Sub
Change Sheet1 in the code to the name of the sheet you want to be hidden.
 
Upvote 0

Forum statistics

Threads
1,211,434
Messages
6,101,831
Members
447,758
Latest member
jojo3strada

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top