How to only make changes to one workbook while opening? - excel

On opening of a specific workbook I want to hide some excel functions, like scroll bars or ribbon. Problem is that all other woorkbooks are affected...I use "Workbook" & Open with the following:
Private Sub Workbook_Open ()
Application.ExecuteExcel4Macro "show.toolbar(""ribbon"", false)"
Application.DisplayScrollBars=False
End Sub
I also tried the same using With Application and With ThisWorkbook.
I don't know how to limit the macro to work when only a specific Excel workbook is opening, without affecting other already opened woorkbooks, or workbooks which will be opened after the specific one is already open.

The Ribbon lives at Application level; consider hiding it in Workbook_SheetActivate and showing it again in Workbook_SheetDeactivate handlers in ThisWorkbook.
Option Explicit
Private Sub Workbook_Open()
'invoked when ThisWorkbook is opened.
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
'invoked when any sheet in ThisWorkbook is activated.
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
'invoked when any sheet in ThisWorkbook is deactivated.
End Sub
Now, these events would be firing when activating/deactivating sheets in ThisWorkbook, ...and I would expect this to cause some kind of annoying flickering with the Ribbon whenever a sheet in ThisWorkbook deactivates and then another sheet in ThisWorkbook is activated immediately after.
You can use the Sh argument to determine whether the ActiveSheet is still in ThisWorkbook, and prevent hiding the Ribbon in SheetDeactivate then:
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
'invoked when any sheet in ThisWorkbook is deactivated.
If Not ActiveSheet.Parent Is ThisWorkbook Then
CommandBars.ExecuteMso "HideRibbon" 'will show the Ribbon if it's hidden
End If
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
'invoked when any sheet in ThisWorkbook is activated.
CommandBars.ExecuteMso "HideRibbon" 'will show the Ribbon if it's hidden
Application.DisplayScrollBars=False
End Sub
Consider toggling Application.DisplayFullScreen instead of invoking Excel4 macros or obscure MsoID commands.

Related

How do I amend this VBA so it runs when a specific worksheet is selected?

I have this VBA written. Essentially what it does is; I've used 'Define Range' ("Test") on a few cells in a worksheet. The VBA makes sure that whenever the workbook is opened, it automatically zooms in to fit that range. Right now, I think it only triggers when the workbook is opened, and it gives an error message if that worksheet is not selected when the workbook is opened. Now, I would like it to run only if the specific worksheet is selected. I've literally copied it from someone else so don't really know how I would amend this...
Thanks in advance!
Private Sub Workbook_Open()
Range("Test").Select
ActiveWindow.Zoom = True
Cells(1, 1).Select
End Sub
Zoom & Scroll To Cell When Worksheet Is Activated...
... and if the worksheet is active when the workbook is opened.
It is assumed that the range is of Workbook scope.
ThisWorkbook Module
Option Explicit
Private Sub Workbook_Open()
Const WorksheetName As String = "Sheet1"
If ActiveSheet.Name = WorksheetName Then SelectMyRange
End Sub
Sheet Module, e.g. Sheet1...
... where Sheet1 is the code name i.e. the name not in parentheses, e.g. Sheet1(Data), seen in the VBE Project Explorer window.
Option Explicit
Private Sub Worksheet_Activate()
SelectMyRange
End Sub
Standard Module, e.g. Module1...
... created by using Insert Module.
Option Explicit
Sub SelectMyRange()
Const RangeName As String = "Test"
With Range(RangeName)
.Select
ActiveWindow.Zoom = True
Application.Goto .Cells(1), True
End With
End Sub

Workbooks.Add doesn't activate the new workbook when running from Workbook_Open()

