2016-09-21 16 views
-1

セルの値に基づいてマクロの継続をトリガーする方法を支援したいと考えています。セルの内容に基づいてマクロを実行する方法

私が期待しているのは、セルA1 =セルB1のときは、タスクを実行する必要があり、そうでなければスクリプトを終了する必要があります。

これは単純なIf... Thenスクリプトでしょうか?どんな支援も大歓迎です!

+0

事のこの種の「メイン」マクロからのパラメータは、通常、 'Worksheet_Change'イベントのイベントハンドラを記述することで行われます。オンラインでさまざまな例を見つけるのは簡単です。 –

答えて

1

ここでは、あなたの価値のすべてがすでに存在し、あなたがチェックしていると仮定しています。その場合、誰かがライブを入力するのではなく、ジョンが良い方法です。しかし、私がworksheet_changeについて気に入らないことの1つは、マクロが実行されると元に戻すことができないということです。私はあなたが2つの空のセルを持っているかのように、引き続き引き金を引くように、長さ> 0で追加しました。

Sub TestValues() 

'Define variables 
Dim rng As Range 
Dim cell As Range 

'Set variables 
Set rng = Range("A1:A10") 

'Begin 
For Each cell In rng 
    If Len(cell) > 0 Then 
     If cell.Value = cell.Offset(0, 1).Value Then 
      'Run macro 
     End If 
    End If 
Next cell 

End Sub 

*編集 - 私はそれを正しく読まなかったと思います。何らかの理由で私はあなたが行くべき価値の範囲があると思っていました。それはより多くのこのような、長いポスト申し訳ありません...

Private Sub Worksheet_Change(ByVal Target As Range) 

If Target.Address = "$A$1" Then 
    If Len(Target) > 0 Then 
     If Target.Value = Target.Offset(0, 1).Value Then 
      'Run macro 
     End If 
    End If 
End If 

End Sub 
1

だろうのためにあなたは、私はそれが本当にを知っているように、あなたのマクロはA1および/またはB1細胞を変更を担当していると仮定し尋ね正確に何をすべきかその "継続チェック"を行う場合はとなります。あなたは複数の方法でそれを行うことができます。この場合

、ここではそれらの2つです:マクロ

Sub main() 

    ' your code till the statement that changes A1 and/or B1 

    If Range("A1").Value <> Range("B1").Value Then Exit Sub '<-- if cells A1 and B1 DON'T have the same values then exit sub 

    'here follows your code for the "Task" 

End Sub 
  • 内部

    • キープ「継続」のコードが別のSubにタスクを要求

      Sub main() 
      
          ' your code till the statement that changes A1 and/or B1 
      
          If Range("A1").Value = Range("B1").Value Then DoTask '<-- if cells A1 and B1 HAVE the same values then call 'DoTask()' 
      End Sub 
      
      
      Sub DoTask() 
          ' here your code code for the "Task" 
      End Sub 
      

      この場合、o NE(またはそれ以上)DoTask

      Sub main() 
      
          ' your code till the statement that changes A1 and/or B1 
      
          If Range("A1").Value = Range("B1").Value Then DoTask Range("A1").Value '<-- if cells A1 and B1 HAVE the same values then call 'DoTask passing A1 cell value as "argument" 
      End Sub 
      
      
      Sub DoTask(val As Variant) '<--| DoTask accepts a parameter declared as of Variant type (but you may choose a more proper one 
          ' here your code code for the "Task" 
          ' it will use 'val' 
      End Sub 
      
  • +0

    @kitarika、それを乗り越えましたか? – user3598756

    関連する問題