2017-01-07 18 views
0

現在の方法では値を変更するためにreplace関数を使用しようとしていますが、VBA/Excel - セルに式が含まれていない場合にのみ値を置き換えます。

置き換え機能は、1つの列内の数式のないセルでのみ機能することができますか? 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 

答えて

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 

それが動作する場合は、次の私はあなたが私が充填されたカラム内すべてののセルを持って疑うように私はおそらく最後を取得し、単に実行可能な範囲を使用することをお勧め。行をして少し時間を節約する Set rng = Range("I1:I" & lastRow)を実行します。

注:これは、9="9"のセルがある場合に機能します。それはちょうど9を置き換え、私の="9"をそこに保ちました。

+0

式 –

+1

@ShaiRado - D'oh!いいえ、あなたはあまりにも疲れていない、私はちょうど良い気分ではありません。私はそれを誤解した、あなたは正しい。私は 'xlCellTypeConstants'に切り替えるように更新しました。 – BruceWayne

0

ないマクロ最速いますが、このように各セルを反復処理することができます:私は疲れたかもしれないが、私はOPは正反対のを望んでいると思う、とのセルを変更しない

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の

関連する問題