+ Reply to Thread
Results 1 to 2 of 2

Application.Worksheetfunction.match ERROR

Hybrid View

  1. #1
    Registered User
    Join Date
    04-02-2012
    Location
    Guatemala, Guatemala
    MS-Off Ver
    Excel 2010
    Posts
    31

    Application.Worksheetfunction.match ERROR

    Hi!

    I'm using a worksheet function in a piece of VBA code to get the column number of an specific date. It seems that the Match Function is unable to find the date even though the Date does exist in a column in the First row of the sheet "Lecturas". I've tried using the Match Function in Excel itself and the capturing the cell value t use it in mu code. This works but I'd rather be able to come up with this value using code only. The code I'm using is as follows:

    
    Private Function BuscaColumna() As Long
    
    
        Dim Columna As Long
        
        Dim Fecha As Date
        
        
        With Worksheets("Ingreso Lecturas")
        
            Fecha = DateValue("01/" & .Cells(4, 27) & "/" & Str(.Cells(5, 27)))
        
        End With
        
        
        With Worksheets("Lecturas")
            
            Columna = Application.WorksheetFunction.Match(Fecha, .Range(.Cells(1, 3), .Cells(1, .Range("1:1").Rows.Count)), 0)   <---------- THIS IS WHERE THE PROBLEM OCCURS
            
        End With
        
        BuscaColumna = Columna
    
    End Function
    Thanks!

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

    Re: Application.Worksheetfunction.match ERROR

    Try this.
    
    Private Function BuscaColumna() As Long
    
    
        Dim Columna As Variant
        
        Dim Fecha As Date
            
        With Worksheets("Ingreso Lecturas")
        
            Fecha = DateValue("01/" & .Cells(4, 27) & "/" & Str(.Cells(5, 27)))
        
        End With
        
        With Worksheets("Lecturas")
            
            Columna = Application.WorksheetFunction.Match(CDbl(Fecha), .Range(.Cells(1, 3), .Cells(1, .Range("1:1").Rows.Count)), 0) 
    
        End With
        
       If Not IsError(Columna) Then
        BuscaColumna = Columna
       End If
    
    End Function
    By the way, why are you using code for this?

    Why not use the worksheet functions MATCH and DATEVALUE?
    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)

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