Hi,
I am trying to accomplish the following, any tips/pointers you can provide will be greatly appreciated.
I have a table in a worksheet. I have another table (with the same structure) in another worksheet. The 2nd table contains records which may or may not already exist in the 1st table. When I press a button, I want to run a VBA code which loops through table #2. For every record, it should check whether the same record exists in table #1. If so, it should update the fields of the table #1 with the values found in table #2. If no record exists, it should add a new record to table #1 using the values found in table #2.
To make this a bit more visual, this is how the tables look like:
TABLE_1 in Sheet1:
FY-PERIOD and PROJECTID together make up the unique key for this table.
TABLE_2 in Sheet2:
After running the add/update VBA code, the TABLE_1 should look like as follows:
TABLE_1 in Sheet1 after the update (updates or additions marked in BOLD):
And finally, when I run another VBA sub, it should first erase TABLE_1 contents and the copy TABLE_1 to TABLE_2 in order to initialize it.
I realize that there are many ways via which this can be done in VBA. I am not asking for a ready made solution but for directions/guidance to follow on through. Such as how to loop through TABLE_2 and link the values read to TABLE_1. Thanks a lot for any help you can provide.
I am trying to accomplish the following, any tips/pointers you can provide will be greatly appreciated.
I have a table in a worksheet. I have another table (with the same structure) in another worksheet. The 2nd table contains records which may or may not already exist in the 1st table. When I press a button, I want to run a VBA code which loops through table #2. For every record, it should check whether the same record exists in table #1. If so, it should update the fields of the table #1 with the values found in table #2. If no record exists, it should add a new record to table #1 using the values found in table #2.
To make this a bit more visual, this is how the tables look like:
TABLE_1 in Sheet1:
FY_PERIOD | PROJECTID | ABC | DEF | GHI |
2020-10 | P000003288 | 55 | Text 1 | 100% |
2020-10 | P000003293 | 84 | Text 2 | 0% |
2020-10 | P000003837 | 666 | Text 3 | 50% |
2020-10 | P000003838 | 777 | Text 4 | 25% |
2020-10 | P000003839 | 555 | Text 5 | 0% |
FY-PERIOD and PROJECTID together make up the unique key for this table.
TABLE_2 in Sheet2:
FY_PERIOD | PROJECTID | ABC | DEF | GHI |
2020-10 | P000003288 | 88 | Text 1 | 90% |
2020-10 | P000003290 | 77 | Text B | 100% |
2020-10 | P000003837 | 680 | Text C | 50% |
2020-10 | P000003845 | 444 | Text D | 10% |
2020-10 | P000003856 | 333 | Text E | 100% |
2020-09 | P000003288 | 22 | Text F | 50% |
2020-11 | P000003288 | 55 | Text G | 15% |
After running the add/update VBA code, the TABLE_1 should look like as follows:
TABLE_1 in Sheet1 after the update (updates or additions marked in BOLD):
FY_PERIOD | PROJECTID | ABC | DEF | GHI |
2020-10 | P000003288 | 88 | Text 1 | 90% |
2020-10 | P000003293 | 84 | Text 2 | 0% |
2020-10 | P000003837 | 680 | Text C | 50% |
2020-10 | P000003838 | 777 | Text 4 | 25% |
2020-10 | P000003839 | 555 | Text 5 | 0% |
2020-10 | P000003290 | 77 | Text B | 100% |
2020-10 | P000003845 | 444 | Text D | 10% |
2020-10 | P000003856 | 333 | Text E | 100% |
2020-09 | P000003288 | 22 | Text F | 50% |
2020-11 | P000003288 | 55 | Text G | 15% |
And finally, when I run another VBA sub, it should first erase TABLE_1 contents and the copy TABLE_1 to TABLE_2 in order to initialize it.
I realize that there are many ways via which this can be done in VBA. I am not asking for a ready made solution but for directions/guidance to follow on through. Such as how to loop through TABLE_2 and link the values read to TABLE_1. Thanks a lot for any help you can provide.