vba programatically have excel forget window structure - vba

In Excel 2013, Excel likes to remember Excel windows (not worksheets) that were open, and when the workbook is opened again, also open up those windows:
From user interface: With a new workbook, Ribbon Tab "View" and then "New Window". Now two windows are open. Edit at will. Save the WorkBook. Then when that workbook is opened again, both windows are opened.
I'd like to prevent this this programatically within VBA, as part of WorkBook_Open I suppose, so that just one window would open. How can I do that? I've tried closing all windows using WorkBook_Close, that didn't work.

Just loop through the Windows collection and close them until you only have one open:
Private Sub Workbook_Open()
Do While Me.Windows.Count > 1
Me.Windows(1).Close
Loop
End Sub

Related

Workbook_Open sub won't run automatically when the workbook is opened

I need this macro to run when the .xlsm workbook is opened. It only runs if I manually run the macro, it does not start when the workbook is opened. FYI, I've checked/verified macro security (this is not signed yet).
Any ideas what I'm doing wrong?
Sub Workbook_Open()
MsgBox "Hello World!"
End Sub
Your code needs to be located in the ThisWorkbook module.
Open the VBA Editor (Alt+F11)
Open the Project Explorer (Ctrl+R)
In the Project Explorer pane, double click ThisWorkbook. (If you have multiple workbooks open, make sure you choose the ThisWorkbook under the correct project.)
In the code editor pane, click the drop-down that says General.
This will bring you to a new (or existing) Workbook_Open procedure:
Private Sub Workbook_Open()
End Sub
That's where your code should be placed.
Note that clicking the other drop-down at the top-right of the Code Editor pane, will list the other Workbook-level procedures you can add.
More Information:
Chip Pearson : Code Module And Code Names

Excel quick access toolbar - run macro from active instead of previous workbook

I have a custom button (not an add-in, just a "shortcut" button) in Excel's quick access toolbar to run a macro in the active workbook. The workbook is modified and saved daily with a new filename. When the link is first created in the active workbook (call it Version 1) the macro runs fine. However the following day (filename now Version 2) clicking on the macro button opens workbook Version 1 and runs the macro saved in Version 1.
Is there a simple way to break the link and run the macro in the most recent active workbook?
The code is basic - it just opens up a userform and is saved in "ThisWorkbook"
Sub OPEN_DATA_USERFORM()
ufDATA.Show
End Sub
I will try to guide you through this, I hope it helps.
What we need to do is we need to call OPEN_DATA_USERFORM() from our "PERSONAL.XLSB" file. In VB editor screen, in the project explorer you will find a file called "PERSONAL.XLSB", you need to add an "Module" to that and add following code:
Sub KickOff()
Call Application.Run(Excel.ActiveWorkbook.Name & "!OPEN_DATA_USERFORM")
End Sub
By this we will be able to call the userform.show function from our PERSONAL.XLSB which is always running in the background when you open the excel.
PS: I am assuming your OPEN_DATA_USERFORM() is coded in your daily workbook.
UPDATE: PERSONAL.XLSB
Your file should be visible in your project explorer like below:
If it is not there record a dummy macro and select your personal file like below then it should appear in your project explorer in VBA screen:

Decide which ribbon tab to be selected

The program, that I'm working on, has a separate (additional) Add-in tab in the Excel ribbon. And there are our custom controls, etc. Some of the controls open a new workbook (new Excel window).
In Excel 2010, when the new workbook is opened, our custom tab is selected by default. But in 2013, the first (Home) tab is selected.
In the project, there is no code that controls which tab to be selected by default in a newly open workbook. That is why I am wondering it works in 2010, but not in 2013?
I researched about that, but I could find mostly articles about "how to create new tab", etc.
Any ideas and suggestions are welcome.
This should be the fix you're looking for:
yourRibbonInstance.ActivateTab("tabID")
keep in mind that the id of the tab might not be the same as its displayed name.
Not sure if this might be overkill but you could use a macro that runs on the workbook open event to activate the sheet you want to see first..
Sub workbook_open()
Sheets("Sheet2").Activate
End Sub

How to open Excel after closing it through VBA?

While coding for an automation tool using VBA for Excel, I came across this situation. I have coded the "Close" command button as well as the "X" on the top-right of the form window. Both codes are same and serving their purpose.
I would like to know, if I close the workbook using the ActiveWorkbook.Close method in both the sub-procedures, then how to view the same Excel sheet's VBA project?
I am using Microsoft Office 2010.
Below is my code for reference:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
MsgBox "Thanks for using! Tool will now close.", vbInformation + vbOKOnly
Unload Me
ActiveWorkbook.Close
End If
End Sub
Visual Basic for Applications lives in a host... application - here Microsoft Excel.
If you have 10 workbooks opened, the Visual Basic Editor (VBE) will have 10 VBA projects in your Project Explorer window (Ctrl+R); closing one, will remove its VBA project from the VBE - the VBA code is in that workbook you just closed!
There are two types of VBA projects: one is the macro-enabled workbook (.xlsm), the other is a Microsoft Excel add-in (.xlam). The former lives and dies with the workbook it's written in; the latter lives and dies with the application it's loaded in.
If you need code that can manage multiple workbooks, consider saving as (F12) an add-in; ActiveWorkbook.Close would close the active workbook, and then the add-in is still loaded... and you can still view its VBA source.
If you only mean to close a form, I suggest you don't implement the QueryClose handler, and let the form close by itself - and leave the workbook open!. If you mean to close another workbook, I strongly recommend you avoid ActiveWorkbook and use a Workbook object reference instead.

Powerpoint VBA code to appear on all new documents

I have a macro in Powerpoint 2010 to open a new document based on a template based in a central location, to be installed on several machines.
Sub PowerpointTemplate()
Application.Presentations.Open ("file")
End Sub
Powerpoint does not save the macro. Even when setting "Macro in:" to "All open presentations", it seems to reset this selection to "Presentation 1". The macro works for the time that I have this current document open, but once I close Powerpoint and reopen it, the macro has been removed.
Is there anyway to set the macro to permanently apply to all presentations?
You need to create an Add-in and load it in order to make the code available to all open presentations.
Setting Macro In to All Open Presentations simply SHOWS you the available public macros in all open presentations; it has no effect on where the macro code is saved.