Home > Net >  I can't compare the Dates on VBA
I can't compare the Dates on VBA

Time:01-12

I'm trying to compare the dates that I choose. I mean I'm trying to take the some items which has a date earlier. So I wrote this on VBA. But I noticed that when I run this code the output was the same as input. So it tries to find the earlier items but it couldn't compare so all items are copied.

Private Sub Macro1()
a = Worksheets("SVS").Cells(Rows.Count, 1).End(xlUp).Row

For i = 3 To a

    If Worksheets("SVS").Cells(i, 22).Value < CDate("28/02/2023") Then

        Worksheets("SVS").Rows(i).Copy
        Worksheets("Summary").Activate
        b = Worksheets("Summary").Cells(Rows.Count, 1).End(xlUp).Row
        Worksheets("Summary").Cells(b   1, 1).Select
        ActiveSheet.Paste
        Worksheets("SVS").Activate

    End If
Next i

Application.CutCopyMode = False

ThisWorkbook.Worksheets("SVS").Cells(1, 1).Select

End Sub

What is missing in the code? I wanna learn.

CodePudding user response:

Check you have a valid date to compare with.

Option Explicit
Private Sub Macro1()

    Dim wb As Workbook, ws As Worksheet, v
    Dim lastrow As Long, i As Long, b As Long, n As Long
    
    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Summary")
    b = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    
    With wb.Sheets("SVS")
        lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row
    
        For i = 3 To lastrow
            v = .Cells(i, 22) ' col V
            If IsDate(v) Then
                If CDbl(v) < DateSerial(2023, 2, 28) Then
                    b = b   1
                    .Rows(i).Copy ws.Cells(b, 1)
                    n = n   1
                End If
            End If
        Next i
    End With
    MsgBox n & " rows copied to Summary", vbInformation, lastrow - 2 & " rows checked"
    
End Sub

CodePudding user response:

Append If Earlier Date

Option Explicit

Sub AppendEarlierDate()

    Dim wb As Workbook: Set wb = ThisWorkbook
    Dim sws As Worksheet: Set sws = wb.Sheets("SVS")
    Dim srg As Range
    Set srg = sws.Range("V3", sws.Cells(sws.Rows.Count, "V").End(xlUp))
    
    Dim surg As Range, sCell As Range, sValue
    
    For Each sCell In srg.Cells
        sValue = sCell.Value
        If IsDate(sValue) Then
            If sValue < DateSerial(2023, 2, 28) Then
                If surg Is Nothing Then
                    Set surg = sCell
                Else
                    Set surg = Union(surg, sCell)
                End If
            End If
        End If
    Next sCell
    
    If surg Is Nothing Then Exit Sub
    
    Dim dws As Worksheet: Set dws = wb.Sheets("Summary")
    If dws.FilterMode Then dws.ShowAllData 
    
    Dim dlCell As Range, dfCell As Range
    
    Set dlCell = dws.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious)
    If dlCell Is Nothing Then
        Set dfCell = dws.Range("A1")
    Else
        Set dfCell = dws.Cells(dlCell.Row   1, "A")
    End If
    
    surg.EntireRow.Copy dfCell
    
End Sub
  • Related