Type Mismatch -- Error 13 in VBA

dmacdougall

New Member
Joined
Oct 18, 2005
Messages
43
Where I can find info on correcting the debugger error "Type Mismatch"?

The Excel help for this subject is rather general, and I'm not familiar enough with VBA to be sure which of my variables is causing this problem...

Also, why can I just get rid of this by using Variant-type variables? Arrrgh! :) dm
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

XLGibbs

Well-known Member
Joined
Feb 25, 2005
Messages
2,446
If you post your code it would be easier to diagnose.

The type mismatch is just that....you have a variable assigned that does not meet the expected variable format. Like you have text instead of an integer or something. Although this code be in the worksheet text that it is reading as well...

It is always better to specifiy your declared variable as the code works more efficiently. If you post your code and indicate the line in question, should be real easy to spot.
 

just_jon

Legend
Joined
Sep 3, 2002
Messages
10,473
Using Variants is just not good coding.

You can step through your code by getting into the VB Editor and clicking

Debug | step into

each time you hit F8, it'll execute a line of code and you can see the error line.
 

dmacdougall

New Member
Joined
Oct 18, 2005
Messages
43
Code is below -- I suspect the variable countyStr is the problem -- I changed it from a String type to Variant, but that did not solve the problem.

The data on the worksheet is a specific name of a county, formatted in the "Text" format.

Perhaps I need a Boolean-formatted variable to hold the data from the worksheet?

I will also double-check to make sure I'm not grabbing the data from the wrong cell, but I doubt that's the problem...

sigh... I wish I was a genius like Mr. Excel! :) dm





Private Sub CountTotalLosses()

Dim countyStr As Variant
Dim ctCounterInt As Integer
Dim tlCounterInt As Integer


ctCounterInt = 3
tlCounterInt = 0

Do While ctCounterInt < 100

countyStr = Sheets(Sheet1).Cells("B", ctCounterInt).Value

If Sheets(Sheet9).Cells("G", ctCounterInt).Value = countyStr And _
Sheets(Sheet9).Cells("N", ctCounterInt).Value = "True" Then

tlCounterInt = tlCounterInt + 1

Else

tlCounterInt = tlCounterInt + 0

ctCounterInt = ctCounterInt + 1

End If

Loop


End Sub
 

dmacdougall

New Member
Joined
Oct 18, 2005
Messages
43

ADVERTISEMENT

John -- tell me about it! I know almost nothing about programming, but when I saw how much space is allotted for a Variant, I hated it right from the beginning. But, I thought that perhaps it might be able to hold more type of data than the other variable types.

I will try your suggestion for running throught the code one line at a time -- I can seem to figure out where the parser is having a problem... thanks! :) dm
 

XLGibbs

Well-known Member
Joined
Feb 25, 2005
Messages
2,446
Try taking out the

.value

from the various lines in your statement
that should do it.

Code:
 countyStr = Sheets(Sheet1).Cells("B", ctCounterInt)

If Sheets(Sheet9).Cells("G", ctCounterInt) = countyStr And _
Sheets(Sheet9).Cells("N", ctCounterInt) = "True" Then 
[/code
 

dmacdougall

New Member
Joined
Oct 18, 2005
Messages
43

ADVERTISEMENT

Gibbs --

thank you!!! I'd forgotten that the .Value does try and pull the actual value, rather than just passing the data unchanged.

I will try and remove that and I'll post again if that does not work...
 

dmacdougall

New Member
Joined
Oct 18, 2005
Messages
43
OK, sorry to report that removing the ".Value" did not resolve the issue.

I guess the real question is whether this line is written correctly -- will this line of code insert the value in cell A3 in the first Sheet of the workbook in to the variable countyStr, if the variable ctCounterInt is equal to 3?

countyStr = Sheets(Sheet1).Cells("A", ctCounterInt)

Or am I just hopeless? :oops: :(
 

XLGibbs

Well-known Member
Joined
Feb 25, 2005
Messages
2,446
countyStr = Sheets(Sheet1).Cells("A", ctCounterInt)

should be

countyStr = Sheets("Sheet1").Cells(ctCounterInt,"A")
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,625
Messages
5,832,750
Members
430,163
Latest member
YesImAk

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
Top