Excel VBA如何replace包含大于零的数字的每个单元格
我怎样才能取代一个范围内大于零的数字?
我可以在给定的范围内replace一个特定的词或数字:
Sheet1.Columns("N").Replace What:="1", Replacement:="Good", LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False
现在,如果我想要replace所有包含大于零的数字(如1-1000000
,如果我使用循环语句,这将使macros运行速度变慢。
我希望能有这样的代码:
Sheet1.Columns("N").Replace What:=">0", Replacement:="Good", LookAt:=xlWhole, SearchOrder:=xlByRows, MatchCase:=False`
你可以使用一个filter:
Sub tgr() On Error Resume Next 'Prevents error if there are no cells with value >0 With Intersect(Sheet1.UsedRange, Sheet1.Columns("N")) .AutoFilter 1, ">0" .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Value = "Good" .AutoFilter End With On Error GoTo 0 'Remove the On Error Resume Next condition End Sub
您可能可以使用= EVALUATE(),但以下内容不应太慢:
Sub qwerty() Dim r As Range, rFix As Range, rr As Range Set r = Intersect(ActiveSheet.UsedRange, Range("N:N")) Set rFix = Nothing For Each rr In r If IsNumeric(rr.Value) Then If rr.Value > 0 Then If rFix Is Nothing Then Set rFix = rr Else Set rFix = Union(rr, rFix) End If End If End If Next rFix.Value = "Good" End Sub
尝试:
Sub PositiveIsGood() For x = 1 To ActiveSheet.UsedRange.Rows.Count If Cells(x, "N") > 0 Then Cells(x, "N") = "Good" End If Next x
结束小组