Type mismatch

SamS

Well-known Member
Joined
Feb 17, 2002
Messages
542
I have a header row that contains text and dates, the dates are formatted as Sep-02 etc. and am using an input box to loop through each cell till I find the correct date eg
Item Descr Qty Jul-02 Aug-02 Sep-02

The code I was using is something like this:

Dim Month as Date
Month = InputBox("Please enter the month eg Sep-02")

Range("A2").Select

Do While ActiveCell.Value <> Month
ActiveCell.Offset(0, 1).Select
Loop

This gives an error message on the first cell as it is Text and not a Date. In the past I've usually had the header formatted as text and been able to work around the problem but in this case it is not an option to change the format.

I hate working with dates.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I tried both but neither works - both bypass the error but neither recognises the date
 
Upvote 0
Have you tried running your code by using F8 in VBE so you can see step-by-step what is going on and when/where the error occurs? If so, at what point do you get the error?
 
Upvote 0
If I Dim variable as a Date the error occurs on the first Text entry, if I leave as a Variant or set it as a String the variable shows the Date Field as 01/09/2002 and the loop continues on until the end of 256 columns. :(

This has really driven me batty.
 
Upvote 0
The problem is that if you Dim the variable as a DATE, but the data is only month and year, then you will get an error. Although the dates in the columns may APPEAR 9/20/2002, the value is quite different. I would suggest that you first try having the user input the date in a standard date format (e.g., 9/20/2002). Then Dim your variable as Date. See if that works. If so, you can then try tweaking as you go.

Does that help?
 
Upvote 0
On 2002-11-04 18:02, phantom1975 wrote:
The problem is that if you Dim the variable as a DATE, but the data is only month and year, then you will get an error. Although the dates in the columns may APPEAR 9/20/2002, the value is quite different. I would suggest that you first try having the user input the date in a standard date format (e.g., 9/20/2002). Then Dim your variable as Date. See if that works. If so, you can then try tweaking as you go.

Does that help?

The dates are entered as 01/09/2002 and the cells are formatted as Sep-02. If I Dim the variable as Date errors occur as soon as the first Text field is found. Unfortunately the header row is a mixture of Text and Dates. I had tried inputting 01/09/2002 with the variable set as String but still no go. Think I've tried every combination.
 
Upvote 0

Forum statistics

Threads
1,214,583
Messages
6,120,383
Members
448,955
Latest member
BatCoder

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