macro to test if file is saved on pc, if so, hide certain sheets

Alwinaz

Board Regular
Joined
Feb 7, 2012
Messages
201
Hi there

I am looking for a vba that will check if file Funmathics.xlsm exist on pc and if so, hide the "Instructions" sheet when open. It the file does not exist hide all sheets, except the Instructions sheet.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
.
.

Try the following code, which should be placed into the code module for ThisWorkbook (in the same workbook that contains your "Instructions" sheet).

Code:
Private Sub Workbook_Open()

    Dim FFold As String
    Dim FName As String
    Dim FPath As String
    Dim Exists As Boolean
    Dim Sht As Object
    
    'Set filename of workbook.
    'Change as necessary
    FName = "Funmathics.xlsm"
    
    'Set location of workbook
    'Change as necessary
    FFold = "C:\Users\gpeacock\Desktop"
    
    'Get path of workbook
    FPath = FFold & Application.PathSeparator & FName
    
    'Unhide all sheets in this workbook
    For Each Sht In ThisWorkbook.Sheets
        Sht.Visible = True
    Next Sht
    
    'Check if file exists
    Exists = (Dir(FPath) <> vbNullString)
    
    'If file exists then hide
    '"Instructions" sheet...
    
    If Exists Then
        ThisWorkbook.Sheets("Instructions").Visible = False
        Exit Sub
    End If
    
    'If file does not exist then hide
    'all sheets except "Instructions"...
    
    For Each Sht In ThisWorkbook.Sheets
        If Sht.Name <> "Instructions" Then
            Sht.Visible = False
        End If
    Next Sht

End Sub
 
Upvote 0
Hi there

The file will always be saved under My Documents\Funmathics. I have a macro that save the file under my documents and then create a folder Funmathics and save the file Funmathic.xlsm in that folder. How do i change your code to always find the path of My documents as sometimes My Documents can be on the C: or D: drive. Will change according to the user's set up.
 
Upvote 0
Hi there

The file will always be saved under My Documents\Funmathics. I have a macro that save the file under my documents and then create a folder Funmathics and save the file Funmathic.xlsm in that folder. How do i change your code to always find the path of My documents as sometimes My Documents can be on the C: or D: drive. Will change according to the user's set up.


Change this line:

From:

FFold = "C:\Users\gpeacock\Desktop"

To:

FFold = CreateObject("WScript.Shell").SpecialFolders("MyDocuments") & Application.PathSeparator & "Funmathics"
 
Upvote 0
hi there.

I paste your code in ThisWorkbook. If i open the file then i get Unable to set the visable property of the Workbook class.
 
Upvote 0

Forum statistics

Threads
1,214,515
Messages
6,119,973
Members
448,933
Latest member
Bluedbw

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