jeudi 26 avril 2018

Excel VBA: Optimal use of DateValue in VBA (or other method to compare dates)

I've written a VBA macro in Access that's creating a new excel file basing on a few different ones. I'm unable to change the way the source files are formatting the data, and I want to write a simple IF statement (if date X is past date Y, delete entire row). The problem is that the dates have different format. The way I'm able to do it is create a new column:

=IF(DATEVALUE(A2)>DATEVALUE(H2),TRUE,FALSE)

And then go from that and at the end delete the column. That doesn't feel right though and I believe there's a better solution. I've been trying to do something like

 If ifDelete = vbYes And .Cells(Lrow, 19).Value Like "*timing*" And DateValue(.Cells(Lrow, 1).Value) > DateValue(.Cells(Lrow, 8).Value) Then
      .Cells(Lrow, 19).EntireRow.Delete
 End If

But it obviously doesn't work. I also tried working with formatting the date columns prior to running that IF statement, but still, no luck. If that helps, dates are formatted in a way MM/DD/YYYY and M/D/YYYY. For some reason going with .NumberFormat = "MM/DD/YYYY" on the 2nd or both fields doesn't work too.

Kind regards, heaton124.

Aucun commentaire:

Enregistrer un commentaire