Sub BatchReplaceWorkbookPath() ' 使用前请先备份报表! ' 使用前请先备份报表! ' 使用前请先备份报表! Dim ws As Worksheet, cell As Range Dim oldrep1$, oldrep2$, oldrep3$, cf$, newrep1$, newrep2$, newrep3$
oldrep1 = "Excel批量修改源文件链接(替换多次)\6月指标" ' 被替换字符1 newrep1 = "7月指标" ' 替换后字符1 oldrep2 = "6月主要经济指标" ' 被替换字符2 newrep2 = "7月主要经济指标" ' 替换后字符2 oldrep3 = "" ' 被替换字符3 newrep3 = "" ' 替换后字符3 Application.DisplayAlerts = False ' 禁止显示警告框 For Each ws In ActiveWorkbook.Worksheets ' 循环处理工作簿的所有工作表 For Each cell In ws.UsedRange ' 遍历工作表中的所有单元格 cf = cell.Formula If InStr(cf, oldrep1) > 0 Or InStr(cf, oldrep2) > 0 Or InStr(cf, oldrep3) > 0 Then ' 检查单元格中的公式链接是否包含需替换的字符 cell.Formula = Replace(cell.Formula, oldrep1, newrep1) ' 替换第1个字符 cell.Formula = Replace(cell.Formula, oldrep2, newrep2) ' 替换第2个字符 cell.Formula = Replace(cell.Formula, oldrep3, newrep3) ' 替换第3个字符 End If Next cell Next ws Application.DisplayAlerts = True ' 恢复显示警告框 End Sub