VBA loop through date type mismatch

Rolsu

New Member
Joined
Jul 16, 2019
Messages
27
Office Version
  1. 2021
Platform
  1. Windows
Hi all!

I have a problem with the dates.
I have a list of dates that I want to loop through and copy the result to another sheet. The condition is a date range.
Here is my code:
VBA Code:
Sub CommandButton1_Click()

    Dim lastrow, erow, i As Long
    Dim mydate, sdate, edate As Date
        
        sdate = InputBox("", "start date")
        edate = InputBox("", "end date")
        
        lastrow = Sheets("Data").Cells(Rows.Count, 1).End(xlUp).Row
        
        Sheets("Data").Range("A1").Select
        
            For i = 2 To lastrow 'using headers
                mydate = Cells(i, 3)
                If mydate >= sdate And mydate <= edate Then
                erow = Sheets("Result").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
                Range(Cells(i, 1), Cells(i, 4)).Copy Destination:=Sheets("Result").Cells(erow, 1)
                End If
            Next i
End Sub

I'd like to use the format "dd.mm.yyyy" everywhere. The list of dates are already set to this format. But when I enter this format to the inputboxes it gives a run-time error '13" type mismatch to the line where I set the "edate"
I've tried to format the inputbox value to the same ( Dim sdate, edate as String, Inputbox("", "start date", Format(Now(), "dd.mm.yyyy")) ), but in that case the error came to the "mydate" line.
I don't know how to set them so they match.
Can someone help?
Thank you.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
When you define variables as below:
VBA Code:
Dim mydate, sdate, edate As Date
Only edate has been defined as a specific type, the first two are left defined with no type, this is why the first error you saw was on 'edate'.
sdate did not give an error as it never had a type, sdate would have changed to a string and accepted any value from the input box.

Input box returns a string. The error you see with edate is because it was defined as a date but was passed a string so you get the error.

IMO, this is the importance of defining variables correctly, it allows you to get the correct errors and speeds up debugging.

You could use a replace function to change the . into / and wrap that in CDATE to ensure you get a date out of the IBox.

There are other ways to write this but thought i would leave the code intact so you can see what has been changed:
VBA Code:
Sub test()
    Dim lastrow As Long, erow As Long, i As Long
    Dim mydate As Date, sdate As Date, edate As Date
        
    sdate = CDate(Replace(InputBox("", "start date"), ".", "/"))
    edate = CDate(Replace(InputBox("", "end date"), ".", "/"))
    lastrow = Sheets("Data").Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = 2 To lastrow 'using headers
        mydate = CDate(Cells(i, 3).Value)
        If mydate >= sdate And mydate <= edate Then
        erow = Sheets("Result").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        Range(Cells(i, 1), Cells(i, 4)).Copy Destination:=Sheets("Result").Cells(erow, 1)
        End If
    Next i
End Sub
 
Upvote 0
When you define variables as below:
VBA Code:
Dim mydate, sdate, edate As Date
Only edate has been defined as a specific type, the first two are left defined with no type, this is why the first error you saw was on 'edate'.
sdate did not give an error as it never had a type, sdate would have changed to a string and accepted any value from the input box.

Input box returns a string. The error you see with edate is because it was defined as a date but was passed a string so you get the error.

IMO, this is the importance of defining variables correctly, it allows you to get the correct errors and speeds up debugging.

You could use a replace function to change the . into / and wrap that in CDATE to ensure you get a date out of the IBox.

There are other ways to write this but thought i would leave the code intact so you can see what has been changed:
VBA Code:
Sub test()
    Dim lastrow As Long, erow As Long, i As Long
    Dim mydate As Date, sdate As Date, edate As Date
       
    sdate = CDate(Replace(InputBox("", "start date"), ".", "/"))
    edate = CDate(Replace(InputBox("", "end date"), ".", "/"))
    lastrow = Sheets("Data").Cells(Rows.Count, 1).End(xlUp).Row
   
    For i = 2 To lastrow 'using headers
        mydate = CDate(Cells(i, 3).Value)
        If mydate >= sdate And mydate <= edate Then
        erow = Sheets("Result").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        Range(Cells(i, 1), Cells(i, 4)).Copy Destination:=Sheets("Result").Cells(erow, 1)
        End If
    Next i
End Sub
Hi Georgiboy!

At the first time, when I just copy-paste your code it didn't work. The date of list is set and shows the following format: "dd.mm.yyyy." When I run your code it gives the error to the mydate line again.
But, I've changed the format in every cell to "dd. mm. yyyy" (I put spaces after the dots) manually. And suprisingly, it works...
Seriously, I'm getting crazy with this...
Why it doesn't work when I set the format with the excel built-in function (Format cells-Dates-United Kingdom-14.03.2012), but it works when I manually change the already set format?
 
Upvote 0
Hmm, a little strange, have you tried changing this line:
VBA Code:
mydate = CDate(Cells(i, 3).Value)
To:
VBA Code:
mydate = CDate(Replace(Cells(i, 3).Value, ".", "/"))
 
Upvote 0
Hmm, a little strange, have you tried changing this line:
VBA Code:
mydate = CDate(Cells(i, 3).Value)
To:
VBA Code:
mydate = CDate(Replace(Cells(i, 3).Value, ".", "/"))
Sorry, still not working. In this case the result is the first 6 lines of the date list, mixed the 8th and 9th months data.
 
Upvote 0
Maybe you could share the data you are looking at with the XL2BB add-in, I can't see what you are looking at.
 
Upvote 0
Maybe you could share the data you are looking at with the XL2BB add-in, I can't see what you are looking at.
This is sheet1:
Copy_with_ date_range_loop.xlsb
ABCD
1CWS codeWork typeDateHours
2404538Meeting05.09.20231
3404538Design06.09.20232
4404538Design07.09.20233
5408821Meeting15.08.20231
6408821Design16.08.20232
7408821Design17.08.20232
8406733Meeting10.09.20231
9406733Design11.09.20233
10406733Design12.09.20234
11402864Meeting25.10.20231
12402864Design26.10.20235
13402864Design27.10.20233
Data


The 2nd sheet only has the same headers.
Thank you.
 
Upvote 0
Sorry, still not working. In this case the result is the first 6 lines of the date list, mixed the 8th and 9th months data.
Do you think you could explain this in a little more detail?

I just ran it with the date range of 15/08/2023 to 25/10/2023 and it pulled the correct data for me.
 
Upvote 0
Do you think you could explain this in a little more detail?

I just ran it with the date range of 15/08/2023 to 25/10/2023 and it pulled the correct data for me.
I launch the inputbox for the sdate. I type 01.09.2023 or 01/09/2023. Than the second is edate in the same format with the last day of the month 30/09/2023. And the result is just the range of data from sheet1, second row to 7th row. So it mixed the August and September.
 
Upvote 0

Forum statistics

Threads
1,223,881
Messages
6,175,159
Members
452,615
Latest member
bogeys2birdies

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