VBA to show Blank Sheet when Excel Contains are not Enabled

SamarthSalunkhe

Board Regular
Joined
Jun 14, 2021
Messages
103
Office Version
  1. 2016
Platform
  1. Windows
Hello Everyone,

I am using LoginInstance (Snapshot is below), but when Excel Contains are not Enabled then the user can see data of sheet. I am trying to use the below VBA code (where I have given command as the user can see a blank sheet with the name of ("Macro-disabled") when Excel Contains are not Enabled) but I'm getting an Error (Snapshot is below).

Can someone provide a solution?

LoginInstance (Snapshot)

1631805906056.png


Error (Snapshot is below)

1631805934449.png




VBA Code:
Private Sub Workbook_Open()

'To Show Login Form
    LoginInstance = 0
    
    Application.Visible = False
    
    frmLogin.Show

'Step 1:  Declare your variables
    Dim ws As Worksheet

'Step 2: Start looping through all worksheets
    For Each ws In ThisWorkbook.Worksheets
 
'Step 3: Unhide All Worksheets
    ws.Visible = xlSheetVisible

'Step 5:  Loop to next worksheet
    Next ws

'Step 6:  Hide the Start Sheet
    Sheets("Macro-disabled").Visible = xlVeryHidden

End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try qualifying your worksheet ...
VBA Code:
'Step 6:  Hide the Start Sheet
    ThisWorkbook.Worksheets("Macro-disabled").Visible = xlSheetVeryHidden
 
Upvote 0
Try qualifying your worksheet ...
VBA Code:
'Step 6:  Hide the Start Sheet
    ThisWorkbook.Worksheets("Macro-disabled").Visible = xlSheetVeryHidden
Hi GWteB,

Thank you for your reply,

I have tried your modification but facing the same error.

Actually, I have added below two codes combine.

If I use both codes separately it works, but if I combine them I am facing an error.

Code 1

VBA Code:
Private Sub Workbook_Open()

'To Show Login Form
    LoginInstance = 0
  
    Application.Visible = False
   
    frmLogin.Show

End Sub

Code 2

VBA Code:
Private Sub Workbook_Open()
 'Step 1:  Declare your variables
    Dim ws As Worksheet

'Step 2: Start looping through all worksheets
    For Each ws In ThisWorkbook.Worksheets
 
'Step 3: Unhide All Worksheets
    ws.Visible = xlSheetVisible
Sheet2.Visible = False
'Step 5:  Loop to next worksheet
    Next ws

'Step 6:  Hide the Start Sheet
    Sheets("Macro-disabled").Visible = xlVeryHidden
 
End Sub
 
Upvote 0
If you did not left out any code involved, then I can only explain the run-time error from the fact that your worksheet "Macro-disabled" doesn't exist. So do check for typos.
Obviously, the code behind your Userform may do things that conflict with what follows after the Userform is closed, but I can't see that from distance.

VBA Code:
Private Sub Workbook_Open()

    'Step 1:  Declare your variables
    Dim ws As Worksheet
    
    LoginInstance = 0
    Application.Visible = False
    FormLogin.Show

    'Step 2: Start looping through all worksheets
    For Each ws In ThisWorkbook.Worksheets
        'Step 3: Unhide All Worksheets
        ws.Visible = xlSheetVisible
    Next ws

    'Step 6:  Hide the Start Sheet
    ThisWorkbook.Worksheets("Macro-disabled").Visible = xlSheetVeryHidden
    
    Application.Visible = True
End Sub
 
Upvote 0
Solution
If you did not left out any code involved, then I can only explain the run-time error from the fact that your worksheet "Macro-disabled" doesn't exist. So do check for typos.
Obviously, the code behind your Userform may do things that conflict with what follows after the Userform is closed, but I can't see that from distance.

VBA Code:
Private Sub Workbook_Open()

    'Step 1:  Declare your variables
    Dim ws As Worksheet
   
    LoginInstance = 0
    Application.Visible = False
    FormLogin.Show

    'Step 2: Start looping through all worksheets
    For Each ws In ThisWorkbook.Worksheets
        'Step 3: Unhide All Worksheets
        ws.Visible = xlSheetVisible
    Next ws

    'Step 6:  Hide the Start Sheet
    ThisWorkbook.Worksheets("Macro-disabled").Visible = xlSheetVeryHidden
   
    Application.Visible = True
End Sub
Thank you GWteB,

The code is working now perfectly. ???
 
Upvote 0
You are welcome and thanks for letting me know (y)
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,039
Members
449,063
Latest member
ak94

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