2017-07-12 5 views
1

特定のセルの値をチェックし、複数の列を値で設定してExcelシートを自動化しようとしています。特定のセルの値を調べる方法

たとえば、A1のセルが「2」の場合、B2:D54のすべてのセルを0に等しい値にしたいと思います。コードは書き込まれていますが、間違っているようです。

Private Sub Worksheet_Change(ByVal Target As Range) 
    //this selects the cell C3 and checks if the value if is 0619 
    If Range("C3")="0619" 
     Dim example As Range 
     Set example =Range("E3:AE53") 
     example.Value = 0 
    End if 
End Sub 

編集 - Thenを追加しましたが、それでも動作していません。

Private Sub Worksheet_Change(ByVal Target As Range) 
    //this selects the cell C3 and checks if the value if is 0619 
    If Range("C3")="0619" Then 
     Dim example As Range 
     Set example =Range("E3:AE53") 
     example.Value = 0 
    End if 
End Sub 
+1

もし 'If Range(...')行の最後に 'THEN'が見つからなかった場合は、プロシージャの先頭に' Application.EnableEvents = False'を追加し、 'Application.EnableEvents = True '最後に、シートに1377の値を追加し、それぞれの変更イベントが発生します。 –

+0

それを追加しましたが、まだ動作しません。 – user456

+1

' C3'はテキストとしてフォーマットされていますか? –

答えて

1
  • あなたはIf

  • に沿ったThenが欠落しているVBAでのコメントは、そう、これは正しく解析しません'、ない//で開始されています。

  • If Range("C3")="0619" Excelは数字の先頭に0を付けます。テキストとして値を書式設定する場合は、先行ゼロのみを使用します。

  • 編集:If Range("C3")のみ


Private Sub Worksheet_Change(ByVal Target As Range) 
    'this selects the cell C3 and checks if the value if is 0619 
    If Range("C3").Value = "0619" Then 
     Dim example As Range 
     Set example = Range("E3:AE53") 
     example.Value = 0 

    End If 
' 
    Range("A1").Select 
End Sub 
0
  • よりIf Range("C3").Value良くC3が変更された場合、作業を行います。
  • C3は、(先行ゼロ)0000として書式設定された数値であれば、コメントで述べたように、その後If Range("C3").Text ="0619" Then
  • を使用し、イベントやあなたのルーチンを無効にしますが、セルの値を変更したときに、それ自体の上で実行しようとします。
  • イベントドリブンサブ手順は

エラー制御を持つべきコード:

Private Sub Worksheet_Change(ByVal Target As Range) 
    if not intersect(target, range("C3")) is nothing then 
     on error goto safe_exit 
     application.enableevents = false 
     select case range("C3").text 
      case "0619" 
       Range("E3:AE53") = 0 
      case else 
       'do nothing 
     end select 
    End if 
safe_exit: 
    application.enableevents = true 
End Sub 

私はあなたの基準は、それが簡単に追加条件に対応するために行うために、Select Caseステートメントを確認し変更しました。

関連する問題