Access Multiple Files in a Folder and Perform Autofilter, Copy and Paste into another Workbook

ragav_in

Board Regular
Joined
Feb 13, 2006
Messages
89
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi team,

I am currently working on a testing project which needs to access multiple Test Result files and extract the Defects from them and put them into one Consolidated Sheet. With my limited knowledge in VBA and using the record macro feature, I was able to write this code that will do the following.
  1. Select the sheet named "MySheet"
  2. Perform Autofilter and Filter out values that are "FAIL" in Column M
  3. Copies the entire range of Column A to Column N (including Header Row) and paste it into the sheet named "Defect"
  4. Then it selects the next Sheet named "YourSheet"
  5. Performs Autofilter and Filter out values that are "FAIL" in Column M
  6. This time, it copies all the rows (except the Header Row), goes to the sheet named "Defect", finds the existing last row, and then pastes the content after that row
VBA Code:
Sub Ultim()
'
' Ultim Macro
'This macro will filter and copies all the FAIL records to the Data Defect Log


   Sheets("MySheet").Select
'    Sheets(1).Select
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.AutoFilter
    Range("M1").Select
    ActiveSheet.Range("$A:$N").AutoFilter Field:=13, Criteria1:="FAIL"
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Defect").Select
    Range("A1").Select
    ActiveSheet.Paste
   
    Sheets("YourSheet").Select
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.AutoFilter
    Range("M1").Select
'    ActiveSheet.Range("$A$1:$N$110").AutoFilter Field:=13, Criteria1:="FAIL"
    ActiveSheet.Range("$A:$N").AutoFilter Field:=13, Criteria1:="FAIL"

