+ Reply to Thread
Results 1 to 6 of 6

Capturing Application.Match error using loops

Hybrid View

  1. #1
    Registered User
    Join Date
    10-10-2018
    Location
    Budapest, Hungary
    MS-Off Ver
    MS Office 2010
    Posts
    2

    Capturing Application.Match error using loops

    I want to locate the position of two dates inside a list of sequential dates.

    The list is arranged earliest date to latest date.

    The first date is the start_date, if the start_date is not contained in the list, it should increase by one until a match occurs.

    The second date is the end_date, if the end_date is not contained in the list, it should decrease by one until a match occurs.

    I currently use Application.Match to look for each date, and unsuccessfully try to fit a loop to capture any error, and increase (decrease) the start (end) date.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Capturing Application.Match error using loops

    Why can not use For loop instaed of Do loops?

    One way to remove the error if there is no match.

    i = 0
    Do Until IsError(Application.Match(Cells(3, 3) + i, Columns("A:A"), 0)) = False
        start_date = Application.Match(Cells(3, 3) + i, Columns("A:A"), 0)
        If IsNumeric(start_date) Then
        
        
            i = i + 1
        End If
    Loop

  3. #3
    Valued Forum Contributor
    Join Date
    02-06-2014
    Location
    N/A
    MS-Off Ver
    N/A
    Posts
    373

    Re: Capturing Application.Match error using loops

    Hiya, I think something like this should work for you.

    Sub MATCH_CATCH()
    
        Dim ws As Worksheet
        Dim sDateStart As String
        Dim sDateEnd As String
        Dim nDistance As Double
        Dim nLR As Double
        Dim nRow As Double
        Dim bFound As Boolean
        
        Set ws = ThisWorkbook.Sheets("Sheet1")
        
        sDateStart = ws.Range("C3")
        sDateEnd = ws.Range("C4")
    
        nLR = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
        
        bFound = False
        
        For X = 2 To nLR
        
            If ws.Cells(X, 1) = sDateStart Then
            
                bFound = True
            
            End If
            
        Next X
        
        nDistance = DateDiff("d", sDateStart, ws.Cells(2, 1))
        nRow = 2
        
        If bFound = False Then
        
            For X = 3 To nLR
            
                If DateDiff("d", sDateStart, ws.Cells(X, 1)) < nDistance And DateDiff("d", sDateStart, ws.Cells(X, 1)) >= 0 Then
                
                    nDistance = DateDiff("d", sDateStart, ws.Cells(X, 1))
                    
                    nRow = X
                
                End If
            
            Next X
        
            ws.Range("C3") = ws.Cells(nRow, 1)
        
        End If
        
        bFound = False
        
        For X = 2 To nLR
        
            If ws.Cells(X, 1) = sDateEnd Then
            
                bFound = True
            
            End If
            
        Next X
        
        nDistance = DateDiff("d", ws.Cells(X, 1), sDateEnd)
        nRow = 2
        
        If bFound = False Then
        
            For X = 3 To nLR
            
                If DateDiff("d", ws.Cells(X, 1), sDateEnd) < nDistance And DateDiff("d", ws.Cells(X, 1), sDateEnd) >= 0 Then
                
                    nDistance = DateDiff("d", ws.Cells(X, 1), sDateEnd)
                    
                    nRow = X
                
                End If
            
            Next X
        
            ws.Range("C4") = ws.Cells(nRow, 1)
        
        End If
        
    End Sub

  4. #4
    Registered User
    Join Date
    10-10-2018
    Location
    Budapest, Hungary
    MS-Off Ver
    MS Office 2010
    Posts
    2

    Re: Capturing Application.Match error using loops

    AB33 I'm not exactly sure how to implement your suggestion.

    Poizhan your code is too advanced for my level.

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

    Re: Capturing Application.Match error using loops

    IRN_HU

    What result would you expect from the data in the workbook you uploaded?
    If posting code please use code tags, see here.

  6. #6
    Valued Forum Contributor
    Join Date
    02-06-2014
    Location
    N/A
    MS-Off Ver
    N/A
    Posts
    373

    Re: Capturing Application.Match error using loops

    Apologies for the complexity, I would be happy to chat and explain the logic / methodology

+ 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. Application Match error 13
    By willyman10 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2017, 01:24 AM
  2. [SOLVED] Error 2042 for application.match
    By a.hudrea in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-04-2016, 03:41 AM
  3. Error 2042 on Application.Match formula
    By krazyhype19 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-05-2015, 03:25 AM
  4. [SOLVED] Application.MoveAfterReturnDirection - Capturing cursor move direction
    By mowens74 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-11-2013, 10:08 AM
  5. Error 2042 with Application.Match
    By mgurren in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-08-2013, 02:15 AM
  6. Application.Worksheetfunction.match ERROR
    By Guido Meng in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-28-2013, 01:27 PM
  7. MATCH and capturing the error
    By Phil_V in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-06-2009, 09:21 AM

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