首页 > 解决方案 > 查找并替换“=”为“=”直到最后一个工作表

问题描述

我正在使用根据字体颜色对值求和的代码。它工作得很好,但由于某种原因,所有方程都会随机变为#N/A,然后我必须进行查找并将“=”替换为“=”以恢复总和。我与我的主管共享文件,我想找到一种方法来防止#N/A 替换公式,或者有一个代码允许我创建一个按钮来运行查找和替换以恢复总和。当我使用查找和替换代码时,它会无限循环。有没有办法让它在最后一个工作表上结束?或者一次只浏览工作簿一次。

这是我正在使用的总和代码:

Function SumCellsByFontColor(rData As Range, cellRefColor As Range)
    Dim indRefColor As Long
    Dim cellCurrent As Range
    Dim sumRes
    Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
 
    Application.Volatile
    sumRes = 0
    indRefColor = cellRefColor.Cells(1, 1).Font.Color
    For Each cellCurrent In rData
        If indRefColor = cellCurrent.Font.Color Then
            sumRes = WorksheetFunction.Sum(cellCurrent, sumRes)
        End If
    Next cellCurrent
 
    SumCellsByFontColor = sumRes
    Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
  End Function
  Sub Sumcellsbyfont()

  End Sub

这是我一直在尝试的查找和替换代码:

 Sub FindReplaceAll()

  Dim sht As Worksheet
  Dim fnd As Variant
  Dim rplc As Variant

   fnd = "="
rplc = "="

For Each sht In ActiveWorkbook.Worksheets
     sht.Cells.Replace what:=fnd, Replacement:=rplc, _
     LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
    SearchFormat:=False, ReplaceFormat:=False

   Next sht

   End Sub

我是使用 VBA 的新手,但我希望通过字体颜色求和来完成这项工作,这样可以省去在进行更改时必须从常规求和公式中排除值的麻烦。我正在使用每个工作簿大约 15 张纸。

非常感谢任何帮助。

标签: excelvbaloopssum

解决方案


推荐阅读