VBA Find Entries Dated Within Next 7 Days And Copy Matching Rows Into Another Workbook

annuity

New Member
Joined
Aug 29, 2017
Messages
6
Hi, Hope you are all well. You will see below two images which represent 2 separate workbooks. The first is the Source workbook and the second is the Destination workbook. What I need help in doing is producing VBA code that will copy the relevant information from the Source workbook into the Destination workbook as it's laid out in the second image.


If we assume the Destination workbook is blank to begin with, some of the information is fixed and won't need copying across from the Source workbook but will need to be factored in by the VBA code. For example most of the column headings apart from the League Name, which is variable and the Home & Away text in column B will always be the same.


The League Name can be found in cell E2 of the Source file.


The date can be found in the relevant row in column F of the Source file.


The main part of the code needs to look at each fixture in columns F to H of the Source file coming up in the next 7 days then match the Home team in column G to the Home Rankings table in column P and copy that row of data from P to X into the Destination workbook(in columns C to K). Then match the Away team in column H to the Away Rankings table in column Z and copy that row of data from Z to AH into the next row down of the Destination workbook(in columns C to K).


I will be running the code on more than one source file but i won't need it to loop through each one. I will run the code on the first source file then close it and open the second source file then run the code again. So it will need to look for the first blank row in the Destination file each time it runs. The structure of each source file is exactly the same so as long as it works for the first it will run for them all.


I have added links below to sample files on Dropbox for you to work with.

Source File - Dropbox - sample_data.xlsx

Destination File - Dropbox - Destination.xlsx

I'm really hoping some kind soul can help me please. I've been trying to do this myself for the last couple of weeks but being a novice have had to admit defeat. I hope i've explained things well enough but please ask if you need anything clarifying.

Source Workbook
Excel 2013/2016
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAH
1
2League NameHOME RANKINGSAWAY RANKINGS
3
4
5
6RdDateHomeAwayTeamFHFHHSHHATHDFHOFHRFHTeamFAFHASHAATADFAOFARFA
7121/08/17Team 1Team 30Team 1239341833Team 12104219171010
8121/08/17Team 2Team 29Team 2344721323Team 2311261292134
9121/08/17Team 3Team 28Team 3852108188Team 3817102417585
10121/08/17Team 4Team 27Team 4302973027252730Team 4302722222293029
11121/08/17Team 5Team 26Team 51327272224222413Team 513153311327
12121/08/17Team 6Team 25Team 67861451757Team 67141417152178
13121/08/17Team 7Team 24Team 7181117810191018Team 7182623232791811
14121/08/17Team 8Team 23Team 810232313310Team 81024730245102
15121/08/17Team 9Team 22Team 961015552556Team 968129713610
16121/08/17Team 10Team 21Team 102818102315271528Team 102830302829272818
17121/08/17Team 11Team 20Team 111272311331312Team 11122118162227127
18121/08/17Team 12Team 19Team 12411611314Team 12425172624541
19121/08/17Team 13Team 18Team 132525192724302425Team 13251929617212525
20121/08/17Team 14Team 17Team 1416131611819816Team 141615629991613
21121/08/17Team 15Team 16Team 15221713192212222Team 152218251517172217
22Team 162728182924272427Team 16272319182732728
23204/09/17Team 30Team 16Team 1759813108105Team 175782051759
24204/09/17Team 17Team 15Team 189222912228229Team 18933112922
25204/09/17Team 18Team 14Team 1914121491081014Team 19141320817131412
26204/09/17Team 19Team 13Team 201116201617131711Team 2011595791116
27204/09/17Team 20Team 12Team 21192622252752719Team 2119414481926
28204/09/17Team 21Team 11Team 222324122619221923Team 22231215131532324
29204/09/17Team 22Team 10Team 23114524138131Team 2312210121114
30204/09/17Team 23Team 9Team 24242125202742724Team 24241613149272421
31204/09/17Team 24Team 8Team 25211521151781721Team 25212216119302115
32204/09/17Team 25Team 7Team 2617611475717Team 26172824252421176
33204/09/17Team 26Team 6Team 27152026181951915Team 271562125171520
34204/09/17Team 27Team 5Team 282023282119221920Team 2820911199212023
35204/09/17Team 28Team 4Team 292619301715271526Team 292629282729132619
36204/09/17Team 29Team 3Team 302930242830193029Team 30292027717132930
37204/09/17Team 1Team 2
38
39305/09/17Team 2Team 30
40305/09/17Team 3Team 1
41305/09/17Team 4Team 29
42305/09/17Team 5Team 28
43305/09/17Team 6Team 27
44305/09/17Team 7Team 26
45305/09/17Team 8Team 25
46305/09/17Team 9Team 24
47305/09/17Team 10Team 23
48305/09/17Team 11Team 22
49305/09/17Team 12Team 21
50305/09/17Team 13Team 20

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1



