Comparing two cells with dates in VBA

Edleets

Board Regular
Joined
Apr 9, 2009
Messages
90
I have trouble trying to get the right sintax of comparing two dates locates in two different sheets.

Example:
Sheet 1, cell A1 = 12/11/2009
Sheet 2, cell B1 = 12/11/2009

If I need to do something based on condition that Sheet1 date = sheet 2 date, how will the code be written? I have

If Sheets("Sheet1").range("A1") = Sheets("Sheet2").range("B1") Then
**** my code here

I am not getting anything, am I missing something?

Thanks
Edmundo
 
So your code now looks something like this?
Code:
Sub n()
Dim i As Long
Sheets("Lines").Range("C68:P68").Selection.Copy
For i = 4 To 366
If CDate(Sheets("L1").Cells(i, 4).Value) = CDate(Sheets("Lines").Range("A68").Value) Then
Cells(i, 4).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Else
MsgBox "Date Not Found"
Exit Sub
End If
Next
End Sub
however there will still be a problem i think, where you see Cells (i, 4).Pastespecial a sheet isn't designated, but that should be simple to sort out :)
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
This is almost bound to crash

Rich (BB code):
Sheets("Lines").Range("C68:P68").Selection.Copy
 
Upvote 0
This is almost bound to crash

Rich (BB code):
Sheets("Lines").Range("C68:P68").Selection.Copy
VoG, thanks, it was just a missed portion when i deleted his previous selects :)
Code:
Sub n()
Dim i As Long
Sheets("Lines").Range("C68:P68").Copy 'correction courtesy of VoG
For i = 4 To 366
If CDate(Sheets("L1").Cells(i, 4).Value) = CDate(Sheets("Lines").Range("A68").Value) Then
Cells(i, 4).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Else
MsgBox "Date Not Found"
Exit Sub
End If
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

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