FileOpenDialog liefert keinen richtigen Dateipfad
01.11.2023 13:15:57
Hauke7
Private Sub cmdStartDataImport_Click()
Dim filePath_CT As Variant
Dim filePath_dRN As Variant
Dim fileDialog As fileDialog
Dim wsTaqmanOptions As Worksheet
Dim Modul As Variant
Dim wbSource As Workbook
Dim wsSource As Worksheet
Dim wsDestination As Worksheet
' Import CT-Datei
' Definiere die Dateipfade
Modul = Right([frmModul], 6)
If [frmPrefixDatei1] = 0 Then
filePath_CT = "C:\Users\NP40\Downloads\" & [frmLaufNr] & "_" & Modul & "_" & [frmInstrument] & [frmDateiendung]
Else
filePath_CT = "C:\Users\NP40\Downloads\" & [frmPrefixDatei1] & [frmLaufNr] & "_" & Modul & "_" & [frmInstrument] & [frmDateiendung]
End If
' Datei manuell auswählen wenn diese nicht existiert
If Dir(filePath_CT) = "" Then
MsgBox "Die CT-Rohdaten wurden nicht gefunden und müssen manuell importiert werden."
Set fileDialog = Application.fileDialog(msoFileDialogFilePicker) ' Erstellt ein FileDialog-Objekt
fileDialog.Filters.Clear
fileDialog.Filters.Add "", "*" & [frmDateiendung]
fileDialog.Filters.Add "Alle Dateien", "*.*"
fileDialog.InitialFileName = "C:\Users\NP40\Downloads\*" & [frmLaufNr] & "*" ' Filter auf Dateien mit LaufNr und Dateiendung"
filePath_CT = fileDialog.Show ' Öffnet den Dateiauswahldialog
End If
' Öffnet die ausgewählte Datei (CT-Datei)
Set wbSource = Workbooks.Open(filePath_CT)
Set wsSource = wbSource.Sheets(1) ' Annahme: Daten befinden sich im ersten Arbeitsblatt
' Kopiert die Daten aus der Quelldatei in die aktuelle Datei (CT-Datei)
Set wsDestination = ThisWorkbook.Sheets("CTReport") ' Definiert Ziel-Tabellenblatt
wsDestination.Unprotect "xxx" ' Schreibschutz aufheben
wsDestination.Cells.Clear
wsSource.UsedRange.Copy wsDestination.Cells(1, 1) ' Fügt die Daten ab Zelle A1 ein
wsDestination.Protect "xxx" ' Schreibschutz wieder setzen
' Schließt die Quelldatei ohne zu speichern
wbSource.Close False
'Zurück zu Tabellenblatt Evaluation wechseln
Sheets("Evaluation").Activate
End Sub