@YOSHIHIKOARCHER

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

日付を入力すると,他のセルに文字が表示されるコードについて

解決したいこと

下のように,A2セルに1が入力されたら,B2セルにcompleteと表示させるコードがありますが,
A1のセルに日付をいれるとB2セルにcompleteと表示させたいとき,
「If MyRow = 2 And MyCol = 1 Then
If Cells(2, 1) = 1 Then 」のコードはどのように書き換えればよいのでしょうか。

該当するソースコード


Private Sub Worksheet_Change(ByVal Target As Range)
Dim MyRow As Long
Dim MyCol As Integer
    MyRow = Target.Row
    MyCol = Target.Column
    If MyRow = 2 And MyCol = 1 Then
        If Cells(2, 1) = 1 Then      'または If Target = 1 Then
        Range("B2").Value = ("complete")                           

        End If
    End If
End Sub



0 likes

3Answer

Comments

  1. @github0013@github様

    Excel関数処理のご教示ありがとうございました。ExcelIF関数で処理できました。

    現在,VBAを勉強中で,If~Thenの箇所で「日付」の書き方がわからなかったものですから質問させて頂きました。

「IsDate」関数を利用すれば実現できると思います。

If IsDate(Cells(2, 1)) Then

「IsDate」を使って、ある列の日付チェックをしたい場合はこのような形になると思います。

Sub CheckDate()
    c = 2
    r = 2
    
    Do While Cells(r, c) <> ""
        If IsDate(Cells(r, c)) Then
            Cells(r, c + 1) = "OK"
        End If
        
        r = r + 1
    Loop
    
End Sub

image.png


「vba 日付 判定」などで検索すると、ほしい情報がいろいろと得られると思います。
0Like

Comments

  1. @YottyPG様

    ご教示ありがとうございました。おかげで解決いたしました。
    「VBA 日付け判定」検索で「日付」に関する目的の情報がありました。
    「IsDate」理解しました。
    ホントにありがとうございました。

次からは、この辺を眺めるとよいかもですね。
恐らく日付判定もするようになると思うので、この辺りとかも?

0Like

Your answer might help someone💌