2016-11-21 8 views
0

だから私は複数のマクロの入力値は、ブック内のセルにあります。これは私がとのトラブルを抱えている関連する部分である:セルの値に何が追加されているかを確認する方法はありますか?

If wsCsh.Cells(cshrow, cshcol) = "" Then 
     Cells(cshrow, cshcol - 1).FormulaR1C1 = "='WREG'!R" + CStr(wregrow) + "C" + CStr(wregcol) 
Else 
     Cells(cshrow, cshcol - 1).FormulaR1C1 = Cells(cshrow, cshcol).FormulaR1C1 + "+'WREG'!R" + CStr(wregrow) + "C" + CStr(wregcol) 
End If 

セル(cshrow、cshcol)が空の場合、それがあれば、セル(wregrow、wregcol)にリンクするR1C1式に入り、それはセル内にあるものにcell(wregrow、wregcol)を追加しません。しかし、私はこのマクロを2度続けて実行すると言う。それから、基本的にcell(wregrow、wregcol)をcellに2回追加し、cell(cshrow、cshcol)は "cell(wregrow、wregcol)+ cell(wregrow、wregcol)"のようになります。 R1C1の式でセルがすでに参照されているかどうかを確認するには、2回目の値の追加を避けることができますか?

+0

最初に:テキストには '' + '"を使用しないでください(いくつかの理由でより良いです)。チェックを実行するには '' WREG '!R "&CStr(wregrow)&" C "&CStr(wregcol)'が '' Cells(cshrow、cshcol)の一部であるかどうかを 'instr()'で調べてください。 FormulaR1C1';) –

+0

私のために働いてくれてありがとう! –

答えて

0
Dim extraBit As String 

With wsCsh 
    extraBit = "+'WREG'!R" & wregrow & "C" & wregcol 
    If .Cells(cshrow, cshcol).Value = "" Then 
     'Set the formula to the "extra bit" 
     .Cells(cshrow, cshcol - 1).FormulaR1C1 = "=" & extraBit 
    ElseIf Right(Cells(cshrow, cshcol - 1).FormulaR1C1, Len(extraBit)) <> extraBit Then 
     'Add the "extra bit" if the current formula doesn't already end with it 
     .Cells(cshrow, cshcol - 1).FormulaR1C1 = .Cells(cshrow, cshcol).FormulaR1C1 & extraBit 
    End If 
End With 
関連する問題