+ Reply to Thread
Results 1 to 9 of 9

error using index and match functions in macro

  1. #1
    Registered User
    Join Date
    12-19-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    13

    error using index and match functions in macro

    I'm fairly new to using the index and match functions and this is my first time trying them in a macro. I have a workbook with multiple sheets and in the "Master" sheet I get an error when I run the following code:

    Please Login or Register  to view this content.

    The error statement says: "unable to get index property of worksheetfunction class."

    I'm not sure why because the code runs the first time through the loop and does what it should, but on the second iteration I get the error.

  2. #2
    Forum Expert
    Join Date
    11-28-2012
    Location
    Guatemala
    MS-Off Ver
    Excel 2010
    Posts
    2,394

    Re: error using index and match functions in macro

    The reason is that if there is no match it post an error
    The best way to control it is to add
    Please Login or Register  to view this content.
    beware that if an error occurs and resumes the cell or the assigned variable will be empty

  3. #3
    Registered User
    Join Date
    12-19-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: error using index and match functions in macro

    I appreciate your help and that's actually a very useful tip. As you predicted my code runs fine, but the cell is empty. That narrows down the problem to the formula not finding an exact match.

    I'm perplexed though because if I enter the following formula manually in my spreadsheet it works for every cell:
    =INDEX(Master!C2,MATCH(G2,Master!D2:D6,0))

    The syntax I have in vba is:
    Please Login or Register  to view this content.
    Note: I changed the order of the values in the Match function because it seems to make more sense this way.

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

    Re: error using index and match functions in macro

    Are you sure that formula works?

    If the MATCH returned anything other than 1 you would get a #REF! error.

    Could you attach a sample workbook?
    If posting code please use code tags, see here.

  5. #5
    Forum Expert
    Join Date
    11-28-2012
    Location
    Guatemala
    MS-Off Ver
    Excel 2010
    Posts
    2,394

    Re: error using index and match functions in macro

    I would break the formula down assigning variables just to be able to "control" their values
    for ex. assign cellseeked=sheets(x).cell(i,7) and so forth since I have encountered many forgotten proper initializations before. It is still a mystery to me why do these worksheet functions work or not work...

  6. #6
    Registered User
    Join Date
    12-19-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: error using index and match functions in macro

    Thanks for the quick response. I've deleted the non-relevant sheets and attached a sample of the workbook. The code runs when the button on the "Master" sheet is pressed. I have the manual formula as a test in the "Debbie" sheet in column "P" and it is working there.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    12-19-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: error using index and match functions in macro

    Thanks for the tip rcm, I'll try breaking the formula down in to variables. That should help me debug the code better too.

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

    Re: error using index and match functions in macro

    The code doesn't work for the same reason the formula doesn't work on the worksheet - the MATCH returns a value more than 1.

    If you change the code to this it will work.
    Please Login or Register  to view this content.
    PS Try this formula in column P on the 'Debbie' sheet.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    12-19-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: error using index and match functions in macro

    The code works beautifully now, thanks so much for your help Norie!
    I understand the formula better now to.

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