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
Related
In a Userform I want to open another workbook (than the active one)
Part of my code:
Private Sub cmbKontoPos1_Change()
Workbooks.Open Filename:=filename1
'Here should of course be some code, but it is not now
Workbooks(filename1).Close SaveChanges:=False
Open-command works.
But the Close-command gives error:
Error no '9'
Index out of bounds
(I get the error-message in Swedish, hope I translate correct)
What is it I do not understand?
Quick fix is to have a global Workbook Variable so you can assign it on Open and use it on other UserForm Events, the example below has two Buttons and one opens while the other one closes the Workbook. Tested and working.
You can see the Dim secondWorkbook outside the UserForm buttons this is to make it global. On the first button I assign the Opened workbook to that Variable and i can close it later.
Dim secondWorkbook As Workbook
Private Sub CommandButton1_Click()
'Open WB
Dim filename1 As String
filename1 = "F:\WorkbookPath\WBName.xlsx"
Set secondWorkbook = Workbooks.Open(filename1)
End Sub
Private Sub CommandButton2_Click()
'Close WB
secondWorkbook.Close False
End Sub
You can also use this variable to make changes to the workbook like: secondWorkbook.Worksheets(1).Range("A1").Value = "Test" as long as it is still open of course.
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
I have a weird situation that I haven't been able to find the solution for.
I am dealing with large amounts of data on multiple workbooks that need to be opened (let's say 3 workbooks). I need a Userform to be able to interact with all 3 workbooks.
I have made a ComboBox able to do that by when the userform is initialized, it will add the names of the workbooks to the Combobox:
'* initialize the userform *'
Private Sub UserForm_Initialize()
Dim wb As Workbook
'* get the name of all the workbooks and add to the combobox '*
For Each wb In Application.Workbooks
Me.PrimaryBook_ComboBox.AddItem wb.name
Next wb
Me.PrimaryBook_ComboBox = ActiveWorkbook.name
End Sub
Upon a change, it will activate that workbook:
Private Sub PrimaryBook_ComboBox_Change()
Application.ScreenUpdating = True
Dim wb As Workbook
If Me.PrimaryBook_ComboBox <> "" Then
Set wb = Workbooks(Me.PrimaryBook_ComboBox.Text)
wb.Activate
End If
Application.ScreenUpdating = False
End Sub
(this userform has two refedits in it)
When I select another workbook in the combobox, it brings that workbook to the front as it should. But immediately as I click into one of my RefEdit boxes, it goes back to the original workbook opened first.
Here's another part I don't understand, when I load this in Excel 2010 it's flawless. I can select which workbook I want and click on the RefEdit and that workbook will remain activated.
Is there something I'm missing? Any tips and/or tricks that I did not think of?
Thank you
[delete , posted solution did not fix problem]
I have one open workbook with a button "SaveClose". I want the event on that button to close the entire Excel application if it is the last workbook opened. The thing is, there are possibility that there could be unknown personal.xlsb open or any other macro workbook open that is not visible.
I want to know if there is any other workable Excel workbook opened. Is there a check for a not visible workbook? If it is the last workbook, close Excel application, if not close active workbook, here's what I got:
Sub CloseForceSave()
'Save the workbook.
ThisWorkbook.Save
'If this is the only workbook opened in the current session, then...
If Workbooks.Count = 1 Then "or Workbooks.Count = 2" to account for personal.xlsb
'...quit Excel
Application.Quit
'Else...
Else
'...close the active workbook
ThisWorkbook.Close
End If
End Sub
When I do this I just ignore any hidden workbooks. If there is only one workbook with a visible window, I quit the application. I don't know of any better way of counting than a loop, but I've never had enough workbooks open for this to be a performance problem.
Function VisibleWorkbookCount() As Long
Dim wb As Workbook
Dim wd As Window
Dim lReturn As Long
For Each wb In Application.Workbooks
For Each wd In wb.Windows
If wd.Visible Then
lReturn = lReturn + 1
Exit For
End If
Next wd
Next wb
VisibleWorkbookCount = lReturn
End Function
if a workbook is not visible, it's usually a AddIn.
Try to check "Workbook.IsAddin Property"
see https://msdn.microsoft.com/de-de/library/office/ff838249.aspx
I have built an excel application(Feedback survey form like), where the user enters the information and then saves and uploads to database. Once the form is completed for a single account and then saved, I need to delete all the cell contents in the source workbook. But when I open the saved file again to check back for later reference, values should be retained instead of clearing when closed.
Any best way I can do this ?. Right now I have included a close button in my form, when clicked will delete the contents of original workbook. How can I do this task with the default close button of excel ?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim wb As Excel.Workbook
Set wb = ThisWorkbook
On Error GoTo NoBlanks
ActiveSheet.Range("A1").ClearContents
ActiveSheet.Range("D3:D8").ClearContents
ActiveSheet.Range("D12:E12").SpecialCells(xlConstants).ClearContents
ActiveSheet.Range("D16:F19").ClearContents
ActiveSheet.Range("D26:F29").ClearContents
ThisWorkbook.Sheets("Data_upload").Range("D39:D41").ClearContents
ActiveSheet.Range("A1").ClearContents
NoBlanks:
Resume Next
wb.Close (True)
wb.save
End Sub