0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

エクセル2019のVBAで時刻を検索するコード

Posted at

エクセルVBAで、時刻を検索するコードを作るのに大変苦労したので、メモを残す。
エクセルのバージョンは、2019。

準備

'
' あらかじめ、時刻を表す各セルを選択し、次のような「書式設定」を行っておく
'
    Selection.NumberFormatLocal = "h:mm"

時刻を検索するコード(2種類)

'
' 「What:=」や「What:=TimeValue(」に続く「Time」には、「"21:00"」・「"0:00"」などが入る。
'
' (1)「6:00」等を検索する(数式検索「LookIn:=xlFormulas」)
'
    Columns("A:A").Select		' 検索範囲を作っておく
    Selection.Find(What:=TimeValue(Time), After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Select
'
' (2)「0:00」等を検索する(値検索「LookIn:=xlValues」)
'
    Columns("A:A").Select		' 検索範囲を作っておく
    Selection.Find(What:=Time, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
        xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
        , SearchFormat:=False).Select

終わりに

エクセルのVBAコードは、マクロの記録通りでも一筋縄ではいかない場合がある。
特に、Findメソッドで時刻を検索しようとすると、検索対象のセルの「書式設定」によって、うまく検索できない場合があって、本当に苦労した。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?