Ich habe in Spalte F die Namen von mehreren Arbeitsmappenverbindungen stehen.
Diese möchte ich nun per Makro löschen.
Kann mir jemand erklären, wie das geht?
Vielen Dank und viel Grüsse
Benjamin
Sub RemoveConnections()
'Löscht die Verbindungen/Connections, deren Namen in einem Zellbereich stehen
Dim rngData As Range, rngZelle As Range
Dim wkbAktiv As Workbook
Dim strMsgText As String, strMsgTitel As String, lngMsgButtons As Long
strMsgTitel = "Makro: Remove Connections"
lngMsgButtons = vbInformation + vbOKOnly
On Error GoTo Fehler
Set wkbAktiv = ActiveWorkbook
If wkbAktiv.Connections.Count > 0 Then
With ActiveSheet
'Bereich mit Verbindungsnamen F2:Fxxx
Set rngData = .Range(.Cells(2, 6), .Cells(.Rows.Count, 6).End(xlUp))
If rngData.Row >= 2 Then
For Each rngZelle In rngData
If rngZelle "" Then
wkbAktiv.Connections(rngZelle.Text).Delete
End If
Next
Else
strMsgText = "Keine Verbindungseinträge im Bereich " & rngData.Address
MsgBox strMsgText, lngMsgButtons, strMsgTitel
End If
End With
Else
strMsgText = "Aktive Datei hat keine Verbindungen"
MsgBox strMsgText, lngMsgButtons, strMsgTitel
End If
Fehler:
lngMsgButtons = vbInformation + vbOKOnly
With Err
Select Case .Number
Case 0 'alles OK
Case 9 'Index-Fehler in Auflisttung - Name ist falsch
lngMsgButtons = vbInformation + vbRetryCancel
strMsgText = "Verbindung """ & rngZelle.Text & """ existiert nicht!"
If MsgBox(strMsgText, lngMsgButtons, strMsgTitel) = vbRetry Then
Resume Next
End If
Case Else
strMsgText = "Fehler-Nr.: " & .Number & vbLf & .Description
MsgBox strMsgText, lngMsgButtons, strMsgTitel
End Select
End With
End Sub
Sub RemoveConnections_kurz()
'Löscht die Verbindungen/Connections, deren Namen in einem Zellbereich stehen
Dim rngData As Range, rngZelle As Range
Dim wkbAktiv As Workbook
On Error Resume Next
Set wkbAktiv = ActiveWorkbook
If wkbAktiv.Connections.Count > 0 Then
With ActiveSheet
'Bereich mit Verbindungsnamen F2:Fxxx
Set rngData = .Range(.Cells(2, 6), .Cells(.Rows.Count, 6).End(xlUp))
If rngData.Row >= 2 Then
For Each rngZelle In rngData
If rngZelle "" Then
wkbAktiv.Connections(rngZelle.Text).Delete
End If
Next
End If
End With
End If
End Sub
'Bereich mit Verbindungsnamen F2:Fxxx
Set rngData = .Range(.Cells(1, 7), .Cells(.Rows.Count, 7).End(xlUp))
If rngData.Row >= 1 Then
For Each rngZelle In rngData
If rngZelle "" Then
wkbAktiv.Connections(rngZelle.Text).Delete Indexfehler '9', Index außerhalb _
des geültigen Bereichs.
End If
Next
Else
Es scheint so, als würde er dann Deine "On Error" nicht aufrufen, oder?Sub RemoveConnections()
'Löscht die Verbindungen/Connections, deren Namen in einem Zellbereich stehen
Dim rngData As Range, rngZelle As Range, varElement As Variant, bolDeleted As Boolean
Dim wkbAktiv As Workbook
Dim strMsgText As String, strMsgTitel As String, lngMsgButtons As Long
strMsgTitel = "Makro: Remove Connections"
lngMsgButtons = vbInformation + vbOKOnly
On Error GoTo Fehler
Set wkbAktiv = ActiveWorkbook
If wkbAktiv.Connections.Count > 0 Then
With ActiveSheet
'Bereich mit Verbindungsnamen F2:Fxxx
Set rngData = .Range(.Cells(2, 6), .Cells(.Rows.Count, 6).End(xlUp))
If rngData.Row >= 2 Then
For Each rngZelle In rngData
If rngZelle "" Then
For Each varElement In wkbAktiv.Connections
If LCase(varElement.Name) = LCase(rngZelle.Text) Then
varElement.Delete
bolDeleted = True
Exit For
End If
Next varElement
If bolDeleted = False Then
lngMsgButtons = vbInformation + vbRetryCancel
strMsgText = "Verbindung """ & rngZelle.Text & """ existiert nicht!"
If MsgBox(strMsgText, lngMsgButtons, strMsgTitel) = vbCancel Then Exit For
End If
End If
Next
Else
strMsgText = "Keine Verbindungseinträge im Bereich " & rngData.Address
MsgBox strMsgText, lngMsgButtons, strMsgTitel
End If
End With
Else
strMsgText = "Aktive Datei hat keine Verbindungen"
MsgBox strMsgText, lngMsgButtons, strMsgTitel
End If
Fehler:
lngMsgButtons = vbInformation + vbOKOnly
With Err
Select Case .Number
Case 0 'alles OK
Case 9 'Index-Fehler in Auflisttung - Name ist falsch
lngMsgButtons = vbInformation + vbRetryCancel
strMsgText = "Verbindung """ & rngZelle.Text & """ existiert nicht!"
If MsgBox(strMsgText, lngMsgButtons, strMsgTitel) = vbRetry Then
Resume Next
End If
Case Else
strMsgText = "Fehler-Nr.: " & .Number & vbLf & .Description
MsgBox strMsgText, lngMsgButtons, strMsgTitel
End Select
End With
End Sub
Sub RemoveConnections_kurz()
'Löscht die Verbindungen/Connections, deren Namen in einem Zellbereich stehen
Dim rngData As Range, rngZelle As Range, varElement As Variant
Dim wkbAktiv As Workbook
On Error Resume Next
Set wkbAktiv = ActiveWorkbook
If wkbAktiv.Connections.Count > 0 Then
With ActiveSheet
'Bereich mit Verbindungsnamen F2:Fxxx
Set rngData = .Range(.Cells(2, 6), .Cells(.Rows.Count, 6).End(xlUp))
If rngData.Row >= 2 Then
For Each rngZelle In rngData
If rngZelle "" Then
For Each varElement In wkbAktiv.Connections
If LCase(varElement.Name) = LCase(rngZelle.Text) Then
varElement.Delete
Exit For
End If
Next varElement
End If
Next
End If
End With
End If
End Sub