+ Reply to Thread
Results 1 to 10 of 10

Iserror/Index/Match in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    10-13-2008
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    85

    Iserror/Index/Match in VBA

    Hi All,

    I am wondering if you could please help me with the below code I have.

    For Each cell In Range("H4:H60")
    
    If IsError(Application.WorksheetFunction.Index(Sheets("PAN").Range("I1:K50"), Application.WorksheetFunction.Match(Range("B" & cell.Row), Sheets("PAN").Range("K1:K50"), 0), 1)) = False Then
    Range("B" & cell.Row).Text = "HA"
    
    End If
    Next
    I've never done an iserror, index or match in vba, so it's something new to me and I'm not exactly sure as to how to go about it.

    Also, while I'm here can you please explain when to use "Application." before "WorksheetFunction" and when not to, as I'm having trouble finding the differences.

    Your help is greatly appreciated.

    Benno
    Last edited by benno87; 04-01-2009 at 09:04 PM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Iserror/Index/Match in VBA

    What exactly do you want to do? There is probably a better approach using VBA.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    10-13-2008
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    85

    Re: Iserror/Index/Match in VBA

    Hi Roy,

    I basically just want to be able to do an index formula (in the cell), if it doesn't error. The B="HA" was purely for testing purposes.

    So if the index doesn't error, I want it to carry out the index (only in vba, as I do not want a formula in the formula bar and copy paste values is not what I'm after).

    Is there a simpler way?

    Thanks for your help,

    Benno

  4. #4
    Forum Contributor
    Join Date
    03-12-2009
    Location
    Calgary, Canada
    MS-Off Ver
    Excel 365
    Posts
    236

    Re: Iserror/Index/Match in VBA

    Could you just use a loop?

    for i = 1 to 10
         if range("A" & i).value <> "" then
                array(i) = range("A" & i).value
         end if
    next

  5. #5
    Forum Contributor
    Join Date
    03-12-2009
    Location
    Calgary, Canada
    MS-Off Ver
    Excel 365
    Posts
    236

    Re: Iserror/Index/Match in VBA

    Sorry if the above doesn't work, I didn't really test it, but it gives a general idea.

  6. #6
    Registered User
    Join Date
    10-13-2008
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    85

    Re: Iserror/Index/Match in VBA

    Thanks for your reply everstrivin but I've never used a loop before, so I don't really understand what is shown below. Do you have any suggestions on how to improve my index formula?

    Cheers

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Iserror/Index/Match in VBA

    In Loop Terms

    Dim vMatch As Variant
    Dim rngCell As Range
    Dim wsSource As Worksheet
    Set wsSource = Sheets("PAN")
    For Each rngCell In Range("H4:H60")
        vMatch = Application.Match(Cells(rngCell.Row, "B"), wsSource.Range("K1:K50"), 0)
        If IsNumeric(vMatch) Then
            rngCell.Value = Application.Index(wsSource.Range("I1:I50"), vMatch)
        Else
            rngCell.Value = "HA"
        End If
    Next rngCell
    Set wsSource = Nothing
    However, an alternate route...somewhat shorter

    With Range("H4:H60")
        .FormulaR1C1 = "=IF(ISNA(MATCH(RC2,PAN!R1C11:R50C11,0)),""HA"",INDEX(PAN!R1C9:R50C9,MATCH(RC2,PAN!R1C11:R50C11,0)))"
        .Value = .Value 'override formulae with value
    End With

  8. #8
    Registered User
    Join Date
    10-13-2008
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    85

    Re: Iserror/Index/Match in VBA

    Will this produce a formula in each cell? I want the cell to only contain the value (no formula).

    Thanks

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Iserror/Index/Match in VBA

    Why not test it and/or read the annotation ?

  10. #10
    Registered User
    Join Date
    10-13-2008
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    85

    Re: Iserror/Index/Match in VBA

    Donkeyote,

    I have now tried this and it work's great. I can use this in other applications also. Thanks for your help...Royuk also.

    Cheers

+ 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