+ Reply to Thread
Results 1 to 3 of 3

Setting variable equal to match result- error 2042

Hybrid View

Feethurt Setting variable equal to... 06-10-2019, 01:32 PM
Norie Re: Setting variable equal to... 06-10-2019, 01:42 PM
Feethurt Re: Setting variable equal to... 06-10-2019, 03:21 PM
  1. #1
    Registered User
    Join Date
    05-28-2019
    Location
    Indianapolis
    MS-Off Ver
    Excel 2016
    Posts
    12

    Setting variable equal to match result- error 2042

    Hello everyone,
    I am trying to write a macro that will first find what row number of a worksheet contains the column headers of some data, then searches that row for specific column names. In the long run, I plan to do it by using some kind of loop that will, using MATCH, search column A for every possible column header until it finds one.

    For now, I am just trying to use the MATCH function to search for one particular column header. I have a variable set equal to the MATCH function. My problem is that I keep getting error 2042 as the value of the variable, even though the header I am searching for is in column A of the worksheet I'm searching. I'm hoping that I'm just making some stupid syntax error, and that one of you might be able to see what it is.

    Here is just the relevant line:
     varRow = Application.Match("System Time", wbCW.Sheets(1).Range("A10"), 0)

    Thanks in advance!


    Here's my entire module, in case anyone actually wants to see it:
    Option Explicit
    
    Sub cy_pst()
    'Copy range of cells to Master Spreadsheet
        Dim lr As Long
        Dim lrC As Long
        Dim wbTarget As Workbook    'Current Open Workbook
        Dim wbCW As Workbook        'Source Data CW
        Dim wbCCW As Workbook       'Source Data CCW
        Dim wbSummary As Workbook   'Cumulative summary workbook
        Dim strCWFileToOpen As String 'Path for Source CW Data Spreadsheet
        Dim strCCWFileToOpen As String 'Path for Source CCW Data Spreadsheet
        Dim FileName As String      'Name for individual report file generated
        Dim varResult As Variant
        Dim varRow As Variant
        Application.ScreenUpdating = False
    
        'set the current active workbook
        Set wbTarget = ThisWorkbook
        'set the target workbook name
       
    
    strCWFileToOpen = Application.GetOpenFilename _
    (Title:="Please choose the clockwise data file", _
    FileFilter:="Excel Files *.xls* (*.xls*),")
    
    
    If strCWFileToOpen = "False" Then
        MsgBox "No file selected.", vbExclamation, "Sorry!"
        Exit Sub
    Else
        Set wbCW = Workbooks.Open(FileName:=strCWFileToOpen)
    End If
    
        varRow = Application.Match("System Time", wbCW.Sheets(1).Range("A10"), 0)
       
        'clear any thing on the clipboard to mazimize available memory
        Application.CutCopyMode = False
        'Copy Data in range A10:BG5233:
        wbCW.Worksheets(1).Range("A10:BG5233").Copy
        'paste the data to the target Worksheet
        wbTarget.Sheets("CW Data").Range("A10:BG5233").PasteSpecial
        Application.CutCopyMode = False
        wbCW.Close False
        Application.ScreenUpdating = True
        
      
        strCCWFileToOpen = Application.GetOpenFilename _
    (Title:="Please choose the counterclockwise data file", _
    FileFilter:="Excel Files *.xls* (*.xls*),")
    
    
    If strCCWFileToOpen = "False" Then
        MsgBox "No file selected.", vbExclamation, "Sorry!"
        Exit Sub
    Else
        Set wbCCW = Workbooks.Open(FileName:=strCCWFileToOpen)
    End If
    
        Application.ScreenUpdating = False
     'clear any thing on the clipboard to mazimize available memory
        Application.CutCopyMode = False
    'Copy Data in range A10:BG5233:
        wbCCW.Worksheets(1).Range("A10:BG5233").Copy
        'paste the data to the target Worksheet
        wbTarget.Sheets("CCW Data").Range("A10:BG5233").PasteSpecial
        'Clear the clipboard
        Application.CutCopyMode = False
        wbCCW.Close False
        
            
       Workbooks.Open FileName:= _
            "C:\Users\aselsley\Documents\EOL Stuff\Summary Sheet 5k-10k-15k.xlsx"
        Set wbSummary = ActiveWorkbook
        
        wbTarget.Sheets("CW Data").Activate
        wbTarget.Sheets("CW Data").Range("BN4:CF4").Copy
        wbSummary.Sheets("5k").Activate
        Dim lastrow As Long
        lastrow = Range("C65536").End(xlUp).Row
        Cells(lastrow + 1, 3).PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
        Cells(lastrow + 1, 2).Value2 = strCWFileToOpen
        Cells(lastrow + 1, 1).Formula = "CW"
        
        wbTarget.Sheets("CCW Data").Activate
        wbTarget.Sheets("CCW Data").Range("BN4:CF4").Copy
        wbSummary.Sheets("5k").Activate
        lastrow = Range("C65536").End(xlUp).Row
        Cells(lastrow + 1, 3).PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
        Cells(lastrow + 1, 2).Value2 = strCCWFileToOpen
        Cells(lastrow + 1, 1).Formula = "CCW"
        
        wbTarget.Sheets("CW Data").Activate
        wbTarget.Sheets("CW Data").Range("BN6:CF6").Copy
        wbSummary.Sheets("10k").Activate
        lastrow = Range("C65536").End(xlUp).Row
        Cells(lastrow + 1, 3).PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
        Cells(lastrow + 1, 2).Value2 = strCWFileToOpen
        Cells(lastrow + 1, 1).Formula = "CW"
        
        wbTarget.Sheets("CCW Data").Activate
        wbTarget.Sheets("CCW Data").Range("BN6:CF6").Copy
        wbSummary.Sheets("10k").Activate
        lastrow = Range("C65536").End(xlUp).Row
        Cells(lastrow + 1, 3).PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
        Cells(lastrow + 1, 2).Value2 = strCCWFileToOpen
        Cells(lastrow + 1, 1).Formula = "CCW"
        
        wbTarget.Sheets("CW Data").Activate
        wbTarget.Sheets("CW Data").Range("BN6:CF6").Copy
        wbSummary.Sheets("15k").Activate
        lastrow = Range("C65536").End(xlUp).Row
        Cells(lastrow + 1, 3).PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
        Cells(lastrow + 1, 2).Value2 = strCWFileToOpen
        Cells(lastrow + 1, 1).Formula = "CW"
        
         wbTarget.Sheets("CCW Data").Activate
        wbTarget.Sheets("CCW Data").Range("BN6:CF6").Copy
        wbSummary.Sheets("15k").Activate
        lastrow = Range("C65536").End(xlUp).Row
        Cells(lastrow + 1, 3).PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
        Cells(lastrow + 1, 2).Value2 = strCCWFileToOpen
        Cells(lastrow + 1, 1).Formula = "CCW"
        
        wbSummary.Save
        Application.ScreenUpdating = True
        
        wbTarget.Sheets("CW Data").Activate
        'displays the save file dialog
        varResult = Application.GetSaveAsFilename(FileFilter:= _
                     "Excel Files (*.xlsx), *.xlsx", Title:="Name the test report", _
                        InitialFileName:="\\Documents\EOL Stuff\")
    
            'checks to make sure the user hasn't canceled the dialog
            If varResult <> False Then
                wbTarget.SaveCopyAs varResult
             End If
    
    
        'clear memory
        Set wbTarget = Nothing
        Set wbCW = Nothing
        Set wbCCW = Nothing
        MsgBox "Data Transferred"
    End Sub

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

    Re: Setting variable equal to match result- error 2042

    Is the value you are looking for definitely in cell A10 of the worksheet wbCW.Sheets(1) refers to?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    05-28-2019
    Location
    Indianapolis
    MS-Off Ver
    Excel 2016
    Posts
    12

    Re: Setting variable equal to match result- error 2042

    Norie: Apparently that was my issue, I feel like such an idiot. I had an extra space in the spreadsheet that wasn't in the code! Sometimes I just need to step away from the computer and come back later.

+ 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. Modify DateTime for Application.Match - error 2042
    By domgilberto in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-21-2016, 09:02 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. setting variable equal to a value in a cell
    By ualdriver in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 02-25-2015, 06:01 AM
  4. Error 2042 on Application.Match formula
    By krazyhype19 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-05-2015, 03:25 AM
  5. [SOLVED] Error 2042 using Application.Match to add item and update combobox list
    By newbi004 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-14-2013, 07:55 AM
  6. [SOLVED] Application.Match returns Error 2042 whene text absolutely does exist?
    By Rhudi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-10-2013, 04:21 PM
  7. Error 2042 with Application.Match
    By mgurren in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-08-2013, 02:15 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