Excel – 使用组合(值和格式)条件查找和replace
我想清空并将单元格格式设置为General,以便表单中的所有单元格:
- 包含“MyText”和
- 被格式化为百分比
我正在使用查找和replace对话框(CTRL + H),但是:
- 如果我将“replace为”框留空,则只更换格式; “MyText”还在;
- 如果我在“replace为”框中写入“SomeText”,则还会执行文本replace以及格式replace
它看起来像我一次不能得到两个replace(空文本和更改格式)。
有没有通配符可用于“replace为:”字段? 或者任何其他简单的解决scheme呢?
更新:我试图录制一个macros。 这是文本和格式replace:
Application.FindFormat.NumberFormat = "0.00%" Application.ReplaceFormat.NumberFormat = "General" Cells.Replace What:="MyText", Replacement:="nothing", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=True, _ ReplaceFormat:=True
但如果我删除replace文本,它只会做格式replace(这是有道理的一点,但这不是我想要实现的)
Application.FindFormat.NumberFormat = "0.00%" Application.ReplaceFormat.NumberFormat = "General" Cells.Replace What:="MyText", Replacement:="", LookAt:=xlPart, _ SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=True, _ ReplaceFormat:=True
也许一个简单的循环可能会更容易:
Sub FixingData() Dim r As Range For Each r In ActiveSheet.Cells.SpecialCells(xlCellTypeConstants) With r If .Value = "MyText" Then If .NumberFormat = "0.00%" Then .ClearContents .NumberFormat = "General" End If End If End With Next r End Sub
编辑#1:
这个版本可能会快一点:
Sub FixingDataFast() Dim r As Range, rUnion As Range Dim calcM Set rUnion = Nothing calcM = Application.Calculation Application.ScreenUpdating = False Application.Calculation = xlCalculationManual For Each r In ActiveSheet.Cells.SpecialCells(xlCellTypeConstants) If r.Value = "MyText" Then If r.NumberFormat = "0.00%" Then If rUnion Is Nothing Then Set rUnion = r Else Set rUnion = Union(rUnion, r) End If End If End If Next r If rUnion Is Nothing Then Else rUnion.ClearContents rUnion.NumberFormat = "General" End If Application.ScreenUpdating = True Application.Calculation = calcM End Sub