简体   繁体   中英

Find if date is present in Range of cells (Excel VBA)

I have a cell F2 that contains a Date, and is formatted as Custom Date field displaying only day and month.

Then i have a Range of Cells C3;C60 that contains also Dates and is formatted as European Date field displaying dd/mm/yyyy

I am writing a VBA that makes a check on those fields, but it is not working. in the sample below the variable c can be any cell between F5 and F20 .

I get RuntimeError 91.

If Worksheets(1).Range("C3", "C60").Find(Worksheets(1).Cells(2, c.Column).Value) = True Then
        c.Value = "Whatever"        
Else

Substitute:

Range("C3:C60")

for

Range("C3", "C60")

There may be other problems.

.Find() returns a range, your if statement is checking to see if it's TRUE . That will fail.

Dim rng as Range

Set rng = Worksheets(1).Range("C3:C60").Find(Worksheets(1).Cells(2, c.Column).Value)
if not rng is Nothing then
  c.Value = "Whatever"        
Else

Note the inclusion of Gary's answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM