Elenca, modifica o elimina i riferimenti a formule esterne (collegamenti) utilizzando VBA in Microsoft Excel

Sommario

Con le macro seguenti puoi trovare ed eliminare le formule nelle celle che fanno riferimento ad altre cartelle di lavoro.
Le macro non trovano tutti i riferimenti esterni poiché cercano solo nelle formule del foglio di lavoro.

Sub DeleteOrListLinks() Dim i As Integer If ActiveWorkbook Is Nothing Then Exit Sub i = MsgBox("YES: Delete external formula references" & Chr(13) & _ "NO: List external formula references", _ vbQuestion + vbYesNoCancel, "Delete o elenca i riferimenti a formule esterne") Select Case i Case vbYes DeleteExternalFormulaReferences Case vbNo ListExternalFormulaReferences End Select End Sub DeleteExternalFormulaReferences() Dim ws As Worksheet, AWS As String, ConfirmReplace As Boolean Dim i As Integer, OK As Boolean Se ActiveWorkbook non è nulla, quindi esci Sub i = MsgBox("Conferma tutte le sostituzioni di riferimenti a formule esterne con valori?", _ vbQuestion + vbYesNoCancel, "Converti riferimenti a formule esterne") ConfirmReplace = False If i = vbCancel Then Exit Sub If i = vbYes Then ConfirmReplace = True AWS = ActiveSheet.Name Application.ScreenUpdating = False per ogni ws in ActiveWorkbook.Worksheets OK = DeleteLinksInWS(ConfirmReplace, ws) Se non va bene, quindi esci per ws successivo Set ws = Nothing Sheets(A WS).Select Application.ScreenUpdating = True End Sub Private Function DeleteLinksInWS(ConfirmReplace As Boolean, _ ws As Worksheet) As Boolean Dim cl As Range, cFormula As String, i As Integer DeleteLinksInWS = True Se ws non è nulla, quindi esci dall'applicazione della funzione. StatusBar = "Eliminazione dei riferimenti a formule esterne in " & _ ws.Name & "… " ws.Activate per ogni cl In ws.UsedRange cFormula = cl.Formula If Len(cFormula) > 0 Then If Left$(cFormula, 1) = "=" Then If InStr(cFormula, "[") > 1 Then If Not ConfirmReplace Then cl.Formula = cl.Value Else Application.ScreenUpdating = True cl.Select i = MsgBox("Sostituisci la formula con il valore?", _ vbQuestion + vbYesNoCancel, _ "Sostituisci il riferimento alla formula esterna in " & _ cl.Address(False, False, xlA1) & _ " con il valore della cella?") Application.ScreenUpdating = False If i = vbCancel Then DeleteLinksInWS = False Exit Function End If i = vbYes Then On Error Resume Next ' nel caso in cui il foglio di lavoro sia protetto cl.Formula = cl.Value On Error GoTo 0 End If E nd If End If End If End If Next cl Set cl = Nothing Application.StatusBar = False End Function Sub ListExternalFormulaReferences() Dim ws come foglio di lavoro, TargetWS come foglio di lavoro, SourceWB come cartella di lavoro se ActiveWorkbook non è nulla, quindi esci da Sub Application.ScreenUpdating = False With ActiveWorkbook On Error Riprendi successivo Imposta TargetWS = .Worksheets.Add(Before:=.Worksheets(1)) Se TargetWS non è nulla Allora ' la cartella di lavoro è protetta Imposta SourceWB = ActiveWorkbook Imposta TargetWS = Workbooks.Add.Worksheets(1) SourceWB.Activate Imposta SourceWB = Nothing End If With TargetWS .Range("A1").Formula = "Sequence" .Range("B1").Formula = "Cell" .Range("C1").Formula = "Formula" .Range( "A1:C1").Font.Bold = True End With For Each ws In .Worksheets If Not ws Is TargetWS Then ListLinksInWS ws, TargetWS End If Next ws Set ws = Nothing End With With TargetWS .Parent.Activate .Activate .Columns ("A:C").AutoFit On Error Resume Next .Name = "Link List" On Error GoTo 0 End With Set TargetWS = Nothing Application.ScreenUpdati ng = True End Sub Private Sub ListLinksInWS(ws As Worksheet, TargetWS As Worksheet) Dim cl As Range, cFormula As String, tRow As Long Se ws non è nulla, quindi esci da Sub se TargetWS non è nulla, quindi esci da Sub Application.StatusBar = "Finding external riferimenti alla formula in " & _ ws.Name & "… " Per ogni cl In ws.UsedRange cFormula = cl.Formula If Len(cFormula) > 0 Then If Left$(cFormula, 1) = "=" Then If InStr(cFormula , "[") > 1 Quindi con TargetWS tRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1 .Range("A" & tRow).Formula = tRow - 1 .Range ("B" & tRow).Formula = ws.Name & "!" & _ cl.Address(False, False, xlA1) .Range("C" & tRow).Formula = "'" & cFormula End With End If End If End If Next cl Set cl = Nothing Application.StatusBar = False End Sub

Aiuterete lo sviluppo del sito, condividere la pagina con i tuoi amici

wave wave wave wave wave