Code does not work - Type Mismatch

twinkle99

Board Regular
Joined
Aug 7, 2005
Messages
240
Hi

I have a small table of data as follows:
Book1.xls
ABCD
1compdatetargdatecode
202/06/200700:0001/06/200700:00A
318/06/200700:0002/06/200700:00D
416/08/200700:0003/08/200700:00A
516/08/200700:0004/06/200700:00D
615/08/200700:0005/07/200700:00D
718/08/200700:0006/06/200700:00D
807/07/200700:00A
912/04/200700:0008/08/200700:00B
1013/04/200700:0009/05/200700:00D
1110/06/200700:00D
1222/05/200700:0011/06/200700:00D
1323/05/200700:0012/05/200700:00C
1413/06/200700:00D
1512/05/200700:0014/05/200700:00E
1613/05/200700:0015/06/200700:00E
1716/08/200700:00D
1816/05/200700:0017/05/200700:00A
1928/05/200700:0018/06/200700:00A
Sheet1


I have written a macro which simply counts the number of rows where column A is later than column B and Column C is 'D'.

Dim rng As Range, c As Range
Dim count As Long
count = 0
Set rng = Range("A1:D20")
For Each c In rng
If c.Offset(0, 2) = "D" And c.Offset(0, 0)<> "" And c.Offset(0, 0) > c.Offset(0, 1) Then count = count + 1
Next c
[D1] = count
End Sub

This works as intended - 4 rows.

However, I now want to count rows where Column A is more than 10 days later than Column B. I tried this code but it does not work.

Dim rng As Range, c As Range
Dim count As Long
count = 0
Set rng = Range("A1:E20")
For Each c In rng
If c.Offset(0, 2) = "D" And c.Offset(0, 0)<> "" And c.Offset(0, 0) > c.Offset(0, 1) + 10 Then count = count + 1
Next c
[D1] = count
End Sub

What have I done wrong?

Also, is it possible to exclude weekends when counting the number of days in the macro?, i.e. 10 working days from the 05/07/2007 would be
19/07/2007.

Thanks
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
First off I would strongly urge you to not use count as a variable as it is a keyword in VBA. Not only can this cause programming issues sometimes it can confuse anyone who is trying to make sense of your code.

Code:
Sub Test1()

Dim rng As Range, c As Range
Dim x As Long
x = 0
Set rng = Range("A1:A20")
For Each c In rng
If c.Offset(0, 2).Value = "D" And c.Value <> "" And c.Value > c.Offset(0, 1).Value + 10 Then x = x + 1
Next c
Range("D1") = x

End Sub

Also, is it possible to exclude weekends when counting the number of days in the macro?, i.e. 10 working days from the 05/07/2007 would be
19/07/2007.
Why not just write a code that takes 14 days instead of 10?

Lastly I was not sure why you were checking each cell A to E I just left it as A.
 
Upvote 0
Thanks Brian

I still get Run-Time Error '13' type mismatch on line 6:

If c.Offset(0, 2).Value = "D" And c.Value <> "" And c.Value > c.Offset(0, 1).Value + 10 Then x = x + 1

An ideas?

Thanks
 
Upvote 0
First do you really need VBA for this?

Code:
=SUMPRODUCT(--(A1:A65535>B1:B65535 + 10),--(C1:C65535="D"))
should do the same thing with a formula as your VBA. However, if Column B of the range you are checking happens to have text in it both will fail (the code will error out and you will receive a #Value!). I guess you have text in column be somewhere?
 
Upvote 0
I posted too quick last time.... here is some VBA to take care of the text problem possibly. Some formula guru probably has some tricks to handle it without VBA.

Code:
Sub Test1()

Dim rng As Range, c As Range
Dim x As Long
x = 0
Set rng = Range("A1:A20")
For Each c In rng
    If IsDate(c.offset(0, 1).Value) Then
        If c.Offset(0, 2).Value = "D" And c.Value <> "" And c.Value > c.Offset(0, 1).Value + 10 Then x = x + 1
    End If
Next c
Range("D1") = x

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,172
Members
448,870
Latest member
max_pedreira

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