breynolds0431
Active Member
- Joined
- Feb 15, 2013
- Messages
- 303
- Office Version
- 365
- 2016
- Platform
- Windows
Hello. I have a userform where a user will select an external workbook that will then import the sheet to the current workbook. The issue that arises is when the external workbook has more than one sheet. So, my plan was to have another userform pop up when the external workbook's sheet count is greater than 1 that would allow the user to select the intended sheet.
I have this part in the import file userform:
For the ufTabSelect, I have the following, where cboSheets is the combobox. The combobox properly lists out the ImportWB's sheets. And there's a Done button to unload the ufTabSelect (Me).
The problem is that the code from the first userform doesn't stop and wait for the user to select the sheets before going to wbImportWB.Close. So, what happens is the CY sheet is created, but it's obviously blank. I'm guessing it's reading impSheet as sheet index 0, which is odd that an error doesn't result. Not sure at this point. Any insightful tips would be greatly appreciated.
I have this part in the import file userform:
VBA Code:
Dim wbThisWB As Workbook: Set wbThisWB = ThisWorkbook
Dim wbImportWB As Workbook
Dim strFullPath As String
Dim lngLastRow As Long
Dim lngLastCol As Long
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Title = "Select the listing"
.Filters.Add "Excel and CSV files", "*.csv; *.xls; *.xls*", 1
.Show
On Error Resume Next
strFullPath = .SelectedItems(1)
If Err.Number <> 0 Then
wbThisWB = Nothing
Application.ScreenUpdating = True
Exit Sub 'Error has occurred so quit
End If
On Error GoTo 0
End With
Dim fileName As String
fileName = strFullPath
'Will check if report is already opened to avoid run-time error.
If IsFileOpen(fileName) = False Then
Set wbImportWB = Workbooks.Open(strFullPath)
Else
MsgBox "The CY listing is open on your computer. Please close and retry import.", vbInformation, "CY Listing Open"
Me.lblCYProcess.Visible = False
Application.ScreenUpdating = True
Unload Me
Exit Sub
End If
'sets workbook name to info tab in order for ufTabSelect to know ImportWB name
wbThisWB.Sheets("Info").Range("CYwbName").Value = wbImportWB.Name
Dim impSheet As Integer
'Checks to ensure there's only one sheet in ImportWB
If wbImportWB.Sheets.count > 1 Then
ufTabSelect.Show
' add variable here
impSheet = ufTabSelect.cboSheets.ListIndex
Else
impSheet = 1
End If
'Checks if CY tab in wbThisWB already exists. If not, it will be created/added.
wbThisWB.Activate
Dim I, sheet_exists As Integer
sheet_exists = 0
For I = 1 To Sheets.count
If Sheets(I).Visible = -1 Then
If Sheets(I).Name = "CY" Then
sheet_exists = 1
End If
End If
Next
If sheet_exists = 0 Then
Sheets.Add(After:=Sheets(Sheets.count)).Name = "CY"
Else
On Error Resume Next
Application.DisplayAlerts = False
ThisWorkbook.Sheets("CY").Delete
On Error GoTo 0
Application.DisplayAlerts = True
Sheets.Add(After:=Sheets(Sheets.count)).Name = "CY"
End If
wbThisWB.Sheets("CY").Select
On Error Resume Next
With wbImportWB.Sheets(impSheet)
lngLastRow = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
lngLastCol = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
If lngLastRow > 0 And lngLastCol > 0 Then
Range(.Cells(1, 1), .Cells(lngLastRow, lngLastCol)).Copy wbThisWB.Sheets("CY").Cells(1, 1)
End If
End With
On Error GoTo 0
wbImportWB.Close False
End Sub
For the ufTabSelect, I have the following, where cboSheets is the combobox. The combobox properly lists out the ImportWB's sheets. And there's a Done button to unload the ufTabSelect (Me).
VBA Code:
Private Sub UserForm_Initialize()
With Me
.StartUpPosition = 0
.Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
.Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
End With
Dim I As Long
Dim wbName As String: wbName = ThisWorkbook.Sheets("Info").Range("CYwbName")
Me.cboSheets.Clear
With Workbooks(wbName)
For I = 1 To .Sheets.count
Me.cboSheets.AddItem .Sheets(I).Name
Next
'Me.cboSheets.Value = ActiveSheet.Name
End With
End Sub
The problem is that the code from the first userform doesn't stop and wait for the user to select the sheets before going to wbImportWB.Close. So, what happens is the CY sheet is created, but it's obviously blank. I'm guessing it's reading impSheet as sheet index 0, which is odd that an error doesn't result. Not sure at this point. Any insightful tips would be greatly appreciated.