'File picker. Import from another workbook's sheet
I'm trying to create file picker to import sheet from another workbook. Workbook can be any path. Here is the code but something going wrong with file name. If anybody solved it before please help what's wrong here
Sub Main()
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Dim vrtSelectedItem As Variant
With fd
.AllowMultiSelect = True
If .Show = -1 Then
For Each vrtSelectedItem In .SelectedItems
Application.ScreenUpdating = False
Dim path As String
Dim file_name As String
Workbooks.Open Filename:=path & file_name
Set closedBook = Workbooks.Open("File_name As String")
closedBook.Sheets("INPUT").Copy Before:=ThisWorkbook.Sheets(1)
closedBook.Close SaveChanges:=False
Application.ScreenUpdating = True
Worksheets("INPUT (2)").Visible = False
Next
Else
End If
End With
Set fd = Nothing
End Sub
Solution 1:[1]
This is modified snippet from something I've used in the past. I'm not certain if FileDialogue works the same, but it's likely somewhat similar, so could probably be modified to work with it instead. Hopefully it helps.
Sub Main()
Dim dataFiles As Variant
Dim dataWorkbook As Workbook
Dim fileCount As Integer
dataFiles = Application.GetOpenFilename(MultiSelect:=True)
If IsArray(dataFiles) Then
For fileCount = LBound(dataFiles) To UBound(dataFiles)
Set dataWorkbook = Workbooks.Open(dataFiles(fileCount))
'whatever you want to do here
dataWorkbook.Close SaveChanges:=False
Next fileCount
Else: Exit Sub
End If
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Basbadger |
