Issue Comparing Dates

fluxcapacitr609

New Member
Joined
Feb 21, 2018
Messages
24
I'm trying to clean up my code. I want to change how I compare dates. It works when I type in the date, but when I declare a function and set it to a date, it doesn't work.

Working Code
Code:
Sub TestMod()










'.......................................................






Dim LastRow As Long
Dim erow As Long
Dim i As Long
Dim strComponent
Dim strVessel
Dim intOil As Long
Dim myCell
Dim bcheck As Boolean




Dim lwRng   ' experimental lower date
Dim hgRng   ' experimental upper date


LastRow = Sheet1.Cells(Rows.count, 1).End(xlUp).Row
Sheet1.Select
Range("A1").Select


'January


On Error Resume Next


bcheck = Evaluate("isref(Jan_2018!A1)")
If bcheck Then


    Set myCell = Sheets("Jan_2018")
    lwRng = "2018-1-1"
    hgRng = "2018-1-32"
    
    'Cape Henlopen, Crossings
    For i = 2 To LastRow


        mydate = Cells(i, 3)
        strVessel = Cells(i, 1)
        strComponent = Cells(i, 2)
    
        If InStr(1, strComponent, "Number of Crossings") And InStr(1, strVessel, "Cape Henlopen") And mydate >= "2018-1-1" And mydate <= "2018-1-32" Then
            erow = Day(mydate) + 3
            Cells(i, 4).Copy Destination:=myCell.Range("J" & erow)
        End If
    
Next i

end sub


Not working

Code:
Sub TestMod()










'.......................................................






Dim LastRow As Long
Dim erow As Long
Dim i As Long
Dim strComponent
Dim strVessel
Dim intOil As Long
Dim myCell
Dim bcheck As Boolean




Dim lwRng   ' experimental lower date
Dim hgRng   ' experimental upper date


LastRow = Sheet1.Cells(Rows.count, 1).End(xlUp).Row
Sheet1.Select
Range("A1").Select


'January


On Error Resume Next


bcheck = Evaluate("isref(Jun_2018!A1)")
If bcheck Then


    Set myCell = Sheets("Jun_2018")
    lwRng = "2018-6-1"
    hgRng = "2018-6-32"
    
    'Cape Henlopen, Crossings
    For i = 2 To LastRow


        mydate = Cells(i, 3)
        strVessel = Cells(i, 1)
        strComponent = Cells(i, 2)
    
        If InStr(1, strComponent, "Number of Crossings") And InStr(1, strVessel, "Cape Henlopen") And mydate >= lwRng And mydate <= hgRng Then
            erow = Day(mydate) + 3
            Cells(i, 4).Copy Destination:=myCell.Range("J" & erow)
        End If
    
Next i
end sub


I'm sure it's something small I'm missing but any help would be greatly appreciated.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I am not really clear on what you are trying to do or where's the code giving an error. However, going through your code, I can suggest the following

Change hgRng = "2018-6-32" to hgRng = WorksheetFunction.EoMonth(lwRng, 0) to avoid error dates as there's only 30 days in Jun

Also, I don't see the need of the below two lines
Sheet1.Select
Range("A1").Select
 
Upvote 0
Sheet1.select gives you the sheet pull data from
range a1 is where the data starts.
when i had the exact date, ex. 6/30/18 then it wouldnt pull data for the 30th. So I have a generic 32 because every month has 31 or less days in it.

Im actually not getting any errors, its just not working.
 
Upvote 0
Remove this line of code
Code:
On Error Resume Next
and try running the macros again. Do you get any errors?
 
Upvote 0

Forum statistics

Threads
1,215,247
Messages
6,123,857
Members
449,129
Latest member
krishnamadison

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