I am new to VBA and could really use your help on this.
I have a folder that has 3 types of files (APL, PL, BPL) and a master file. I would like to pull specific data from specific sheets in each of these files and pull it back into the master file.
The issue I am having is there are over 50 files in this folder (1 APL to 19 APL, 20 PL to 40 PL, 41 BPL to 56 BPL).
How do I create a loop that will pull specific cells and paste them back in a orderly fashion( 1 through 56 rows) in the master file starting cells C5, D5, E5 in the CONSOLIDATION tab.
Each file in the folder should be labeled a number (1,2,3) followed by either APL, PL or BPL.
Based on the file name the loop should pull specific cells.
For any APL files - Copy summary tab Cells A1, A2 and A7 paste in master file Consolidation tab starting C5, D5, E5
For any PL files - copy Data tab cells D4, D7 and G98
For any BPL files - Liquidation tabl cells E4,R5,T6
Below is what my CONSOLIDATION sheet looks like
1 APL
2 APL
3 APL
4 APL
5 APL
6 APL
7 APL
8 APL
9 APL
10 APL
11 APL
12 APL
13 APL
14 APL
15 APL
16 APL
17 APL
18 APL
19 APL
20 PL
21 PL
22 PL
23 PL
24 PL
25 PL
26 PL
27 PL
28 PL
29 PL
30 PL
31 PL
32 PL
33 PL
34 PL
35 PL
36 PL
37 PL
38 PL
39 PL
40 PL
41 BPL
42 BPL
43 BPL
44 BPL
45 BPL
46 BPL
47 BPL
48 BPL
49 BPL
50 BPL
51 BPL
52 BPL
53 BPL
54 BPL
55 BPL
56 BPL
Here's some sample code to get you started. Once you understand the copy/paste parts, you should be able to adapt to the rest of your data and the rest of your files. This code needs to live in your master workbook and you must have the master workbook activated/selected when you start running the code.
Public Sub test()
Dim tempWb As Workbook
Dim masterWb As Workbook
Set masterWb = ActiveWorkbook
Dim Path As String
'change to your path
Path = "C:\TEMP\test\"
Filename = Dir(Path & "*.xlsm")
Do While Len(Filename) > 0
'make sure the file name contains 1 of the 3 strings
If InStr(Filename, "APL") > 0 Or InStr(Filename, "PL") Or InStr(Filename, "BPL") Then
'split the file name to get the number and file text
splitByPeriod = Split(Filename, ".")
splitBySpace = Split(splitByPeriod(0), " ")
Dim fileNumber As Integer
Dim fileText As String
fileNumber = splitBySpace(0)
fileText = splitBySpace(1)
'activate the master
masterWb.Activate
Worksheets("Consolidation").Activate
Dim rowNumber As Integer
rowNumber = fileNumber + 4
'open the file
Set tempWb = Workbooks.Open(Path & Filename)
If fileText = "APL" Then
'activate the workbook
tempWb.Activate
'activate the summary tab
Worksheets("summary").Activate
'copy the data in range A1
Range("A1").Select
Selection.Copy
'activate your summary workbook
masterWb.Activate
'activate the consolidation tab
Worksheets("Consolidation").Activate
'activate the cell to place the data in
Range("C" & rowNumber).Select
'paste the data
ActiveSheet.Paste
'repeat as necessary
'add additional copy/paste code here
ElseIf fileText = "PL" Then
'add copy/paste code
ElseIf fileText = "BPL" Then
'add copy/paste code
End If
'close the file, don't save, don't display alerts about not saving
Application.DisplayAlerts = False
tempWb.Close False
Application.DisplayAlerts = True
End If
Filename = Dir
Loop
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.