Storyteller
Instructor
Hello All,
I've been working on the following and cannot figure out how to get the wkbook variable from the UserForm_Initialize() to be available in the cmdImport_Click() procedure.
This is the code I am working with:
Public Sub cmdImport_Click()
Dim i As Integer
Dim wkbook As Workbook
On Error Resume Next
For i = 0 To lstWorkSheets.ListCount - 1
If lstWorkSheets.Selected(i) = True Then
ActiveWorkbook.Worksheets(i + 1).Copy _
after:=Workbooks("Calculations").Sheets("StartingSheet")
End If
Next i
Workbooks(wkbook).Close 'This does not close
Unload Me
End Sub
Public Sub UserForm_Initialize()
Dim FileToOpen As String
Dim FileToUse As String
Dim wkbook As Workbook
Dim wksheet As Worksheet
FileToOpen = Application.GetOpenFilename("Excel (*.xls), *.xls")
Workbooks.Open Filename:=FileToOpen
FileToUse = Right(FileToOpen, Len(FileToOpen) _
- (InStrRev(FileToOpen, "\", -1)))
Set wkbook = Workbooks(FileToUse)
With Me.lstWorkSheets
For Each wksheet In wkbook.Worksheets
.AddItem (wksheet.Name)
Next
End With
End Sub
Everything works great except for the wkbook file does not close.
Any help would be greatly appreciated.
Regards,
Michael
I've been working on the following and cannot figure out how to get the wkbook variable from the UserForm_Initialize() to be available in the cmdImport_Click() procedure.
This is the code I am working with:
Public Sub cmdImport_Click()
Dim i As Integer
Dim wkbook As Workbook
On Error Resume Next
For i = 0 To lstWorkSheets.ListCount - 1
If lstWorkSheets.Selected(i) = True Then
ActiveWorkbook.Worksheets(i + 1).Copy _
after:=Workbooks("Calculations").Sheets("StartingSheet")
End If
Next i
Workbooks(wkbook).Close 'This does not close
Unload Me
End Sub
Public Sub UserForm_Initialize()
Dim FileToOpen As String
Dim FileToUse As String
Dim wkbook As Workbook
Dim wksheet As Worksheet
FileToOpen = Application.GetOpenFilename("Excel (*.xls), *.xls")
Workbooks.Open Filename:=FileToOpen
FileToUse = Right(FileToOpen, Len(FileToOpen) _
- (InStrRev(FileToOpen, "\", -1)))
Set wkbook = Workbooks(FileToUse)
With Me.lstWorkSheets
For Each wksheet In wkbook.Worksheets
.AddItem (wksheet.Name)
Next
End With
End Sub
Everything works great except for the wkbook file does not close.
Any help would be greatly appreciated.
Regards,
Michael