+ Reply to Thread
Results 1 to 4 of 4

VBA Index/match Error 1004

Hybrid View

  1. #1
    Registered User
    Join Date
    08-08-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    2

    VBA Index/match Error 1004

    I'm trying to do a 2 way match using VBA, the lookup range is dynamic.

    Both lastRow and lookUpvalue are set as double

    lastRow = Worksheets("TPHPivot").UsedRange.Rows.Count
    
    For i = 2 To Worksheets("RoutingSpeed_MP").Range("G" & Rows.Count).End(xlUp).Row
       
    lookUpvalue = Application.WorksheetFunction.Index(Worksheets("TPHPivot").Range("E1001:P" & lastRow), Application.WorksheetFunction.Match(Worksheets("TPHEdit").Range("B" & i), Worksheets("TPHPivot").Range("D1001:D"), 0), Application.WorksheetFunction.Match(Worksheets("TPHEdit").Range("F" & i), Worksheets("TPHPivot").Range("E1000:P1000"), 0))
    
    ...
    
    Next i
    Not sure if the ranges are not written correctly. any help is appreciated. Thanks

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,051

    Re: VBA Index/match Error 1004

    Well, this bit's wrong:
    Range("D1001:D")
    Looks like it needs an "& lastrow" adding.

    Counters relating to rows would normally be Long. So, both lastrow and i should be Long. lookUpvalue would be whatever you expect to get back ... but you might get an error if it's, say, blank. Maybe safer to make it a Variant.

    If either of the Matches fail, you will get an Error, so you would need to use On Error Resume Next to start error trapping and then test whether or not the formula returned an error.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    08-08-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: VBA Index/match Error 1004

    Thanks so much!

    I have made the lookUpvalue into a variant, here's the entire code:

    Public Sub TPHupdate()
    
    Dim i As Long
    Dim lookUpvalue As Variant
    Dim lastRow As Long
    Dim endRow As Long
    
    lastRow = Worksheets("TPHPivot").UsedRange.Rows.Count
    endRow = Worksheets("RoutingSpeed_MP").Range("G" & Rows.Count).End(xlUp).Row
    
    For i = 2 To endRow
       
        lookUpvalue = Application.Index(Worksheets("TPHPivot").Range("E1001:U" & lastRow), Application.Match(Worksheets("TPHEdit").Range("B" & i), Worksheets("TPHPivot").Range("D1001:D" & lastRow), 0), Application.Match(Worksheets("TPHEdit").Range("F" & i), Worksheets("TPHPivot").Range("E1000:U1000"), 0))
    
        If Worksheets("RoutingSpeed_MP").Range("G" & i).Value <> lookUpvalue Then
            Worksheets("RoutingSpeed_MP").Range("G" & i).Value = lookUpvalue
        Else
            Worksheets("RoutingSpeed_MP").Range("G" & i).Value = Worksheets("RoutingSpeed_MP").Range("G" & i).Value
            
        End If
        
    Next i
    Now im getting a type mismatch error on the first IF statement, comparing a value to the variant. But when I add a msgbox for the lookupvalue, it seems to be returning the correct values going through the loop. Do I have to add something to it to reformat the variant?

  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: VBA Index/match Error 1004

    What happens if you change the code to this?
    Public Sub TPHupdate()
    
    Dim i As Long
    Dim lookUpvalue As Variant
    Dim lastRow As Long
    Dim endRow As Long
    
    lastRow = Worksheets("TPHPivot").UsedRange.Rows.Count
    endRow = Worksheets("RoutingSpeed_MP").Range("G" & Rows.Count).End(xlUp).Row
    
    For i = 2 To endRow
       
        lookUpvalue = Application.Index(Worksheets("TPHPivot").Range("E1001:U" & lastRow), Application.Match(Worksheets("TPHEdit").Range("B" & i), Worksheets("TPHPivot").Range("D1001:D" & lastRow), 0), Application.Match(Worksheets("TPHEdit").Range("F" & i), Worksheets("TPHPivot").Range("E1000:U1000"), 0))
    
        If IsError(lookUpvalue) Then
            MsgBox "Lookup error on row " & i & "."
        Else
            If Worksheets("RoutingSpeed_MP").Range("G" & i).Value <> lookUpvalue Then
                Worksheets("RoutingSpeed_MP").Range("G" & i).Value = lookUpvalue
            Else
                Worksheets("RoutingSpeed_MP").Range("G" & i).Value = Worksheets("RoutingSpeed_MP").Range("G" & i).Value
            
            End If
        End If
    Next i
    If posting code please use code tags, see here.

+ 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. runtime error 1004 unable to get match property
    By AAvcal71 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-16-2014, 03:10 PM
  2. [SOLVED] Match & Index code results in Runtime error 1004
    By DJvdW in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-27-2013, 03:52 AM
  3. Replies: 3
    Last Post: 05-02-2013, 01:31 AM
  4. [SOLVED] WorksheetFunction.match run time error '1004' with numbers but not text
    By Hussar13 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-13-2013, 05:31 PM
  5. worksheetfunction.match giving run time error '1004'
    By devo2511 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-15-2010, 04:47 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