Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50061

How do I store the current date to a variable and compare it to cells that have dates?

$
0
0
Here is the current macro I am working with.:
Code:

Sub MySub()

    Dim strColumn As String
    Dim currentRow As Integer
    Dim todayDate As Date
    Dim ws As Worksheet
   
    Set ws = Workbooks("file_I_am_getting_data_from.xlsm").Sheets(1) 'I am getting the data from the larger file
    todayDate = Format(Date, "dd/mm/yyyy")

    currentRow = 3458
   
    'Copies over data that is relevant to this day (=today) from the larger file to smaller one
    Do Until ws.Cells(row, 1).Value <> todayDate
        ws.Range("A" & currentRow & ":" & "X" & currentRow).Copy ThisWorkbook.Worksheets(1).Range("AO2")
        currentRow = currentRow - 1
    Loop
   
    'ws.Range("A" & currentRow & ":" & "X" & currentRow).Copy ThisWorkbook.Worksheets(1).Range("AO2")
   
End Sub

I am trying to change an Excel file from the bottom-up until I find a date that is not today's date (where today means the day you look at the file and not the day this forum post is posted) in which case, I just want the copying process to stop but I am not able to successfully compare the dates.

The compiler says:
Quote:

Run-time error '1004':

Application-defined or object-defined error
and suggests that the following line is at fault.:
Code:

Do Until ws.Cells(row, 1).Value <> todayDate
Any help in getting the loop and date comparison to work well would be greatly appreciated!

Viewing all articles
Browse latest Browse all 50061

Trending Articles