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
Bookmarks