Destination Workbook
Excel 2013/2016
ABCDEFGHIJK
1DateLeague NameFHFHSHAtDFOFRF
204/09/17HomeTeam 30131541611151615
304/09/17AwayTeam 167141313111322
404/09/17HomeTeam 1771131875
504/09/17AwayTeam 1511774101353
604/09/17HomeTeam 189133817115
704/09/17AwayTeam 141298311567
804/09/17HomeTeam 1946586445
904/09/17AwayTeam 139181812121777
1004/09/17HomeTeam 204811551439
1104/09/17AwayTeam 12108113410156
1204/09/17HomeTeam 2171131875
1304/09/17AwayTeam 1111774101353
1404/09/17HomeTeam 229133817115
1504/09/17AwayTeam 101298311567
1604/09/17HomeTeam 2346586445
1704/09/17AwayTeam 99181812121777
1804/09/17HomeTeam 244811551439
1904/09/17AwayTeam 8108113410156
2004/09/17HomeTeam 2571131875
2104/09/17AwayTeam 711774101353
2204/09/17HomeTeam 269133817115
2304/09/17AwayTeam 61298311567
2404/09/17HomeTeam 2746586445
2504/09/17AwayTeam 59181812121777
2604/09/17HomeTeam 284811551439
2704/09/17AwayTeam 4108113410156
2804/09/17HomeTeam 29108113410156
2904/09/17AwayTeam 3108113410156
3004/09/17HomeTeam 1108113410156
3104/09/17AwayTeam 2108113410156
32DateLeague 2 NameFHFHSHAtDFOFRF
3304/09/17HomeTeam 471131875
3404/09/17AwayTeam 211774101353
3504/09/17HomeTeam 59133817115
3604/09/17AwayTeam 61298311567
3704/09/17HomeTeam 746586445
3804/09/17AwayTeam 89181812121777
3904/09/17HomeTeam 94811551439
4004/09/17AwayTeam 1046586445

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Just wanted to add that the example source file i've provided with 30 teams is the maximum number of teams that any other source file will have so the code doesn't need to do full column ranges. Columns F to H will only need to go up to row 933 and columns P to X and Z to AH will only need to go to row 36. All other source files will fit within those ranges. Thanks.
 
Upvote 0
One final thing which might be worth mentioning is that the Source filenames will be different so it would be great if a specific filename wasn't referenced in the code for that but the Destination filename will always be the same so that can be. Thanks.
 
Upvote 0
Posted this in the wee hours. Hoping someone can help me with the above if possible please.
 
Last edited by a moderator:
Upvote 0
Would you consider this:
1) Have the macro code sitting inside the destination workbook
2) To run the macro, you would open the destination workbook and keep it open in the background. You would then open the Source workbook. Press a hotkey to invoke the macro.
3) The macro would always pull data from the active workbook and place the records in the destination workbook below any previous records.
 
Upvote 0
Just wanted to mention that the date format in column F of the Source file has now changed from my original post and sample data. It now includes the time and is formatted as follows 04.09. 19:30
 
Upvote 0

Forum statistics

Threads
1,214,400
Messages
6,119,289
Members
448,885
Latest member
LokiSonic

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