Liebe VBA-Profis,
ich möchte über VBA mehere Excel-Dateien öffnen, wenn diese noch nicht geöffnet sind. Dazu habe ich für jede Datei einige Befehlsteilen geschreiben.
Es macht aber doch Sinn, dies über eine Schleife etc. zu machen. Allerdings habe ich keine idee wie. Vielleicht könnt ihr mir helfen?
Das ist mein CODE
Vielen Dank schon einmal für eure Hilfe!
Sub CONT_Verknuepfte_Dateien_oeffnen()
Dim Pfad_EW_PK As String
Dim Pfad_EW_FK As String
Dim Pfad_ME_PK As String
Dim Pfad_ME_FK As String
Dim Dateiname As String
Pfad_EW_PK = Sheets(CON_Tabellenblatt).Range(CON_Pfad_EW_PK).Value
Pfad_EW_FK = Sheets(CON_Tabellenblatt).Range(CON_Pfad_EW_FK).Value
Pfad_ME_PK = Sheets(CON_Tabellenblatt).Range(CON_Pfad_ME_PK).Value
Pfad_ME_FK = Sheets(CON_Tabellenblatt).Range(CON_Pfad_ME_FK).Value
Dateiname = Sheets(CON_Tabellenblatt).Range(CON_Dateiname).Value
On Error GoTo CONT_Verknuepfte_Dateien_oeffnen_Err
'Prüfung, ob Datei bereits geöffnet ist
If EW_PK_Exists(Pfad_EW_PK) = False Then
If Dir(Pfad_EW_PK) = "" Then
MsgBox "Datei " & Pfad_EW_PK & " wurde nicht gefunden!"
Else
Workbooks.Open Pfad_EW_PK
End If
End If
'Prüfung, ob Datei bereits geöffnet ist
If EW_PK_Exists(Pfad_EW_FK) = False Then
If Dir(Pfad_EW_FK) = "" Then
MsgBox "Datei " & Pfad_EW_FK & " wurde nicht gefunden!"
Else
Workbooks.Open Pfad_EW_FK
End If
End If
'Prüfung, ob Datei bereits geöffnet ist
If ME_PK_Exists(Pfad_ME_PK) = False Then
If Dir(Pfad_ME_PK) = "" Then
MsgBox "Datei " & Pfad_ME_PK & " wurde nicht gefunden!"
Else
Workbooks.Open Pfad_ME_PK
End If
End If
'Prüfung, ob Datei bereits geöffnet ist
If ME_PK_Exists(Pfad_ME_FK) = False Then
If Dir(Pfad_ME_FK) = "" Then
MsgBox "Datei " & Pfad_ME_FK & " wurde nicht gefunden!"
Else
Workbooks.Open Pfad_ME_FK
End If
End If
'Fenster Ablaufplan wieder aktivieren
Windows(Dateiname).Activate
Range("C38").Select
CONT_Verknuepfte_Dateien_oeffnen_Exit:
Exit Sub
CONT_Verknuepfte_Dateien_oeffnen_Err:
MsgBox Error$
Resume CONT_Verknuepfte_Dateien_oeffnen_Exit
Resume Next
End Sub
Private Function EW_PK_Exists(sFile As String) As Boolean
Dim Pfad_EW_PK As String
Dim wkb As Object
Pfad_EW_PK = Sheets(CON_Tabellenblatt).Range(CON_Pfad_EW_PK).Value
On Error Resume Next
Set wkb = Workbooks(Pfad_EW_PK)
If Not wkb Is Nothing Then
EW_PK_Exists = True
End If
On Error GoTo 0
End Function
Private Function EW_FK_Exists(sFile As String) As Boolean
Dim wkb As Object
On Error Resume Next
Set wkb = Workbooks(Pfad_EW_FK)
If Not wkb Is Nothing Then
EW_FK_Exists = True
End If
On Error GoTo 0
End Function
Private Function ME_PK_Exists(sFile As String) As Boolean
Dim wkb As Object
On Error Resume Next
Set wkb = Workbooks(Pfad_ME_PK)
If Not wkb Is Nothing Then
ME_PK_Exists = True
End If
On Error GoTo 0
End Function
Private Function ME_FK_Exists(sFile As String) As Boolean
Dim wkb As Object
On Error Resume Next
Set wkb = Workbooks(Pfad_ME_FK)
If Not wkb Is Nothing Then
ME_FK_Exists = True
End If
On Error GoTo 0
End Function
|