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
 
I solve it.

I will input the new workbook_beforeclose if anybody in the future needs it.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Macros disabled").Visible = True
Sheets("Shock Compliance Information").Visible = xlVeryHidden
Sheets("Prices").Visible = xlVeryHidden
Sheets("Copied Prices").Visible = xlVeryHidden
For Each Worksheet In Worksheets
If Worksheet.Name <> "Macros disabled" Then Worksheet.Visible = xlSheetVeryHidden
Next
End Sub
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I prefer that usage of sheet clearing
Rich (BB code):
Worksheets("Copied Prices").UsedRange.Clear
why you activate that sheet now?
Rich (BB code):
Worksheets("Shock Compliance Information").Activate
not before you use
Rich (BB code):
Range("A1").Select
you cant select a sheet)
i suppose you need something like
Rich (BB code):
Sheets("Exchange rates").UsedRange.Select
 
Upvote 0
So here is my update,

I ended up not using the messagebox when I open the wordbook.

I created a macro button to update the exchange rates when required.

My new workbook code looks like this, and it works just as I need it to.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Macros disabled").Visible = True
Sheets("Shock Compliance Information").Visible = xlVeryHidden
Sheets("Prices").Visible = xlVeryHidden
Sheets("Copied Prices").Visible = xlVeryHidden
For Each Worksheet In Worksheets
If Worksheet.Name <> "Macros disabled" Then Worksheet.Visible = xlSheetVeryHidden
Next
End Sub
Private Sub Workbook_Open()
Sheets("Shock Compliance Information").Visible = True
Worksheets("Shock Compliance Information").Activate
Sheets("Prices").Visible = True
Sheets("Copied Prices").Visible = True
Sheets("Macros disabled").Visible = xlVeryHidden
Worksheets("Copied Prices").Cells.Delete
Worksheets("Prices").Range("B5").Value = ""
End Sub

Many many thanks for your help S_wish!
 
Upvote 0

Forum statistics

Threads
1,215,454
Messages
6,124,933
Members
449,195
Latest member
Stevenciu

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