我正在尝试使用替换功能来更改某些值,但是我现在这样做会改变重要公式中的值。
如何让替换功能仅在一列中没有公式的单元格上工作?我尝试了If Not Columns(“I”)。HasFormula然后但如果找到一个公式,则会阻止替换在整个列上工作。
Columns("I").Replace What:="10", _
Replacement:="Five", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Columns("I").Replace What:="9", _
Replacement:="Four", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Columns("I").Replace What:="8", _
Replacement:="Three", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Columns("I").Replace What:="7", _
Replacement:="Three", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Columns("I").Replace What:="6", _
Replacement:="Two", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Columns("I").Replace What:="5", _
Replacement:="Two", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Columns("I").Replace What:="4", _
Replacement:="One", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Columns("I").Replace What:="3", _
Replacement:="One", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Columns("I").Replace What:="2", _
Replacement:="One", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Columns("I").Replace What:="1", _
Replacement:="One", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
End If
答案 0 :(得分:1)
这个怎么样:
Sub replaceFormulas()
Dim rng As Range
Set rng = Range("I:I")
With rng.SpecialCells(xlCellTypeConstants)
.Replace What:="9", Replacement:="Four", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
' etc etc
End With
End Sub
如果可行,接下来我建议只使用可行范围,因为我怀疑你在第I列填写了每个单元格。也许得到最后一行并做
Set rng = Range("I1:I" & lastRow)
节省一点时间。
注意:如果我的单元格为9
和="9"
,则此方法有效。它刚刚替换9
并将="9"
保留在那里。
答案 1 :(得分:0)
不是最快的宏,但你可以像这样迭代每个单元格:
Sub fixCol_I()
Dim cell As Range
Dim iMatch As Integer
Dim strWhat As Variant
Dim strReplc As Variant
strWhat = Array("10", "9", "8", "7", "6", "5", "4", "3", "2", "1")
strReplc = Array("Five", "Four", "Three", "Three", "Two", "Two", "One", "One", "One", "One")
For Each cell In Columns("I").rows
If Not cell.HasFormula And cell <> "" Then
For iMatch = 0 To UBound(strWhat)
cell.Replace What:=strWhat(iMatch), _
Replacement:=strReplc(iMatch), _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Next iMatch
End If
Next cell
End Sub