Help with VBA Code Trying to get code to run on all sheets on open of the spreadsheet

Norto

New Member
Joined
Mar 26, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have been trying to get a code to work on my spreadsheet that runs as soon as you open the spreadsheet file. The code allows for the grouped rows to be seen or not seen depending on the user but doesn't allow any of the cells to be edited unless you have the password (basically like a protected sheet).

I have attached screenshot of the spreadsheet as well as the code I have been trying to manipulate. I'm very new to coding and only teaching myself (with the help of this forum and youtube) so go easy on me if I have the total wrong code or something.

Thanks
Regards
Norto

here is the code.

Sub Workbook_Open()
Dim Dosomething()
Dim xSh As Worksheet
Application.ScreenUpdating = False
For Each xSh In Worksheets
xSh.Select
Call RunCode
Next
Application.ScreenUpdating = True
End Sub
Sub RunCode()
Dim OnEdit
Dim EnableOutlining()
Dim xWs As Worksheet
Set xWs = Application.ActiveSheet
Dim xPws As String
xPws = Application.InputBox("Password:", xTitleId, "", Type:=2)
xWs.Protect Password:=xPws, Userinterfaceonly:=True
xWs.EnableOutlining = True

End Sub

Here is the front page of the sheet.

1585256517780.png
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Is the problem that the code does not run at all?

If it is then have you put the code in the ThisWorkbook Module?
 
Upvote 0
No the code works just doesn't work as I want it to.
Which is, as soon as the spreadsheet has been opened and across all worksheets.

regards,
Norto
 
Upvote 0
First thing to do is to get the code to work when you open the workbook.

Substitute the code that you have with the code below and let us know if it works.

If it does work just build up from there. If not then you probably do not have the code in the ThisWorkbook module.

VBA Code:
Sub Workbook_Open() 
     MsgBox "It works"
end Sub

Then substitute the code below.

VBA Code:
Sub Workbook_Open()
Dim xSh As Worksheet
    
    Application.ScreenUpdating = False
    
    For Each xSh In Worksheets
        MsgBox xSh.Name
    Next
       
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Thanks Heaps I'll give it a go and let you know.
regards,
Norto
 
Upvote 0
First thing to do is to get the code to work when you open the workbook.

Substitute the code that you have with the code below and let us know if it works.

If it does work just build up from there. If not then you probably do not have the code in the ThisWorkbook module.

VBA Code:
Sub Workbook_Open()
     MsgBox "It works"
end Sub

Then substitute the code below.

VBA Code:
Sub Workbook_Open()
Dim xSh As Worksheet
   
    Application.ScreenUpdating = False
   
    For Each xSh In Worksheets
        MsgBox xSh.Name
    Next
      
    Application.ScreenUpdating = True
   
End Sub
Thanks Mate,

That has worked perfectly, now just to get the outlining working with protected cells and I will be good to go.

Regards,
Norto.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,047
Members
449,064
Latest member
scottdog129

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