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
 

Some videos you may like

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

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")
 

Watch MrExcel Video

Forum statistics

Threads
1,119,268
Messages
5,577,102
Members
412,768
Latest member
klig
Top