Search code examples
excelvbatrim

TRIM Function for only rows with yesterday's Date


I have this TRIM function running but to save time would like to only run it on the previous days rows, column G contains a Date.

Please can someone guide me to achieving this.

sub TrimText()
Dim lRow As Integer

With Worksheets("Data")
    lRow = .Range("A2").End(xlDown).Row
    
    For i = 2 To lRow
        .Cells(i, "A").Value = Trim(.Cells(i, "A").Value)
    Next i
End With
End Sub

Solution

  • If you mean that there will be a date stored in column G, and if that date is yesterday when running - then the TRIM code should run on that row:

    For i = 2 To lRow
        If .Cells(i, "G").Value = Int(Now()) - 1 Then
            .Cells(i, "A").Value = Trim(.Cells(i, "A").Value)
        End If
    Next i