go to specific worksheet on close

tuktuk

Well-known Member
Joined
Nov 13, 2006
Messages
856
wht is vb code to select a specific worksheet every time you close a workbook?

that is, i need to active the active worksheet to be "CompiliedMaster" prior to closing the .xls. i still want the user to be prompted for the option of saving the file.

thanks
Terry
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.Goto reference:=Sheets("CompiliedMaster").Range("A1"), scroll:=True
End Sub

This goes in the workbook's code module (right click XL logo to the left of File > View Code).
 
Upvote 0
Put this in ThisWorkbook in the VBA objects:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("CompiliedMaster").Activate

End Sub
 
Upvote 0
what is a private sub?

that is, do i add it to my module with all of my other code? i want to say no becuase i am unable to Step Into it like i do when i test my other coding.

t
 
Upvote 0
gotcha. do i have to have a ActiveWorkbook.Save cmd in the code?

i added it to ThisWorkbook but it doesn't open to the CompilieMaster tab after saving the code....closing the .xls.......and then reopening.

t
 
Upvote 0
You could include a Save command in the code.

However, if what you want is for the same sheet always to be selected on *opening* the workbook then this would be simpler:

Code:
Private Sub Workbook_Open()
Application.Goto reference:=Sheets("CompiliedMaster").Range("A1"), scroll:=True
End Sub

which goes in the same Workbook module (you can then delete the other code as it would be superfluous).
 
Upvote 0
ahhhhh i see what you are talking about wiht the On_Open concept....but i already have a code that runs on open....i know i can add it to the code but let me explain how it all works......

here is the current on open code (i think you helped me with it a few days ago)
Code:
'ReFresh Named Range on Sheet1 to Current Region
'Sheet1="CompiliedMaster" for CPS Doc
    ActiveWorkbook.Names.Add Name:="PRange", RefersTo:=Worksheets("CompiliedMaster").Range("B4").CurrentRegion

'ReFresh Pivot Table on Sheet2
'Sheet2="CompiledMasterPivot" for CPS Doc
    For Each pvt In Worksheets("CompiledMasterPivot").PivotTables
        pvt.PivotCache.Refresh
    Next pvt
End Sub
the order of how this works currently is:

i have a code in another file that opens a 2nd file and uploads data to CompiliedMaster sheet. therefore i need to have the active sheet to always be CompiliedMaster do the data is updated to the correct sheet.

a 2nd sheet in the .xls contain a pivot witch uses "CompiliedMaster" data for it's source.

so i was thinking On Close => makes CompiliedMaster as the active sheet (then the new days data is auto updated in the correct sheet).

then On Open = > refresh pivot with the new data from CompiliedMaster.

any suggestions on streamlining this concept?
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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