简体   繁体   中英

VBA / Excel - Replace value only if cell does not contain formula

I'm attempting to use the replace function to change some values, however the way I'm currently doing it will change values in vital formula.

How can I have the replace function work only on cells with no formula, within one column? I tried If Not Columns("I").HasFormula Then but that prevents the replace from working on the entire column if a formula is found.

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

What about this:

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

If that works, next I suggest just using the workable range, as I doubt you have every cell in column I filled in. Perhaps get the last row and do Set rng = Range("I1:I" & lastRow) to save a little time.

Note: This worked if I had cells with 9 and ="9" . It just replaced 9 and kept my ="9" there.

Not the quickest macro but you could iterate over each cell like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM