[VBA] How to Find a Cell with Today’s Date | Mastering the Match Function

目次

Introduction

When handling dates in VBA, there is often a need to “determine if today’s date exists within a specified range.”

I recently needed to identify the position of a cell containing “today’s date” in a sheet where dates are listed consecutively in column A. I implemented a method to do this using VBA.

Function Used: WorksheetFunction.Match

VBA has a convenient feature called WorksheetFunction that allows you to use standard Excel functions directly in your code. For this task, we will use the Match function.

By using this, you can retrieve the position of the cell that matches “today’s date” within column A.

Code Example: Searching for Today’s Date in A1:A1000

Dim find_date As Variant

' Search for today's date in the range A1:A1000
find_date = WorksheetFunction.Match(CLng(Date), Range("A1:A1000"), 0)

Explanation of the Code

CLng(Date)

The “today’s date” obtained with the Date function is treated as a Date type in VBA. However, the Match function requires the value to match the Serial Value (Long type) used on the Excel sheet. Therefore, we use CLng() to convert the date into a long integer.

Range(“A1:A1000”)

This sets the search scope from A1 to A1000. You can change this range as needed.

0 (Exact Match)

By specifying 0 as the third argument of the Match function, you enable “Exact Match” mode. This ensures the function only targets cells that contain exactly the same date as today.

Example of Execution Result

For instance, if column A contains the date 2024/05/01 in the 5th row, and that matches today’s date, find_date will store the value 5.

Note: This 5 represents the “5th item within the range (A1:A1000),” not necessarily the absolute row number of the worksheet.

Error Handling: When Today’s Date Does Not Exist

A key characteristic of WorksheetFunction.Match is that it throws a runtime error if the target value is not found. Therefore, it is safe to include error handling in advance.

Simple Error Handling Example

Dim find_date As Variant

' Prevent the code from stopping if an error occurs
On Error Resume Next
find_date = WorksheetFunction.Match(CLng(Date), Range("A1:A1000"), 0)

' Check if an error occurred (Err.Number will be non-zero if Match failed)
If Err.Number <> 0 Then
    MsgBox "Today's date was not found."
End If

' Reset error handling
On Error GoTo 0

By using this structure, even if the target date is not found, the process will not crash. Instead, you can display a message to the user and proceed to the next step safely.

Summary

If you want to “search for a cell containing today’s date” in VBA, you can implement it smoothly by following these points:

  • Use WorksheetFunction.Match to get the search position.
  • Get today’s date with the Date function and convert it to a serial value using CLng().
  • Specify Exact Match (0) for the third argument of Match.
  • Do not forget error handling for cases where the target is not found.

By utilizing this technique, you can build smarter automation processes that use dates as keys.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次