VBA - date comparison is not working

logstarter

New Member
Joined
Apr 14, 2017
Messages
39
Hi,

I have written the following VBA code but it seems that it doesn't work because the code keeps creating the new row even the date is today. I would like to seek advice if anything wrong in my code.

Thanks so much!

Code:
Sub createDate()

    Dim lRow As Long
    z = Format(Now, "dd/mm/yyyy")
    lRow = Worksheets("Log").Cells(Rows.count, 1).End(xlUp).Row
    lRowValue = Worksheets("Booking Log").Cells(lRow, 1).Value
    If (lRowValue <> z) Then
        Worksheets("Log").Cells(lRow + 1, 1).Value = z
        Worksheets("Log").Cells(lRow + 1, 2).Value = 0
    End If
    
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
This converts the date into a text

Code:
Z = Format(Now, "dd/mm/yyyy")


then you should also convert the comparison date

Code:
If [COLOR=#0000ff]Format(lRowValue, "dd/mm/yyyy")[/COLOR] <> Z Then
 
Upvote 0
Thanks. I tried and debugged. I found another strange issue.

Code:
    If (lRowValue <> z) Then

the IRowValue is showing 04/03/2019 (which is the same as excel worksheet) and z is showing 03/04/2019, that's why that cannot be compared as equal. However, the IRowValue should be created by z as it's assigned by the code "Worksheets("Log").Cells(lRow + 1, 1).Value = z". Do you have any clue about that? Thanks!

Code:
Sub createDate()

    Dim lRow As Long
    z = Format(Now, "dd/mm/yyyy")
    lRow = Worksheets("Log").Cells(Rows.count, 1).End(xlUp).Row
    lRowValue = Format(Worksheets("Log").Cells(lRow, 1).Value, "dd/mm/yyyy")
    If (lRowValue <> z) Then
        Worksheets("Log").Cells(lRow + 1, 1).Value = z
        Worksheets("Log").Cells(lRow + 1, 2).Value = 0
    End If

End Sub
 
Upvote 0
Code:
If Format(lRowValue, "dd/mm/yyyy") <> Z Then

Worksheets("Log").Cells(lRow + 1, 1).Value = Cdate(z)
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
This post just solved one of my issues. Thanks for the coding.
 
Upvote 0

Forum statistics

Threads
1,214,534
Messages
6,120,086
Members
448,944
Latest member
sharmarick

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top