Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to fix the Run-time error "13": Type mismatch? 1

Status
Not open for further replies.

feipezi

IS-IT--Management
Aug 10, 2006
316
US
Hi,
I'm running a extremely simple procedure but it gave me the error and I have no idea what Excel wants. Here goes the code.

Sub testing()
Dim DmnIt As Variant
Set DmnIt = Range("a10:f10").Value
For i = 1 To UBound(DmnIt, 1)
MsgBox i
Next
End Sub

It's a one-row range trying to read in as an Array. Error found on the 3rd row: the Set statement.
BTW, the values in DmnIt is "a, b, c, d, e, f".
Thanks in advance.
John Z.
 
Remove Set when assigning to your Variant. Set would be okay if you were assigning the Range itself, but not its .Value.
 
Hi folks,
Thanks for the input! But I did not make myself clear. What I want is a single row range (and I don't want to use Transpose unless it's necessary). Range("a1:a6") is a column range.

Dave,
I tried this following what you told me:

Sub testing()
Dim DmnIt As Variant
DmnIt = Range("a30:f30")
For i = 1 To UBound(DmnIt, 1)
If DmnIt(30, i) = "e" Then MsgBox i
Next
End Sub

But I got Run-time error: '9' indicating Excel won't recognize it's a row-range.

Of course, I can do a For loop:

...
For Each c in Range("a30:f30")
If c = "e" Then ...
Next
...

I'm sure it will work but if I have about 200 columns to search with more manipulation, Array should be more efficient. That's the whole idea.
Thanks again for your efforts.
John Z.
 





Code:
Sub testing()
    Dim DmnIt As Variant, i, j
    DmnIt = Selection
    For i = 1 To UBound(DmnIt, 2)
        For j = 1 To UBound(DmnIt, 1)
            If DmnIt(j, i) = 4 Then MsgBox i
        Next
    Next
End Sub


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Skip,
This is exactly what I want.
One last question. Is that safe if I do the following? I tested it and it worked too: just trying to get rid of one Loop.

Sub testing()
Dim DmnIt As Variant, i
DmnIt = range("a30:f30")
For i = 1 To UBound(DmnIt, 2)
If DmnIt(1, i) = "e" Then MsgBox i
Next
End Sub

Thanks a lot.
John Z.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top