+ Reply to Thread
Results 1 to 8 of 8

type mismatch error when trying to populate array

Hybrid View

  1. #1
    Registered User
    Join Date
    11-15-2013
    Location
    Philadelphia, PA
    MS-Off Ver
    Latest
    Posts
    15

    type mismatch error when trying to populate array

    Hello!

    I'm attempting to get a Query table to filter with an array made from the values in column A of another sheet.

    I'm having the worst of luck...I was pretty sure this would work but for some reason I'm getting a "type mismatch error '13'" attached to the line that says "sArray(i) = var1(i, 1)"

    My code:
    Sub SON()
    
    Dim srange As Range
    Set srange = Sheet13.Range("A2:A60")
    Dim var1 As Variant
    Dim sArray() As String
    Dim i As Long
    var1 = srange.Value
    
    ReDim sArray(1 To UBound(var1))
    
    For i = 1 To (UBound(var1))
        sArray(i) = var1(i, 1) ' here's where the error is...
    Next
    
    Sheet12.ListObjects("Table_Query_from_ELHR").Range.AutoFilter Field:=5, _
            Criteria1:=sArray, Operator:=xlFilterValues
            
    End Sub
    Could anyone tell me what I'm doing wrong?
    Last edited by neek13; 12-21-2013 at 03:36 PM. Reason: Solved !

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: type mismatch error when trying to populate array

    If you step through the code does it give you that error on the first iteration of the loop?
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Registered User
    Join Date
    11-15-2013
    Location
    Philadelphia, PA
    MS-Off Ver
    Latest
    Posts
    15

    Re: type mismatch error when trying to populate array

    Hey Solus, thanks for the quick reply.

    It does work for the first loop. Actually, it works up until loop 50. I think it may have to do with the fact that there are values in the column, and at a certain point the rest of the values are zero. i.e. A2:A52 have values, then A53:A60 are just zeros. Could this be where the problem lies?

  4. #4
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: type mismatch error when trying to populate array

    Or it could be that there are more values in one array then you've dim'd for the second. You redim your array to Ubound of var but var starts from 0 not 1 so it will have at least one more value than sArray can hold.

  5. #5
    Registered User
    Join Date
    11-15-2013
    Location
    Philadelphia, PA
    MS-Off Ver
    Latest
    Posts
    15

    Re: type mismatch error when trying to populate array

    Thanks for your help Solus! I got it to work. Instead of defining a range, I did a rows.count and added Option Base 1 to the beginning.
    Here's what worked:
    Option Base 1
    
    Sub SON()
    
    Dim here As Integer
    
    here = Sheet1.Range("A:A").SpecialCells(xlCellTypeConstants).Rows.Count
    
    Dim srange As Range
    Set srange = Sheet13.Range("A2:A" & here & "")
    Dim var1 As Variant
    Dim sArray() As String
    Dim i As Long
    var1 = srange.Value
    
    ReDim sArray(1 To UBound(var1))
    
    For i = 1 To UBound(var1)
        sArray(i) = var1(i, 1)
    Next
    
    Sheet12.ListObjects("Table_Query_from_ELHR").Range.AutoFilter Field:=5, _
            Criteria1:=sArray, Operator:=xlFilterValues
            
    End Sub

  6. #6
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Re: type mismatch error when trying to populate array

    Hi, I would try explicitly casting to string type using
    Cstr(var(i,1))
    or I would test to see if the cell errors before assigning using resume next.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: type mismatch error when trying to populate array

    FWIW there's no need for Option Base 1, var1 will be populated with a 1-indexed 2-dimensional array.

    The problem was probably, as you mentioned, blank values in the array.
    If posting code please use code tags, see here.

  8. #8
    Registered User
    Join Date
    11-15-2013
    Location
    Philadelphia, PA
    MS-Off Ver
    Latest
    Posts
    15

    Re: type mismatch error when trying to populate array

    Norie, that makes a whole lot of sense. Thanks so much for your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Date array function has type mismatch error...
    By secret2 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-21-2011, 02:07 PM
  2. Multi-Level Array Type Mismatch Error
    By CrazyFileMaker in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-04-2009, 06:20 PM
  3. Array problem: Key words-Variant Array, single-element, type mismatch error
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-09-2005, 01:54 AM
  4. Array problem: Key words-Variant Array, single-element, type mismatch error
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2005, 12:30 AM
  5. Help: Compile error: type mismatch: array or user defined type expected
    By lvcha.gouqizi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2005, 05:05 PM

Tags for this Thread

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.6.0 RC 1