lundi 26 février 2018

13 error (type doesn't match) when comparing two dates within a conditional instruction (vba)

I've wrote the following code to delete rows of worskheet 2 in which value of column 7 (a date) is less tan a value of a certain cell of worksheet 1:

Sub delete()

Dim listaops As Worksheet
Dim RToDelete As Range
Dim DTtoCompare As Date
Dim DTofOp As Date
Dim i As Integer

Set listaops = ThisWorkbook.Worksheets("QryOperacionesComprasyAmort0239")
Set RToDelete = Range("G2", Range("G2").End(xlDown))

DTtoCompare = ThisWorkbook.Worksheets(1).Cells(1, 1).Value

For i = RToDelete.Cells.Count + 1 To 2 Step -1

    DTofOp = listaops.Cells(i, 7).Value

    If DTofOp < DTtoCompare Then
        Cells(i, 7).EntireRow.delete
    End If

Next i

End Sub

When it arrives to the instruction in which I set DTtoCompare: DTtoCompare = ThisWorkbook.Worksheets(1).Cells(1, 1).Value I get the following:

13 Error: Types mismatch

I guess it is because variables are not right formatted or right assigned to a date format, I have tried so many things without success.

Could anyone help me?

Aucun commentaire:

Enregistrer un commentaire