'   This line is to select only the values and not header again
    ActiveSheet.AutoFilter.Range.Offset(1).SpecialCells(xlCellTypeVisible).Cells(1, 1).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy

    Sheets("Defect").Select
    Range("A1").Select
    Selection.End(xlDown).Select
    'This line is for going to the last empty row in the existing Defect Sheet for pasting
    ActiveCell.Offset(1, 0).Select

    ActiveSheet.Paste
    Selection.End(xlDown).Select
    Range("A1").Select
    Selection.End(xlToRight).Select
    Range("N2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Application.CutCopyMode = False
    Range("N20").Select
    ActiveWorkbook.Save
End Sub

The above macro has some hard-coded values such as sheet name and also this works only when both the sheets (MySheet and YourSheet) are available in the same workbook in which the Macro is executed. However, my requirement is different as below.
  1. There will be a folder that will contain multiple individual workbooks.
  2. All workbooks will have only 1 sheet of the same format similar to MySheet or YourSheet, but the names are dynamic and unknown
  3. The "new" macro to be written should ask for the location of the folder where all Individual workbooks are available
  4. Once the path is chosen, the Macro should execute in each Individual workbook; i.e., Open each one of them, perform the Autofilter and Copy from the individual sheets, and paste them in another Workbook (which will have a sheet named Defect), and paste the data there
  5. Note that only the first time (first workbook), the Copy Paste should happen with the Header Rows.
  6. From the 2nd workbook onwards, the macro needs to copy-paste the rows excluding the Header Rows (as they are already present in the "Defect" sheet)
  7. The Macro should save the new workbook in the same folder as the Individual Files are available and provide some name (that can be customizable)
I have attached the "Defect Log.xlsm" with this post in which the current macro is available. Also, I have attached 2 individual workbooks that have sheets with the data in which the activity needs to be performed.

I would like to humbly request this community to help me with the above requirement. To date, we have many individual files that need to be accessed and data to be copy-pasted manually. If we could have the macro perform this task, there will be a reduction in human effort and also an increase in the correctness of the defect consolidation. I understand that this will be a little complicated and needs some effort to be put, and I would thank each and every individual for their time and effort.

Defect Log.xlsm
ABCDEFGHIJKLMN
1ProjectProcessAreaDataObjectTargetSourceABCDFIELDNAMECriticalRiskImpactPre_ValuePost_ValueResultCount
2ProjectProcessAreaDataObjectTargetSourceABCDFIELDNAMECriticalRiskImpactPre_ValuePost_ValueResultCount
3Project1PeopleObject 1Target 1FileXYZ530Field1Not CriticalHighPASS464
4Project1PeopleObject 2Target 2FileXYZ530Field2Not CriticalHigh0FAIL464
5Project1PeopleObject 3Target 3FileXYZ530Field2Not CriticalHigh0FAIL464
6Project1PeopleObject 4Target 4FileXYZ530Field3Not CriticalHighFAIL464
7Project1PeopleObject 5Target 5FileXYZ530Field4CriticalHighKey STFAIL5
8Project1PeopleObject 6Target 6FileXYZ530Field4CriticalHighKey PCESTFAIL459
9Project1PeopleObject 7Target 7FileXYZ530Field4CriticalHighKey PASS464
10Project1PeopleObject 8Target 8FileXYZ530Field4CriticalHighKey 0.000FAIL292
11Project1PeopleObject 9Target 9FileXYZ530Field4CriticalHighKey 10080.00010080.000PASS6
12Project1PeopleObject 10Target 10FileXYZ530Field4CriticalHighKey 1010.0001010.000PASS2
13Project1PeopleObject 11Target 11FileXYZ530Field4CriticalHighKey 1020.0001020.000PASS1
14Project1PeopleObject 12Target 12FileXYZ530Field4CriticalHighKey 10200.00010200.000PASS4
15Project1PeopleObject 13Target 13FileXYZ530Field4CriticalHighKey 10500.00010500.000PASS1
MySheet


Defect Log.xlsm
ABCDEFGHIJKLMN
1ProjectProcessAreaDataObjectTargetSourceABCDFIELDNAMECriticalRiskImpactPre_ValuePost_ValueResultCount
2ProjectProcessAreaDataObjectTargetSourceABCDFIELDNAMECriticalRiskImpactPre_ValuePost_ValueResultCount
3Project1ToolsData Object 1Target 1FileXYZ530Field5Not CriticalHighPASS464
4Project1ToolsData Object 2Target 2FileXYZ530Field6Not CriticalHighPASS464
5Project1ToolsData Object 3Target 3FileXYZ530Field6Not CriticalHigh0FAIL464
6Project1ToolsData Object 4Target 4FileXYZ530Field7Not CriticalHighFAIL464
7Project1ToolsData Object 5Target 5FileXYZ530Field8CriticalHighKey STFAIL5
8Project1ToolsData Object 6Target 6FileXYZ530Field8CriticalHighKey PCESTFAIL459
9Project1ToolsData Object 7Target 7FileXYZ530Field8CriticalHighKey PASS464
10Project1ToolsData Object 8Target 8FileXYZ530Field8CriticalHighKey 0.000FAIL292
11Project1ToolsData Object 9Target 9FileXYZ530Field8CriticalHighKey 10080.00010080.000PASS6
12Project1ToolsData Object 10Target 10FileXYZ530Field8CriticalHighKey 1010.0001010.000PASS2
YourSheet


Defect Log.xlsm
ABCDEFGHIJKLMN
1ProjectProcessAreaDataObjectTargetSourceABCDFIELDNAMECriticalRiskImpactPre_ValuePost_ValueResultCount
2Project1PeopleObject 2Target 2FileXYZ530Field2Not CriticalHigh0FAIL464
3Project1PeopleObject 3Target 3FileXYZ530Field2Not CriticalHigh0FAIL464
4Project1PeopleObject 4Target 4FileXYZ530Field3Not CriticalHighFAIL464
5Project1PeopleObject 5Target 5FileXYZ530Field4CriticalHighKey STFAIL5
6Project1PeopleObject 6Target 6FileXYZ530Field4CriticalHighKey PCESTFAIL459
7Project1PeopleObject 8Target 8FileXYZ530Field4CriticalHighKey 0.000FAIL292
8Project1PeopleObject 4Target 4FileXYZ530Field3Not CriticalHigh1280.0001280.000FAIL2
9Project1PeopleObject 4Target 4FileXYZ530Field3Not CriticalHigh1851.0001851.000FAIL1
10Project1PeopleObject 4Target 4FileXYZ530Field3Not CriticalHigh272.000272.000FAIL1
11Project1PeopleObject 4Target 4FileXYZ530Field3Not CriticalHigh3886.0003886.000FAIL1
12Project1PeopleObject 4Target 4FileXYZ530Field3Not CriticalHigh5760.0005760.000FAIL2
13Project1PeopleObject 4Target 4FileXYZ530Field3Not CriticalHigh838.000838.000FAIL1
14Project1PeopleObject 11Target 11FileXYZ530Field4CriticalHighKey 0.000FAIL464
15Project1ToolsData Object 3Target 3FileXYZ530Field6Not CriticalHigh0FAIL464
16Project1ToolsData Object 4Target 4FileXYZ530Field7Not CriticalHighFAIL464
17Project1ToolsData Object 5Target 5FileXYZ530Field8CriticalHighKey STFAIL5
18Project1ToolsData Object 6Target 6FileXYZ530Field8CriticalHighKey PCESTFAIL459
19Project1ToolsData Object 8Target 8FileXYZ530Field8CriticalHighKey 0.000FAIL292
20Project1ToolsData Object 11Target 11FileXYZ530Field8CriticalHighKey 0.000FAIL464
Defect
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Dear Alex, thanks for your time. I do not have knowledge on using Power Query till date. Also I am not sure of how it will be different from VBA Macro; hence wanted to have a VBA code written which can be re-used across my organization.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top