+ Reply to Thread
Results 1 to 11 of 11

Thread: Type mismatch error assigning range to array......

  1. #1
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Type mismatch error assigning range to array......

    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
    Attached Files Attached Files
    Last edited by welchs101; 02-12-2012 at 06:00 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    380

    Re: Type mismatch error assigning range to array......

    Possibly because you dimmed test_array as an array using ()?

  3. #3
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Type mismatch error assigning range to array......

    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.

  4. #4
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Type mismatch error assigning range to array......

    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
    array_Count = ws.Range("A2:A2").Cells.Count
    ReDim test_array(array_count)
    . If you remove the array brackets at the original declaration then your variant becomes a range of the cell(s) that you have declared in
    ws.Range("A2:A2")
    Please leave a message after the beep!

  5. #5
    Valued Forum Contributor
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    380

    Re: Type mismatch error assigning range to array......

    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.

  6. #6
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Type mismatch error assigning range to array......

    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?

  7. #7
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Type mismatch error assigning range to array......

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



  8. #8
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Type mismatch error assigning range to array......

    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:

     For x = 1 To UBound(test_array, 1)
        MsgBox (test_array(x, 1))
    
        Next x
    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.

    But it is interesting why by removing the "()" that this does not give a type mismatch errror

    test_array = ws.Range("A2:A2").Value2

  9. #9
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Type mismatch error assigning range to array......

    i have another question: i hope i am not asking to many questions

    i have defined this array as follows:

    Dim test_array As Variant
    And even if i redim it like this

    ReDim test_array(1 To 1, 1 To 1)
    what snb said holds true.......it converts the single value to a string/number and not an array.......

    so what am i to do..........since i cant redim this and make it an array what should i do?

  10. #10
    Valued Forum Contributor
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    Excel 2003, 2007, 2010
    Posts
    380

    Re: Type mismatch error assigning range to array......

    Depends on what you're trying to accomplish, but one possibility is using the IsArray function...
        If IsArray(test_array) Then

  11. #11
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Type mismatch error assigning range to array......

    if you remove () you redim the array variable to a variant variable.

    You can always check the kind of variable using

    msgbox typename(sn)



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0