I'm trying to run a macro when my workbook is opened. The macro is supposed to create a new workbook using Workbooks.Add and end with the new workbook active. However, I cannot seem to make this happen no matter what I try.
The simplest attempts I tried were the following (you can try it yourself):
Private Sub Workbook_Open()
Dim wb As Workbook
Set wb = Workbooks.Add
wb.Activate
End Sub
Or this one from within the module itself:
Sub Auto_Open()
Dim wb As Workbook
Set wb = Workbooks.Add
wb.Activate
End Sub
I tried various things such as adding the following lines of code:
DoEvents or
Application.Wait (Now + TimeValue("00:00:02"))
I also tried using If Then statements to check the name of the active workbook, but that didn't work either.
In the end, the original file that I clicked on is still the active workbook. How can I get the newly created workbook to be the active window when the macro is finished running? Thanks!
Edit: By the way, note that if you try running the exact same code in a regular sub, it behaves the way that I want. It just doesn't seem to work when running in the 'run on workbook open' subs.
Application.Wait just idles the Excel Application, so that won't work as you've observed. If all else fails (I'm not in a position to test at the moment) you could use Application.OnTime to schedule a procedure that creates a new workbook and activates it, 1 second after the Workbook_Open event:
Option Explicit
Private Sub Workbook_Open()
Application.OnTime Now + TimeValue("00:00:01"), "addNewWb"
End Sub
Private Sub addNewWb()
Dim wb As Workbook
Set wb = Workbooks.Add
wb.Activate
End Sub
I have been running all the different ways I can. But a workaround is to add a MsgBox
Private Sub Workbook_Open()
Dim wb As Workbook
Set wb = Workbooks.Add
wb.Activate
MsgBox "New Book Added"
End Sub
When you will click okay on the MsgBox, it will stay on the Newly created Workbook.

Auto Close Workbook

I have 2 workbooks. Workbook A & B.
When workbook A opens, workbook B opens automatically. Auto open workbook macro is working nicely. But when I want to close workbook A, the workbook B should close together automatically. But with the auto close macro below, it doesn't seems to work. Please advise where went wrong :
Private Sub Workbook_Close()
If Workbooks.Close("A.xlxm") Then
ThisWorkbook.Close
End If
End Sub
The code above is pasted in workbook B's ThisWorkbook.
In workbook A in the ThisWorkbook code pane.
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
Application.Workbooks.Item("B.xlsm").Close True
On Error GoTo 0
End Sub

Default worksheet on workbook open

I have tried to macro my workbook select a specific worksheet but unsuccessfully. Please see picture attached for the code.
Any ideas how to fix it or what I have done wrong?
Put your workbook open sub in ThisWokrbook rather than in the sheet.
Add this to ThisWorkbook
Private Sub Workbook_Open()
Run "OpenSheet"
End Sub
and in a module add:
Sub OpenSheet()
'Activate Workbook
Workbooks("urworkbook.xls").Activate
'Activate Worksheet
Workbooks("urworkbook.xls").Sheets("urSheet").Activate
End Sub
just some improvements to the Workbook_Open procedure :
(It will display Full Screen, like a real application)
Private Sub Workbook_Open()
Application.DisplayFullScreen = True
Run "OpenSheet"
End Sub

Set Workbook Variable from Userform Combobox

I am creating a macro for my co-workers. They get a file daily and at the end of the day have to copy certain information to another workbook. The macro is to take care of the copying. I want to have a userform with a combobox popup that contains a list of current open workbooks so it knows which file to copy from. How do I set it up so that the selection made there sets a workbook variable with that selection?
What I'm trying to do is:
Sub CopySub()
Dim wb As Workbook
UserForm1.Show
Set wb = Workbooks(ComboBox1.Value)
....Rest of Copy and Paste Code
Below is the code for the userform:
Private Sub OK_Click()
'Take user selection and continue copy and paste code
UserForm1.Hide
End Sub
Private Sub Cancel_Click()
'Cancel everything, end all code
End
End Sub
Private Sub UserForm_Activate()
'Populate list box with names of open workbooks.
Dim wb As Workbook
For Each wb In Workbooks
ComboBox1.AddItem wb.Name
Next wb
End Sub
Your code isn't working now because CopySub doesn't know what\where ComboBox1 is. Also, if the user clicks the form's X to close it instead of pressing the cancel button or clicks the OK button without selecting a workbook, CopySub will keep running.
There are a couple different ways to get the form information. The simplest with your current code is to properly reference ComboBox1 and add a simple test.
Sub CopySub()
Dim wb As Workbook
UserForm1.Show
If UserForm1.ComboBox1.Value = "" Then
Exit Sub
End If
Set wb = Workbooks(UserForm1.ComboBox1.Value)
' rest of code goes here
End Sub
Something else to think about though is ways to make your macro quicker and easier to run. If the only thing on your form is a Combobox for selecting the workbook and users will be starting the macro from a keyboard-shortcut or from the menu, consider having the macro ask if they want to run the macro on the active workbook. Clicking Yes to a question is a lot faster than having to click a dropdown box, select the workbook, and then click OK.
Sub CopySub()
Dim wb As Workbook
If MsgBox("Do you want to run the macro on '" & ActiveWorkbook.Name & "'?", vbQuestion + vbYesNo) = vbYes Then
Set wb = ActiveWorkbook
Else
UserForm1.Show
If UserForm1.ComboBox1.Value = "" Then
Exit Sub
End If
Set wb = Workbooks(UserForm1.ComboBox1.Value)
End If
' rest of code goes here
End Sub
After further searching I found the answer, and its the same as what mischab points out, I didn't create a global variable so there was no way for my userform to communicate with the subroutine. I solved this by declaring a variable with scope for the whole workbook as such:
Public wb1 As String
Sub CopySub()
Dim wbCAR As Workbook
UserForm1.Show
Set wbCAR = Workbooks(wb1)
....Rest of code
and by setting the userform code to such:
Private Sub OK_Click()
wb1 = ComboBox1.Value
UserForm1.Hide
End Sub
Private Sub Cancel_Click()
Unload Me
End
End Sub
Private Sub UserForm_Activate()
'Populate list box with names of open workbooks.
Dim wb As Workbook
For Each wb In Workbooks
ComboBox1.AddItem wb.Name
Next wb
End Sub

Resources