Seznam, změna nebo odstranění odkazů na externí vzorce (odkazy) pomocí VBA v aplikaci Microsoft Excel

Anonim

S níže uvedenými makry můžete najít a odstranit vzorce v buňkách, které odkazují na jiné sešity.
Makra nenajdou všechny externí odkazy, protože vypadají pouze ve vzorcích listu.

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 nebo vyjmenujte odkazy na externí vzorce ") Vyberte případ i Případ vbAno DeleteExternalFormulaReferences Případ vbNo ListExternalFormulaReferences Konec Vybrat Konec Sub Sub Sub DeleteExternalFormulaReferences () Dim ws jako list, AWS jako řetězec, ConfirmReplace jako Boolean Dim i jako celé číslo, OK jako Boolean Pokud ActiveWork Sub i = MsgBox („Potvrdit všechna nahrazení odkazů na externí vzorce hodnotami?“, _ VbQuestion + vbYesNoCancel, „Převést odkazy na externí vzorce“) ConfirmReplace = False If i = vbCancel Then Exit Sub If i = vbYes Then ConfirmReplace = True AWS = ActiveSheet.Name Application.ScreenUpdating = False For each ws In ActiveWorkbook.Worksheets OK = DeleteLinksInWS (ConfirmReplace, ws) If Not OK then Exit For Next ws Set ws = Nothing Sheets (A WS). Vyberte 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 If ws is nothing Then Exit Function Application. StatusBar = "Mazání odkazů na externí vzorce v" & _ ws.Name & "…" ws.Activate for each 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 ("Replace the formula with the value?", _ vbQuestion + vbYesNoCancel, _ "Nahradit odkaz na externí vzorec v" & _ cl.Address (False, False, xlA1) & _ "hodnotou buňky?") Application.ScreenUpdating = False If i = vbCancel Then DeleteLinksInWS = False Exit Function End If If i = vbYes Then On Error Resume Next 'v případě, že je list chráněn cl.Formula = cl.Hodnota 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 Sub ListExternalFormulaReferences () Dim ws As Worksheet, TargetWS As Worksheet, SourceWB As Workbook If ActiveWorkbook is nothing then Exit Sub Application.ScreenUpdating = False With ActiveWorkbook On Error Resume Next Set TargetWS = .Worksheets.Add (Before: =. Worksheets (1)) If TargetWS Is Nothing Then 'workbook is protected Set SourceWB = ActiveWorkbook Set TargetWS = Workbooks.Add.Worksheets (1) SourceWB.Activate Nastavte SourceWB = Nic nekončí, pokud 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"). Automatické přizpůsobení při chybě Pokračovat Další. Název = "Seznam odkazů" Při chybě GoTo 0 Konec nastaveným cílem TargetWS = Nic 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 If ws Is Nothing Then Exit Sub If TargetWS Is Nothing Then Exit Sub Application.StatusBar = "Finding external odkazy na vzorce v "& _ ws.Name &" … "Pro každý cl In ws.UsedRange cFormula = cl.Formula If Len (cFormula)> 0 Then If Left $ (cFormula, 1) =" = "Then If InStr (cFormula , "[")> 1 Potom s TargetWS tRow = .Range ("A" & .Rows.Count) .End (xlUp) .Row + 1 .Range ("A" & tRow) .Formula = tRow - 1 .Range ("B" & tRow). Vzorec = ws.Název & "!" & _ 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