Background:
Daily oil analysis reports from the lab come in and are stripped from e-mails into a folder. Saved in a folder by date, each sample dispatch may be between 1 and 20 samples, not every date, but any time of the day. The *.csv
files are stored in a dated folder, and the files are saved with their date and time. That part I have deconstructed from the VBA script I inherited.
These files are ignored until the end month (there is a parallel .pdf
report that we use for immediate action for the results), when they are rolled up into a monthly / continuous report for longer term trend analysis (each engine sampled only shows up once every two months or so). Problem appears that the lab has changed the .csv
report.
The original script took columns that appeared in regular well defined (as in fixed columns). The new report has dynamic columns (as in: if no data for any sample, no column). Worse, they have changed the order of the columns when they do present. The automated roll up report, which used to walk through each dated folder, extract each file, and append it to the master workbook has ceased to have meaning with the garbage data.
The easy solution, I feel, is to rebuild the data in each sample file, by rebuilding and reformatting the columns in a new sheet (searching for the column header, error capture if not present, copy and paste, reformat / do unit conversions etc.), and then copying that new sheet into the master, save and close the file, and repeat with the next one.
The issue I have is that when I pass file names, to the subroutine that opens the new file, every sheet that gets added is in the master file, and the subsequent cut an paste operations are failing (subscripts out of range, or my favorite 400). I am calling every book by the dated name, and every page by the Sheet designation, but the looped repeated activations have me getting lost.
I think I am referencing the sheets incorrectly based on the passed names. There must be an easier way. All other examples seem to have fixed names (if only life were that simple).
Update 1: Forgot to mention that I prototyped the spreadsheet build in one of the files, and then tried to transfer it into here as a sub-routine. Fell apart there. I have cleaned up much of the inherited code and economized the spaghetti, but this is my first attempt at manipulating another worksheet in another workbook from within the original calling one. Again, most examples used fixed names, which are not an option here.
Sub CopyPasteOAP(bookname)
Dim datarow As Long
'Grabs index of last data entered to prevent overwrite
ThisWorkbook.Activate
Sheet1.Range("B2").Activate
Range("B2").End(xlDown).Select
datarow = ActiveCell.Row
'Restructures the Data Columns into a new worksheet for copying
Workbooks(bookname).Activate
Worksheet.Add
'Date Column
Sheet1.Range("G1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Select Sheet2
Range("A1").Select
ActiveSheet.Paste
'Unit Column
Sheets.Select Sheet1
Range("B1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Select Sheet2
Range("B1").Select
ActiveSheet.Paste
'Enter in OAP
Sheets.Select Sheet2
Range("C1").Select
ActiveCell.FormulaR1C1 = "OAP"
Selection.Copy
Range("C2:C50").Select
ActiveSheet.Paste
'Fault Description = Lab Comments
Sheets.Select Sheet1
Cells.Find(What:="Lab Comments", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Select Sheet2
Range("D1").Select
ActiveSheet.Paste
'Severity Column
Sheets.Select Sheet1
Range("H1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Select Sheet2
Range("G1").Select
ActiveSheet.Paste
'Notes = Lab Recommendations
Sheets.Select Sheet1
Cells.Find(What:="Lab Recommendations", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Select Sheet2
Range("L1").Select
ActiveSheet.Paste
'Fuel in Oil
Sheets.Select Sheet1
Range("BL1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Select Sheet2
Range("M1").Select
ActiveSheet.Paste
'Coolant in Oil
Sheets.Select Sheet1
Range("BP1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Select Sheet2
Range("N1").Select
ActiveSheet.Paste
Sheets.Select Sheet1
'Copies from the new sheet
'Application.Workbooks(bookname).Activate
Sheets.Select Sheet1
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
'Pastes to the master copy
Application.ThisWorkbook.Activate
Sheet1.Range("A" & datarow + 1).Select
'Paste Special, Match Destination Format
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
'Adjusts Font (otherwise new is all neon green)
With Selection.Font
.ColorIndex = xlAutomatic
.TintAndShade = 0
End With
'Clears Clipboard because the prompt is annoying
Application.CutCopyMode = False
Application.Workbooks(bookname).Activate
ActiveWorkbook.Close False
End Sub