Auto Close Workbook - excel

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

Related

Automatically close all opened form when excel workbook is deactivated

I have a macro enabled workbook with more than one for opened, When I switched workbook to another workbook, the opened form in the other workbook will prevent me from use until I close them.
Please how can i automatically close all opened form when excel workbook is deactivated
I tried this but not working
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim i as Long
For i = Userforms.Count - 1 To 0 Step -1
Unload UserForms(i)
Next
End Sub

Out of memory when open a golden xlsm file

I added few VBA code in my workbook (myworkbook.xlsm) for the function :
when my workbook is opened, the VBA code will copy one sheet from the golden workbook (golden.xlsm).
I added the Sub Workbook_Open into the object "ThisWorkbook" of myworkbook.xlsm, the copy is successful. But the issue is when I use "Alt+F11" to open the Visual Basic editor, there will be a message box to complain "Out of memory", and I can't edit my other VBA code anymore.
I try to comment out some code in the Workbook_Open handler, the following line will lead to this issue :
Set goldenWorkbook = Workbooks.Open("golden.xlsm")
My code added for the open event of object "ThisWorkbook"
Private Sub Workbook_Open()
Dim goldenWorkbook As Workbook
Application.ScreenUpdating = False
Debug.Print "Hello~you just open the file"
Set goldenWorkbook = Workbooks.Open("golden.xlsm")
goldenWorkbook.Sheets("Common").Copy Before:=ThisWorkbook.Sheets(1)
goldenWorkbook.Close True
Application.ScreenUpdating = True
Set goldenWorkbook = Nothing
End Sub

Open and close UserForm multiple times without showing it's workbook

I've created a workbook that only shows a UserForm when it runs with the following code
Private Sub Workbook_Open()
Application.Visible = False
UserForm1.Show
End Sub
When the UserForm is initialized, it opens another workbook (WHITEBOOK.xls). After data has been entered into the form and the user clicks the Submit button, the data is inserted into this workbook that has been opened (I've omitted that code, but it works)
Private Sub UserForm_Initialize()
Set wb = Workbooks.Open("S:\!Data Tracking\WHITEBOOK.xls")
wb.Sheets("Jobs with Correct Template").Activate
If IsEmpty(wb.Worksheets("Jobs with Correct Template").Range("A3").Value) = True Then
Set SheetNameCellRange = wb.Worksheets("Jobs with Correct Template").Range("A2")
Else
wb.Worksheets("Jobs with Correct Template").Activate
Set SheetNameCellRange = wb.Worksheets("Jobs with Correct Template").Range("A2", Range("A2").End(xlDown))
End If
End Sub
When the user closes the UserForm, I close the form with this code, and close the UserForm's workbook also.
Private Sub UserForm_Terminate()
Unload Me
For Each wb In Application.Workbooks
If (StrComp(wb.Name, "Whitebook Data Entry (Final).xlsm", vbTextCompare) = 0) Then
wb.Close
End If
Next
End Sub
The problem I'm having is that I can't open the UserForm if the WHITEBOOK.xls workbook is already opened. I get a Run-time error '9': Subscript out of range and it fails at this line of the UserForm_Initialize() Sub.
wb.Sheets("Jobs with Correct Template").Activate
I've tried adding a conditional when I open the WHITEBOOK workbook to check if it is already open, and if so, to make it active, but I keep running into the same issue.
The code I used to test if the workbook is open was
Private Sub UserForm_Initialize()
Dim wkb As Workbook
On Error Resume Next
Set wkb = Application.Workbooks("S:\!Data Tracking\WHITEBOOK.xls")
If wkb Is Nothing Then
Set wb = Workbooks.Open("S:\!Data Tracking\WHITEBOOK.xls")
Else
wb.Sheets("Jobs with Correct Template").Activate
End If

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