I need to open a "select file" dialog then copy a sheet (always same name, same position) from the selected file into the second sheet in the current Excel file.
So far the I can choose and open the file. That is where it stops. Nothing is copied and nothing is in my clipboard.
I searched and found a few not working solutions.
Here is the code:
Public Sub Import()
Dim VarDateiPfad As Variant
Dim Source As Workbook
Dim FilterDestination As Workbook
'You choose the starting Folder for the "Select File" Dialog
ChDrive ("X:\") 'hier Laufwerk angeben
ChDir ("X:\X....") ' hier exakten Pfad angeben
'Starts the dialog and saves the link to the file
VarDateiPfad = Application.GetOpenFilename("Exceldateien,*.xls*", 1)
Workbooks.Open Filename:=VarDateiPfad, ReadOnly:=False
'Here seems to be where the problems occur
ActiveWorkbook.Sheets("X").UsedRange.Copy
ThisWorkbook.Sheets("Import").PasteSpecial xlValues
ActiveWorkbook.Close
This here was my last Version before fixing the issue:
Public Sub Import()
Dim VarDateiPfad As String
Dim FilterSource As Workbook
Dim FilterDestination As Workbook
Set FilterDestination = ActiveWorkbook
'Legt Standard Verzeichnis des "Datei öffnen" Dialogs fest.
ChDrive ("X") 'hier Laufwerk angeben
ChDir ("X") ' hier exakten Pfad angeben
'Startet "Import" Dialog und legt ausgewählte Datei in "VarDateiPfad" ab
VarDateiPfad = Application.GetOpenFilename("Exceldateien,*.xls*", 1, "X")
Set FilterSource = Workbooks.Open(Filename:=VarDateiPfad, ReadOnly:=False)
FilterSource.Worksheets("X").UsedRange.Copy
FilterDestination.Worksheets("Import").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Source.Close