Array - 'Type mismatch'

almagg

Well-known Member
Joined
Aug 21, 2002
Messages
705
i don't know what the problem is.
but i noticed that the variable 'myvalue' shows up in the 'Watch' window as an array.
which seems weird since i have not declared it as an array.

Option Base 1
Dim myarray()
Dim myvalue

Range(Selection, Selection.End(xlDown)).Select
elements = Selection.Count
ReDim myarray(elements)
For i = 1 To elements
.....myarray(i) = Selection.Offset(i - 1, 0).Value
Next i

For i = 1 To elements - 1
.....myvalue = myarray(i)
.....Range("D1") = myvalue 'THE CORRECT VALUE SHOWS UP IN D1. JUST A TEST
.....For j = 1 To elements - 1
..........If myvalue > myarray(j + 1) Then 'HERE I GET THE 'Type Mismatch' ERROR
...ETC.
can't go any further till i fix the above erro.

any ideas at all?
aaaack
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Is it possible that myvalue or myarray(j+1) is an error value (e.g. #DIV/0)

If so, the comparison CStr(myvalue) > CStr(myarray(j+1) should be used to convert the error value into a data type that > can handle, unless you are expecting numbers, in which case Val(CStr(...)) can be useds
 
Upvote 0
well i set up a short test list of 4 numbers in cells A1:A4
6,4,5,2
just to initially test the program.

and when i put the values of 'myvalue' and 'myarray(j + 1)' to check, the correct numbers appear.

i just tried bot CStr and Val(CStr(...
and still get the 'Type Mismatch' error

i just dunno
 
Upvote 0
also just to make something clear.

the variable 'myvalue' shows up in the Watch window as an array.
??
 
Upvote 0
I think I found the problem
Code:
Range(Selection, Selection.End(xlDown)).Select

elements = Selection.Count

ReDim myarray(elements)
For i = 1 To elements
    myarray(i) = Selection.Offset(i - 1, 0).Value
Next i

The first line makes Selection a multi-cell range
The line myarray(i) = Selection.Offset(i - 1, 0).Value takes the value of that (offset) array and puts in in myarray(i). So myArray is an array, each element of which is an array.

You might change that code to
Code:
Range(Selection, Selection.End(xlDown)).Select

elements = Selection.Count

ReDim myarray(elements)
For i = 1 To elements
    myarray(i) = Selection.Cells(i,1).Value
Next i

Or you could avoid the re-Selection entirly.

Code:
With Range(Selection, Selection.End(xlDown))
    elements = .Rows.Count

    ReDim myarray(1 to elements)

    For i = 1 To elements
        myarray(i) = .Cells(i,1).Value
    Next i
End With
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,762
Members
452,940
Latest member
rootytrip

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