Help with workbook open code

CV899000

Board Regular
Joined
Feb 11, 2016
Messages
98
Hello, I have this code that runs when I open my workbook.

The problem I have is that I have a macro that updates my exchange rates and I prompt a message when I open the workbook asking if I want to update or not.

The sheet where the exchange rates are in, is hidden, so I have to unhide the sheet when updating and hide it again when done.

This works fine when I say yes to the update.

If I say no, then I exit the sub and the exchange rate sheet is not hidden.
How do I ensure that it is hidden when I press YES and NO?

Here is the code:

Private Sub Workbook_Open()
Worksheets("Copied Prices").Cells.Delete
Worksheets("Shock Compliance Information").Activate
Worksheets("Prices").Range("B5").Value = ""
Application.ScreenUpdating = False
Sheets("Exchange rates").Visible = True
If MsgBox("Internet connection needed to update exchange rates. Update now?", vbYesNo) = vbNo Then Exit Sub
Sheets("Exchange rates").Select
Selection.QueryTable.Refresh BackgroundQuery:=False
Sheets("Exchange rates").Visible = False
Range("A1").Select
Application.ScreenUpdating = True
End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
change your IF line on
Code:
If MsgBox("Internet connection needed to update exchange rates. Update now?", _
vbYesNo) = vbNo Then
Sheets("Exchange rates").Visible = False
Application.ScreenUpdating = True
Exit Sub
End If
 
Upvote 0
Yes, I know why it did not work.

It did not work because excel was told that if I chose "NO" then it should exit the sub, and the code to hide the "exchange rates" again, appeared further down the sub.

I have an additional question actually.
Can I have the "Exchange rates" sheet hidden as "very hidden" instead of just "hidden"?
 
Upvote 0
Again, thank you very much for the help.

I found the answer in the thread you have provided.
Simple answer really, but I am all new to this, and I don't "speak" the same language as excel yet.
You have to know the language to do the simple things :)
 
Upvote 0
Maybe you can help me with another thing.

I have a workbook full of macros.
If another user decides to open the workbook and disables macros, I want a sheet called "Macros disabled" to be shown, and all other sheets to be very hidden.
If the user enables macros, then I want the "Macros disabled" sheet to be very hidden, but "Shock Compliance Information", "Prices" and "Copied Prices" to be shown and the normal open workbook code to run.

I tried just to say:
Sheets("Shock Compliance Information").Visible = True
Sheets("Prices").Visible = True
Sheets("Copied Prices").Visible = True
Sheets("Macros disabled").Visible = xlVeryHidden

In the start of the Workbook Open sub, but that doesn't work.
 
Last edited:
Upvote 0
I cannot get this to work,

here is my entire workbook code;

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim sh As Worksheet
For Each sh In Worksheets
If sh.Name <> "Macros disabled" Then _
sh.Visible = xlSheetVeryHidden
Next
End Sub
Private Sub Workbook_Open()
Sheets("Shock Compliance Information").Visible = True
Sheets("Prices").Visible = True
Sheets("Copied Prices").Visible = True
Sheets("Macros disabled").Visible = xlVeryHidden
Worksheets("Copied Prices").Cells.Delete
Worksheets("Shock Compliance Information").Activate
Worksheets("Prices").Range("B5").Value = ""
Application.ScreenUpdating = False
Sheets("Exchange rates").Visible = True
If MsgBox("Internet connection needed to update exchange rates. Update now?", _
vbYesNo) = vbNo Then
Sheets("Exchange rates").Visible = xlVeryHidden
Application.ScreenUpdating = True
Exit Sub
End If
Sheets("Exchange rates").Select
Selection.QueryTable.Refresh BackgroundQuery:=False
Sheets("Exchange rates").Visible = xlVeryHidden
Range("A1").Select
Application.ScreenUpdating = True
End Sub

When I try to close my workbook I get the error: Run-time error '1004' Method 'Visible' of object '_Worksheet' failed.
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,031
Members
448,940
Latest member
mdusw

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