see enclosed file.
i created this test file to show what i am getting.
basically i am assigning the values in column A sheet1 to an array (not including the header) using this statement
test_array = ws.Range("A2:A2").Value2
And it gives me a type mismatch error.
If i change the above statement to the following it runs ok.......but i am not sure why.........any ideas?
test_array = ws.Range("A2:A3").Value2
Last edited by welchs101; 02-12-2012 at 06:00 AM.
Possibly because you dimmed test_array as an array using ()?
you are correct. If i remove the "()" then it runs fine..........
This is an opportunity to learn here for me.........
Why is this? Why does removing the "()" solve the problem.
Hi welches101, if you keep the () then you'll have to ReDim your array to let it know how big the array will be. For instance, the following is error free. If you remove the array brackets at the original declaration then your variant becomes a range of the cell(s) that you have declared inarray_Count = ws.Range("A2:A2").Cells.Count ReDim test_array(array_count)ws.Range("A2:A2")
Please leave a message after the beep!
Without the (), Excel will assign the type to a variant variable when it is used. By adding the (), you are telling Excel to make the variable a dynamic array with dimensions not yet defined.
thanks guys.
i did some reading as well and while i understand that the "()" means the array will be dynamic what i dont understand (and pardon me if you explained it and i dont understand yet) is if i change the code from
"A2:A2" to "A2:A3" it works.........
i guess this is what i dont understand at this point. I must be missing something. Could it have to do with only being a "single" element in the array vs. more than one?
VBA turns the value of 1 cell automatically into a string/number
the values of 2 cells or more can only be converted into a (Variant) array
Becasue a string/number <> an array you get a type mismatch
so:
sn=range("A1") results in a string or number
sn= range("A1:A2") results in a variant array
So the resulting string/number of sn=range("A1") doesn't match a dimmed array sn()
snb, thanks for the explanation. But one more question.
Why does then removing the "()" cause the type mismatch to go away?
I added this code to the end of the program:
What i found out is that even by removing the "()" what snb said holds true. When i try to use array type referencing like Ubound i get a type mismatch errror.For x = 1 To UBound(test_array, 1) MsgBox (test_array(x, 1)) Next x
But it is interesting why by removing the "()" that this does not give a type mismatch errror
test_array = ws.Range("A2:A2").Value2
i have another question: i hope i am not asking to many questions
i have defined this array as follows:
And even if i redim it like thisDim test_array As Variant
what snb said holds true.......it converts the single value to a string/number and not an array.......ReDim test_array(1 To 1, 1 To 1)
so what am i to do..........since i cant redim this and make it an array what should i do?
Depends on what you're trying to accomplish, but one possibility is using the IsArray function...
If IsArray(test_array) Then
if you remove () you redim the array variable to a variant variable.
You can always check the kind of variable using
msgbox typename(sn)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks