1

我已将工作表复制到一批工作簿中(大约 45 个)。

此工作表包含公式和参考,即=Sheetname!A1=IF(Sheetname!A2=1, "this", "that").

但是,当工作表被复制后,这些公式就变成了

=[Original_Book.xlsm]Sheetname!$A$1

=IF([Original_Book.xlsm]Sheetname!A2=1, "this", "that").

我想要一种删除所有“[Original_Book.xlsm]”引用的方法,以便公式再次变为本地。

实际示例(以上仅用于说明目的

=[Survey_Checker.xlsm]Output!D42

应该成为

=Output!D42

同样地:

=INDEX([Survey_Checker.xlsm]QR1!E3:[Survey_Checker.xlsm]QR1!E9,[Survey_Checker.xlsm]Output!$G$17,0)

应该成为

=INDEX(QR1!E3:QR1!E9,Output!$G$17,0)

等等。

事实上,如果有一个 VBA 方法可以从每个文件的单元格范围 A1:PE5 中的工作表(称为“DB 输出”)中删除字符串“[Survey_Checker.xlsm]”的每个实例,我相信这可能工作?

编辑:A1 和 PE5 之间的每一列都有一个需要删除的参考论坛。

4

1 回答 1

5

这是原始宏的一个版本,添加了修复公式的代码。

Sub Example()
    Dim path As String
    Dim file As String
    Dim wkbk As Workbook
    Dim rCell As Range

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False


    path = "C:\Test\"
    file = Dir(path)

    Do While Not file = ""
        Workbooks.Open (path & file)
        Set wkbk = ActiveWorkbook
        Sheets.Add After:=Sheets(Sheets.Count)
        On Error GoTo Sheet_Exists
        ActiveSheet.Name = "DB Output"
        On Error GoTo 0
        ThisWorkbook.Sheets("DB Output").Range("A1:PE5").Copy Destination:=wkbk.Sheets("DB Output").Range("A1")

        For Each rCell In wkbk.Sheets("DB Output").UsedRange
            If InStr(rCell.Formula, ThisWorkbook.Name) > 0 Then
                rCell.Replace What:="[*]", Replacement:=""
            End If
        Next

        wkbk.Save
        wkbk.Close
        file = Dir
    Loop

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Exit Sub

Sheet_Exists:
    Sheets("DB Output").Delete
    Resume
End Sub

这是一个宏,它将简单地从公式中删除工作簿引用

Sub Example()
    Dim path As String
    Dim file As String
    Dim rCell As Range

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False


    path = "C:\Test\"
    file = Dir(path)

    Do While Not file = ""
        Workbooks.Open (path & file)

        Sheets("DB Output").Select

        For Each rCell In ActiveWorkbook.Sheets("DB Output").UsedRange
            rCell.Replace What:="[*]", Replacement:=""
        Next

        ActiveWorkbook.Save
        ActiveWorkbook.Close
        file = Dir
    Loop

    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
End Sub
于 2013-06-11T01:23:58.137 回答