Scope: I have several thousand files (14k right now) that are all in the same identical layout that I want to accumulate the data into one file. It's a collection of velocity testing data. Sheet1 is all single entry data >> one value always. Sheet2 is variable between 1-20 rows of data points. The accumulation of Sheet1 data points is always the same, accumulation of Sheet2 data points is variable.
Sheet 1:
Test: Single Value
Operator: Single Value
Test Date: Single Value
Test Time: Single Value
Gun: Single Value
Mfg / Model: Single Value
Caliber: Single Value
Serial #: Single Value
Powder: Single Value
Powder Wgt: Single Value
Lot Number: Single Value
Avg Velocity: Single Value
Sheet 2:
Rnd: Values are integers varying between 1-20 rows. (Rnd = Vel13 = Prf regarding the number of rows)
Vel13: Values are integers varying between 1-20 rows. (Rnd = Vel13 = Prf regarding the number of rows)
Prf: Values are integers varying between 1-20 rows. (Rnd = Vel13 = Prf regarding the number of rows)
File Name: Single Value
Question Part 1: I am successfully cycling through the files and collecting data for Sheet1. However, my runtime is about 28sec per 100 files. So about 66min at 14k files runtime whenever the sheet is updated. Not really acceptable... How do I speed this up?
Question Part 2: On Sheet2 I need to collect variable data using the Rnd column. See image. Data always begins in A4 so I'm trying to capture the range of (A4:LastRowTestData) using LastRowTestData = ActiveWorkbook.Sheets(2).Cells(Rows.Count, 1).End(xlUp).Row but it's not working. My guess is it's the variable being defined/passed incorrectly. I can run the below in the file I'm opening and it works just fine. When I run it from my "accumulation sheet" it doesn't work.
Dim LastRowTestData As Long
LastRowTestData = ActiveWorkbook.Sheets(2).Cells(Rows.Count, 1).End(xlUp).Row
MsgBox LastRowTestData
End Goal: Open File 1 in selected folder, copy the range of (A4:LastRowTestData), (B4:LastRowTestData), C4:LastRowTestData) from Sheet2 to the Accumulation Sheet under the proper header. Copy the single value data from Sheet1 to Accumulation Sheet. Duplicate the Sheet1 single values on the Accumulation sheet for the Sheet2 data length.
Index to the next file, repeat the process for all files.
Code:
Sub Accumulate_All_Test_Data()
Dim myPath As String
Dim myFile As String
Dim FldrPicker As FileDialog
Dim sh As Worksheet
Dim i As Long
Dim LastRowTestData As Long
Dim Test As String
Dim Operator As String
Dim Test_Date As String
Dim Test_Time As String
Dim Gun As String
Dim Mfg_Model As String
Dim Caliber As String
Dim Serial_Num As String
Dim Powder As String
Dim Powder_Wgt As String
Dim Lot_Number As String
Dim Avg_Velocity As String
Dim Rnd As String
Dim Vel13 As String
Dim Prf As String
Dim File_Name As String
'Assign column values to the data accumulation headers
Test = "B"
Operator = "C"
Test_Date = "D"
Test_Time = "E"
Gun = "F"
Mfg_Model = "G"
Caliber = "H"
Serial_Num = "I"
Powder = "J"
Powder_Wgt = "K"
Lot_Number = "L"
Avg_Velocity = "M"
Rnd = "N"
Vel13 = "O"
Prf = "P"
File_Name = "Q"
Application.ScreenUpdating = False
Set sh = ThisWorkbook.Sheets(1)
With sh 'Clear sheet contents and write data accumulation headers
.Cells.ClearContents
.Range(Test & 2) = "Test:"
.Range(Operator & 2) = "Operator:"
.Range(Test_Date & 2) = "Test Date:"
.Range(Test_Time & 2) = "Test Time:"
.Range(Gun & 2) = "Gun:"
.Range(Mfg_Model & 2) = "Mfg / Model:"
.Range(Caliber & 2) = "Caliber:"
.Range(Serial_Num & 2) = "Serial #:"
.Range(Powder & 2) = "Powder:"
.Range(Powder_Wgt & 2) = "Powder Wgt:"
.Range(Lot_Number & 2) = "Lot Number:"
.Range(Avg_Velocity & 2) = "Avg Velocity"
.Range(Rnd & 2) = "Rnd"
.Range(Vel13 & 2) = "Vel13"
.Range(Prf & 2) = "Prf"
.Range(File_Name & 2) = "File Name"
End With
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker 'Select folder to accumulate data from
.Title = "Please Select Folder That Test Data Files Are Located In"
.AllowMultiSelect = False
.ButtonName = "Confirm Folder Location"
If .Show = -1 Then
myPath = .SelectedItems(1) & "\"
Else
End
End If
End With
myFile = Dir(myPath)
i = 3
Do While (myFile <> "" And i < 20) 'Note that I'd remove the "and" condition for i < 103. I'm currently clocking about 28sec per 100 files and am limiting how many files I'm collecting.
Workbooks.Open Filename:=myPath & myFile
'Collect data from Sheet1
sh.Range(Test & i) = ActiveWorkbook.Sheets(1).Cells(2, 2).Text 'Copy value from Test Setup Sheet "Test:"
sh.Range(Operator & i) = ActiveWorkbook.Sheets(1).Cells(3, 2).Text 'Copy value from Test Setup Sheet "Operator:"
sh.Range(Test_Date & i) = ActiveWorkbook.Sheets(1).Cells(11, 2).Text 'Copy value from Test Setup Sheet "Test Date:"
sh.Range(Test_Time & i) = ActiveWorkbook.Sheets(1).Cells(12, 2).Text 'Copy value from Test Setup Sheet "Test Time:"
sh.Range(Gun & i) = ActiveWorkbook.Sheets(1).Cells(2, 5).Text 'Copy value from Test Setup Sheet "Gun:"
sh.Range(Mfg_Model & i) = ActiveWorkbook.Sheets(1).Cells(3, 5).Text 'Copy value from Test Setup Sheet "Mfg / Model:"
sh.Range(Caliber & i) = ActiveWorkbook.Sheets(1).Cells(4, 5).Text 'Copy value from Test Setup Sheet "Caliber:"
sh.Range(Serial_Num & i) = ActiveWorkbook.Sheets(1).Cells(5, 5).Text 'Copy value from Test Setup Sheet "Serial #:"
sh.Range(Powder & i) = ActiveWorkbook.Sheets(1).Cells(7, 8).Text 'Copy value from Test Setup Sheet "Powder:"
sh.Range(Powder_Wgt & i) = ActiveWorkbook.Sheets(1).Cells(8, 8).Text 'Copy value from Test Setup Sheet "Powder Wgt:"
sh.Range(Lot_Number & i) = ActiveWorkbook.Sheets(1).Cells(9, 8).Text 'Copy value from Test Setup Sheet "Lot Number:"
sh.Range(Avg_Velocity & i) = ActiveWorkbook.Sheets(1).Cells(13, 8).Text 'Copy value from Test Setup Sheet "Avg Velocity"
'Collect data from Sheet2
' LastRowTestData = ActiveWorkbook.Sheets(2).Cells(Rows.Count, 1).End(xlUp).Row NOT WORKING
' MsgBox LastRowTestData
' = ActiveWorkbook.Sheets(2).Range("A4:& ).Text 'Copy value from Test Data Sheet "Rnd"
' = ActiveWorkbook.Sheets(2).Range("B4:& ).Text 'Copy value from Test Data Sheet "Vel13"
' = ActiveWorkbook.Sheets(2).Range("C4:& ).Text 'Copy value from Test Data Sheet "Prf"
ActiveWorkbook.Close savechanges:=False
myFile = Dir
i = i + 1
Loop
Application.ScreenUpdating = True
End Sub
Sheet 1:
Test: Single Value
Operator: Single Value
Test Date: Single Value
Test Time: Single Value
Gun: Single Value
Mfg / Model: Single Value
Caliber: Single Value
Serial #: Single Value
Powder: Single Value
Powder Wgt: Single Value
Lot Number: Single Value
Avg Velocity: Single Value
Sheet 2:
Rnd: Values are integers varying between 1-20 rows. (Rnd = Vel13 = Prf regarding the number of rows)
Vel13: Values are integers varying between 1-20 rows. (Rnd = Vel13 = Prf regarding the number of rows)
Prf: Values are integers varying between 1-20 rows. (Rnd = Vel13 = Prf regarding the number of rows)
File Name: Single Value
Question Part 1: I am successfully cycling through the files and collecting data for Sheet1. However, my runtime is about 28sec per 100 files. So about 66min at 14k files runtime whenever the sheet is updated. Not really acceptable... How do I speed this up?
Question Part 2: On Sheet2 I need to collect variable data using the Rnd column. See image. Data always begins in A4 so I'm trying to capture the range of (A4:LastRowTestData) using LastRowTestData = ActiveWorkbook.Sheets(2).Cells(Rows.Count, 1).End(xlUp).Row but it's not working. My guess is it's the variable being defined/passed incorrectly. I can run the below in the file I'm opening and it works just fine. When I run it from my "accumulation sheet" it doesn't work.
Dim LastRowTestData As Long
LastRowTestData = ActiveWorkbook.Sheets(2).Cells(Rows.Count, 1).End(xlUp).Row
MsgBox LastRowTestData
End Goal: Open File 1 in selected folder, copy the range of (A4:LastRowTestData), (B4:LastRowTestData), C4:LastRowTestData) from Sheet2 to the Accumulation Sheet under the proper header. Copy the single value data from Sheet1 to Accumulation Sheet. Duplicate the Sheet1 single values on the Accumulation sheet for the Sheet2 data length.
Index to the next file, repeat the process for all files.
Code:
Sub Accumulate_All_Test_Data()
Dim myPath As String
Dim myFile As String
Dim FldrPicker As FileDialog
Dim sh As Worksheet
Dim i As Long
Dim LastRowTestData As Long
Dim Test As String
Dim Operator As String
Dim Test_Date As String
Dim Test_Time As String
Dim Gun As String
Dim Mfg_Model As String
Dim Caliber As String
Dim Serial_Num As String
Dim Powder As String
Dim Powder_Wgt As String
Dim Lot_Number As String
Dim Avg_Velocity As String
Dim Rnd As String
Dim Vel13 As String
Dim Prf As String
Dim File_Name As String
'Assign column values to the data accumulation headers
Test = "B"
Operator = "C"
Test_Date = "D"
Test_Time = "E"
Gun = "F"
Mfg_Model = "G"
Caliber = "H"
Serial_Num = "I"
Powder = "J"
Powder_Wgt = "K"
Lot_Number = "L"
Avg_Velocity = "M"
Rnd = "N"
Vel13 = "O"
Prf = "P"
File_Name = "Q"
Application.ScreenUpdating = False
Set sh = ThisWorkbook.Sheets(1)
With sh 'Clear sheet contents and write data accumulation headers
.Cells.ClearContents
.Range(Test & 2) = "Test:"
.Range(Operator & 2) = "Operator:"
.Range(Test_Date & 2) = "Test Date:"
.Range(Test_Time & 2) = "Test Time:"
.Range(Gun & 2) = "Gun:"
.Range(Mfg_Model & 2) = "Mfg / Model:"
.Range(Caliber & 2) = "Caliber:"
.Range(Serial_Num & 2) = "Serial #:"
.Range(Powder & 2) = "Powder:"
.Range(Powder_Wgt & 2) = "Powder Wgt:"
.Range(Lot_Number & 2) = "Lot Number:"
.Range(Avg_Velocity & 2) = "Avg Velocity"
.Range(Rnd & 2) = "Rnd"
.Range(Vel13 & 2) = "Vel13"
.Range(Prf & 2) = "Prf"
.Range(File_Name & 2) = "File Name"
End With
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker 'Select folder to accumulate data from
.Title = "Please Select Folder That Test Data Files Are Located In"
.AllowMultiSelect = False
.ButtonName = "Confirm Folder Location"
If .Show = -1 Then
myPath = .SelectedItems(1) & "\"
Else
End
End If
End With
myFile = Dir(myPath)
i = 3
Do While (myFile <> "" And i < 20) 'Note that I'd remove the "and" condition for i < 103. I'm currently clocking about 28sec per 100 files and am limiting how many files I'm collecting.
Workbooks.Open Filename:=myPath & myFile
'Collect data from Sheet1
sh.Range(Test & i) = ActiveWorkbook.Sheets(1).Cells(2, 2).Text 'Copy value from Test Setup Sheet "Test:"
sh.Range(Operator & i) = ActiveWorkbook.Sheets(1).Cells(3, 2).Text 'Copy value from Test Setup Sheet "Operator:"
sh.Range(Test_Date & i) = ActiveWorkbook.Sheets(1).Cells(11, 2).Text 'Copy value from Test Setup Sheet "Test Date:"
sh.Range(Test_Time & i) = ActiveWorkbook.Sheets(1).Cells(12, 2).Text 'Copy value from Test Setup Sheet "Test Time:"
sh.Range(Gun & i) = ActiveWorkbook.Sheets(1).Cells(2, 5).Text 'Copy value from Test Setup Sheet "Gun:"
sh.Range(Mfg_Model & i) = ActiveWorkbook.Sheets(1).Cells(3, 5).Text 'Copy value from Test Setup Sheet "Mfg / Model:"
sh.Range(Caliber & i) = ActiveWorkbook.Sheets(1).Cells(4, 5).Text 'Copy value from Test Setup Sheet "Caliber:"
sh.Range(Serial_Num & i) = ActiveWorkbook.Sheets(1).Cells(5, 5).Text 'Copy value from Test Setup Sheet "Serial #:"
sh.Range(Powder & i) = ActiveWorkbook.Sheets(1).Cells(7, 8).Text 'Copy value from Test Setup Sheet "Powder:"
sh.Range(Powder_Wgt & i) = ActiveWorkbook.Sheets(1).Cells(8, 8).Text 'Copy value from Test Setup Sheet "Powder Wgt:"
sh.Range(Lot_Number & i) = ActiveWorkbook.Sheets(1).Cells(9, 8).Text 'Copy value from Test Setup Sheet "Lot Number:"
sh.Range(Avg_Velocity & i) = ActiveWorkbook.Sheets(1).Cells(13, 8).Text 'Copy value from Test Setup Sheet "Avg Velocity"
'Collect data from Sheet2
' LastRowTestData = ActiveWorkbook.Sheets(2).Cells(Rows.Count, 1).End(xlUp).Row NOT WORKING
' MsgBox LastRowTestData
' = ActiveWorkbook.Sheets(2).Range("A4:& ).Text 'Copy value from Test Data Sheet "Rnd"
' = ActiveWorkbook.Sheets(2).Range("B4:& ).Text 'Copy value from Test Data Sheet "Vel13"
' = ActiveWorkbook.Sheets(2).Range("C4:& ).Text 'Copy value from Test Data Sheet "Prf"
ActiveWorkbook.Close savechanges:=False
myFile = Dir
i = i + 1
Loop
Application.ScreenUpdating = True
End Sub