initialize a workbook to open to the same sheet every time when opened - vba

I have a workbook in excel that has an accumulating number of sheets. I understand the excel document will open to the last sheet you were on. But is their a way to make the workbook open to the same page every time? (it's ok if I need to use VBA) thank you in advance for any help.

Excel's Workbook_Open() event fires too soon in the loading process for some methods or properties of the workbook itself to be reliably used. Just activate the sheet that you want to return to in the BeforeClose event handler.
'In ThisWorkbook
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Worksheets("SheetWhatever").Activate
End Sub

Include a Workbook_Open event within the code module for ThisWorkBook
Private Sub Workbook_Open()
'Instead of "Sheet1", use whichever sheet you want to activate
Worksheets("Sheet1").Activate
End Sub

Related

Activating a different worksheet using VBA does not change the focus on it permanently

I want to select and modify different worksheets programmatically every time the workbook is saved. At the end however, I want to set the focus on a particular worksheet so that the workbook is saved with that particular worksheet in focus. What I'm noticing is that whenever the code executes it activates the worksheets, modifies them but at the end it goes back to the worksheet that I had selected before running the code.
Here's my code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets(1).Activate
Debug.Print Sheets(1).Name
End Sub
The code above is executed in an empty, local workbook with 2 empty worksheets
Sheet1 and Sheet2. Whenever I save the workbook with Sheet2 selected, I see that it is indeed activated because the console log prints Sheet1, in the workbook however, the selected worksheet remains Sheet2.I'm using SAP's BusinessObjects Analysis but as noted above, the workbook is a local macro-enabled workbook that is not saved on the SAP NetWeaver platform.
Is it possible for me to permanetly set the focus to a different worksheet so that it's visible in the workbook?
Thanks
EDIT:
Oh no!!! I have the annoying problem of inconsistent behavior with the different save buttons once again and that is yet to be resolved! I just realized that if I save through the workbook save button the sheet permanently changes, however when I save through the code editor it doesn't. The previous problem I had experienced was on workbooks saved on SAP NetWeaver where VBA code is not executed through the workbook save button but is, through the code editor save button. I guess I will have to log an Oss with SAP for this inconsistency.
What you are saying is only possible, if someone has written:
Private Sub Worksheet_Activate()
Sheets(2).Activate
End Sub
At Worksheets(1).
Otherwise, the code you are using:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets(1).Activate
Debug.Print Sheets(1).Name
End Sub
should activate the first Sheet and it should not be changed later.
I have no idea where Sheet2 is compared to Sheet1.
You say
The code (above) is executed in an empty, local workbook with 2 empty
worksheets Sheet1 and Sheet2. Whenever I save the workbook with
Sheet2 selected, I see that it is indeed activated because the console
log prints Sheet1, in the workbook however, the selected worksheet
remains Sheet2
Your code doesn't do anything to a sheet called Sheet2. It only looks at the first sheet in the tab order - the sheet could be called anything.
It activates the first sheet and then puts the name of the first sheet in the immediate window.
This code will select the sheet with the tab name Sheet2, it will then put the name of the activesheet (Sheet2) in cell A1 of the sheet with the tab name Sheet1.
Finally it selects the sheet with the codename Sheet3 (The codename is the name not in brackets in the Project Explorer).
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
With ThisWorkbook
.Worksheets("Sheet2").Select
.Worksheets("Sheet1").Range("A1") = ActiveSheet.Name
End With
Sheet3.Select
End Sub
Just use following code:
Sub activateSheet(sheetname As String)
'activates sheet of specific name you want.
Worksheets(sheetname).Activate
End Sub
then for select another sheet:
Sub activateSheet(sheetname As String)
'selects sheet of specific name you want.
Sheets(sheetname).Select
End Sub
Regards
Xsi

How to make vba code to run auto-close only on active workbook?

Here's the scenario. I have multiple excel workbooks that copy and paste data among each other. So the macro works to open.copy.close from one workbook then open.paste.close to another. I am working on creating a function to auto run macro when file is closed.
Here's the issue, when I click macro button in workbook 1, it is supposed to open.copy.close from workbook 2. However, because of the auto run when file is closed function in workbook 2, an error will occur (2 macros cannot run at the same time)Any solution for this? I am looking for a solution to only auto run macro when file is closed IF IT IS AN ACTIVE WORKBOOK. Here is what I have now:
Workbook 1
Sub workbook_beforeclose(cancel As Boolean)
Application.Run "Sheet1.UpdateYellowTabs_Click"
End Sub
Workbook 2
Sub Workbook_BeforeClose(Cancel As Boolean)
Workbook.BeforeClose
Application.Run "Sheet12.UpdateGreen_Click"
End Sub
How do I code it in the workbook code to only make this run only when it's active/closed by a human user and not when open/close by macro?
Thanks!
Well I am not sure to understand your final goal from this, but I can answer the "technical" question. Technically, if you want to check if a given workbook is active: If Application.ActiveWorkbook Is Me,
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not Application.ActiveWorkbook Is Me Then Exit Sub ' <-- add this test
''''''''''''''''''''''''''
' Whatever code goes here
''''''''''''''''''''''''''
End Sub
EDIT
But problem is that invoking wb2.close will make the workbook wb2 the "active" one during the execution of the macro. So this method won't work. Another method is to disable events before closing the workbook; so that the event Workbook_BeforeClose will not fire. After that, you can enable events back if needed. The code looks like this:
' ... open wb2 and do the work with it
Application.EnableEvents = False
wb2.Close False
Application.EnableEvents = True
notice, if you had already disabled events at the beginning of the current macro, which is usually recommended, then this additional code wouldn't be needed.

VBA Select a sheet when the workbook is opened

The following program doesn't work when I open my workbook. What are the possible reasons?
' Select the first sheet when the workbook is opened.
Private Sub Workbook_Open()
Sheet4.Select
Sheet4.Range("B1").Select
End Sub
If you hit alt+F11 to go to the VBA code editor. On the left side, under the file name you will see the different sheets, and whatever modules you might have in there. If you go under the ThisWorkbook module
and place your code there, it will automatically run when you start up the Excel File.
you are using the "Select" Method without an Activating a Sheet First!
Yes, when you have closed your workbook last time, the current sheet will remain in the memory index and when you will again open the same workbook, the pointer search for the most recent sheet used based on the index no.
'Here is the code
Private Sub Workbook_Open()
Sheet4.Activate
Sheet4.Select
Sheet4.Range("B1").Select
End Sub
using "Select Method" without Activating the Parent Object is a Crime. lol
Hope this will help you.

Switch back to one workbook after activating another workbook

My problem is as follows:
I have two files opened - file1.xlsm and file2.xlsm. The first one is active. I want to switch back to the first workbook, every time I activate the second one. I put
Private Sub Workbook_Activate()
Windows("file1.xlsm").Activate
End Sub
in a macro module of the second workbook. It doesn't work. Is there a way to do it? Thanks for any help
You have to place
Private Sub Workbook_Activate()
Workbooks("file1.xlsm").Activate
End Sub
in the ThisWorkbook code window of file2.xlsm (not a macro module).
Try
Call Workbooks("filename.xlsm").Activate

Macro from on open event for worksheet

I have a macro that I want to run as part of open event. I recorded the macro to copy and paste some columns on a worksheet. This macro will only work when I am on that worksheet.
Since I want to run this macro as one of many applications when workbook is open and it doesn't always open on this specific worksheet I would like the code to target the worksheet and run the macro.
Here is my code but for some reason it doesn't work :
Application.Run "'Workbook.xlsb'!Sheet5.Copy_Paste2"
If I'm understanding you, you need to call Copy_Paste2 from ThisWorkbook Open event.
Private Sub Workbook_Open()
Sheet5.Copy_Paste2
End Sub