PDA

View Full Version : [SOLVED:] Pick and Chose which Workbook(s) to Close from a UserForm



rorobear
08-19-2021, 09:51 AM
Hello Everyone,

Wondering if i could get a little help. I'd like to expand the code below. If i have multiple workbooks open (which i'm opening from a userform), I want to be able to chose with workbook to close. I don't want to just close the active book or close all books. I want to modify the code so I get some sort of promt (msgbox maybe) to allow me to chose which workbook I want to close. If anyone can help, I'd be most grateful.

rb


Option Explicit

Sub Close_WorkbookAndSave()


ActiveWorkbook.Close savechanges:=True


End Sub

jolivanes
08-19-2021, 04:51 PM
See attached



Private Sub UserForm_Initialize()
Dim j As Long
For j = 1 To Workbooks.Count
If Workbooks(j).Name <> ThisWorkbook.Name And Workbooks(j).Name <> "PERSONAL.XLSB" Then _
ListBox1.AddItem Workbooks(j).Name
Next j
End Sub

Private Sub ListBox1_Click()
Dim j As Long, wb As Workbook, ListIndex As Long
Application.ScreenUpdating = False
Set wb = Workbooks(ListBox1.Value)
wb.Close True
ListBox1.Clear
UserForm_Initialize
Application.ScreenUpdating = True
End Sub



Private Sub Cmd_Exit_Click()
Unload Me
End Sub

rorobear
08-19-2021, 05:15 PM
jolivanes,

This is Brilliant!!! let me see if I can incorporate this into my project. I'll be back should I have any questions or to thank you once again if I can make it work.

cheers

jolivanes
08-20-2021, 02:11 PM
Thanks for letting us know.
Good Luck

If it will be part of a bigger UserForm_Initialize sub, the "UserForm_Initialize line in the other code might have to be changed.

rorobear
08-20-2021, 03:46 PM
Yes, it is part of a bigger project. I'm building a Worksheet Manager for navigating workbooks with multiple sheets. I'm including what I have done so far if anyone is interest. thank you again.