+ Reply to Thread
Results 1 to 11 of 11

checking for value in array getting error message

Hybrid View

  1. #1
    Registered User
    Join Date
    10-25-2013
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    79

    checking for value in array getting error message

    i have an array and after it gets populated im trying to check if it contains a certain text. the array and certain text are dynamic and will be different every time i run the code. i found a function code that looks like it should work but i get a 'type mismatch' error when it runs through the function

    as below
    Sub newtest()
    Dim today As Date
    Dim ystrday As Date
    Dim twodays As Date
    Dim top As String
    Dim bot As String
    Dim home As String
    Dim away As String
    home = Workbooks("CHL Schedule.xlsm").Sheets(3).Range("W2")
    away = Workbooks("CHL Schedule.xlsm").Sheets(3).Range("V2")
    today = Workbooks("CHL Schedule.xlsm").Sheets(1).Range("B404").End(xlUp).Offset(1, -1)
    If today = "01/10/2002" Then
        ystrday = today
        twodays = today
    Else
        If today = "02/10/2002" Then
            ystrday = today - 1
            twodays = today
        Else
            ystrday = today - 1
            twodays = today - 2
        End If
    End If
    top = Workbooks("CHL Schedule.xlsm").Sheets(1).Range("A:A").Find(today).Address(False, False)
    bot = Workbooks("CHL Schedule.xlsm").Sheets(1).Range("A:A").Find(today, after:=Cells(3, 1), searchDirection:=xlPrevious).Address(False, False)
    Dim Arrtoday() As Variant
    Arrtoday = Range(Workbooks("CHL Schedule.xlsm").Sheets(1).Range(top).Offset(0, 1), Workbooks("CHL Schedule.xlsm").Sheets(1).Range(bot).Offset(0, 2))
    Dim R As Long
    Dim C As Long
    For R = 1 To UBound(Arrtoday, 1)
        For C = 1 To UBound(Arrtoday, 2)
            Debug.Print Arrtoday(R, C)
        Next C
    Next R
    If IsInArray(home, Arrtoday) or IsInArray(away, Arrtoday) Then
        'some code
    Else
        'some more code
    End If
    End Sub
    
    Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
      IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
    End Function
    i get the error code on this line:

    IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
    all i want to do is check to see if the array contains what home or away is and depending on if it is or not 2 different things will happen

    if anyone can see where im going wrong or if im missing something any help would be appreciated.

    thanks

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: checking for value in array getting error message

    Hello hopefulhart,

    This statement assigns a Range object to ArrToday.
    Arrtoday = Range(Workbooks("CHL Schedule.xlsm").Sheets(1).Range(top).Offset(0, 1), Workbooks("CHL Schedule.xlsm").Sheets(1).Range(bot).Offset(0, 2))
    To assign the Range values as 2-D array to the variable ArrToday use this...
    Arrtoday = Range(Workbooks("CHL Schedule.xlsm").Sheets(1).Range(top).Offset(0, 1), Workbooks("CHL Schedule.xlsm").Sheets(1).Range(bot).Offset(0, 2)).Value
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    10-25-2013
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    79

    Re: checking for value in array getting error message

    thanks for the reply Leith

    however, i changed the code as per your suggestion and i still receive the 'type mismatch' error at the same line

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: checking for value in array getting error message

    If you store the data in the array variable from the range, it will be 2D array, no exception.
    Filter function only works with single D array, so the error....

  5. #5
    Registered User
    Join Date
    10-25-2013
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    79

    Re: checking for value in array getting error message

    ok well it does have multiple rows and columns so i will work on a loop. i found one that i may be able to adjust for what i need

    thanks

  6. #6
    Registered User
    Join Date
    10-25-2013
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    79

    Re: checking for value in array getting error message

    aha! thanks for that jindon

    i see, so what would i use for a 2d array?

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: checking for value in array getting error message

    Either simple loop through the array or convert 2D array to a single D array only if the array does not have multiple rows and columns.

    To covert 2D array to single D array from the data that has one column with multiple rows
    Application.Transpose(ArrToday)
    Else single row with multiple columns
    Application.Transpose(Application.Transpose(ArrToday))

  8. #8
    Registered User
    Join Date
    10-25-2013
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    79

    Re: checking for value in array getting error message

    will do, may take me a little while to work it out but i will

    thanks again

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: checking for value in array getting error message

    No problem.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  10. #10
    Registered User
    Join Date
    10-25-2013
    Location
    canada
    MS-Off Ver
    Excel 2007
    Posts
    79

    Re: checking for value in array getting error message

    ok

    the loop seems to be working pretty well so far. my end goal is a little more complicated than my original post but so far i like it

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: checking for value in array getting error message

    Thanks.

    I also really prefer the loop because Filter function will pick up partial match, so it may not be the expected result, however you could still convert part of 2D array to a single D array using Index Function....

+ 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. [SOLVED] Formula array error message
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-29-2015, 05:12 PM
  2. How to get rid of an Error Checking symbol and message.
    By moosetales in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-03-2014, 02:37 PM
  3. [SOLVED] VBA Macro: Array-Formula, Error Message
    By Brianandstewie in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-20-2013, 04:19 PM
  4. checking cells are empty and giving error message
    By Nick_in_Dubai in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-15-2009, 05:17 AM
  5. Array Formula Error Message
    By elew69811 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-08-2008, 01:21 PM

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