VBA to combine multiple rows into one

baby0bugs

New Member
Joined
Jun 9, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I have tried seveeral different scripts and formula, but I can't seem to get this right.

I have several different conditions, but if I can get help with the first one, then I can figure the rest out:

SI = Studlent ID
'MA = _MailingAddress Contact Type
'SA = ShippingAddress Contact Type
'COA = COA Contact Type

For each Student ID Matching Group

'First Case: MA, SA, & COA Exist

'IF _MailingAddress and COA are all listed as a Contact Type THEN
'Copy the Contact Address columns where the Contact Type = _MailingAddress to the Mailing columns and add "COA Pending" to Mailing2 column
'Copy the Contact Address columns where the Contact Type = ShippingAddress to the Shipping columns and add "COA Pending" to Shipping2 column

Here is the code I have. It's not looking at the Student ID, it's focused on the RegularAddress. I need it focused on the StudentID and cycle to the next one, after it has found all of the first one.

Note: The final spreadsheet will be sorted by the Student ID.

Below is the sample sheet:

Sample.xls
ABCDEFGHIJKL
1STUDENT_IDRegularAddress1RegularAddress2RegularCityRegularStateREgularZipCONTACT_TYPECONTACT_ADDRESS_1CONTACT_ADDRESS_2CONTACT_ADDRESS_CITYCONTACT_ADDRESS_STATECONTACT_ADDRESS_ZIP
246344608 Platte RoadFairless HillsPA19030Father608 Platte RoadFairless HillsPA19030
346344608 Platte RoadFairless HillsPA19030Mother608 Platte RoadFairless HillsPA19030
480173555 Anderson Street ExtensionMount JewettPA16740_MailingAddressPO Box 429Mount JewettPA16740
580173555 Anderson Street ExtensionMount JewettPA16740Father555 Anderson Street ExtensionMount JewettPA16740
680173555 Anderson Street ExtensionMount JewettPA16740Mother555 Anderson Street ExtensionMount JewettPA16740
7200082123 Academy RoadBrownsvillePA15417_MailingAddressPO Box 106MerrittstownPA15463
8200082123 Academy RoadBrownsvillePA15417Father123 Academy RoadMerrittstownPA15463
9200082123 Academy RoadBrownsvillePA15417Mother123 Academy RoadMerrittstownPA15463
10200246102 Division StreetPortlandPA18351Father102 Division StreetPortlandPA18351
11200246102 Division StreetPortlandPA18351Mother102 Division StreetPortlandPA18351
123068562553 Lynnwood DriveCOA PENDINGEast StroudsburgPA18302_MailingAddress221 Skyline Drive COA PendingEast StroudsburgPA18301
133068562553 Lynnwood DriveCOA PENDINGEast StroudsburgPA18302COA111 Aspen CircleCOA PendingEast StroudsburgPA18302
143068562553 Lynnwood DriveCOA PENDINGEast StroudsburgPA18302Father2553 Lynnwood DriveCOA PendingEast StroudsburgPA18302
153068562553 Lynnwood DriveCOA PENDINGEast StroudsburgPA18302Mother2553 Lynnwood DriveCOA PendingEast StroudsburgPA18302
163080662553 Lynnwood DriveEast StroudsburgPA18302COA111 Aspen CircleEast StroudsburgPA18302
173080662553 Lynnwood DriveEast StroudsburgPA18302Father2553 Lynnwood DriveEast StroudsburgPA18302
183080662553 Lynnwood DriveEast StroudsburgPA18302Mother2553 Lynnwood DriveEast StroudsburgPA18302
19308438112 Custer AvenueVandergriftPA15690_MailingAddressPO Box 4PA
20308438112 Custer AvenueVandergriftPA15690COA128 Elder Run RoadLeechburgPA15656
21308438112 Custer AvenueVandergriftPA15690Mother112 Custer AvenueVandergriftPA15690
Sample Data


Here is the Sample Output - I'm trying to achieve:
Sample.xls
ABCDEFGHIJKLMNOPQRSTUV
1STUDENT_IDRegularAddress1RegularAddress2RegularCityRegularStateREgularZipCONTACT_TYPECONTACT_ADDRESS_1CONTACT_ADDRESS_2CONTACT_ADDRESS_CITYCONTACT_ADDRESS_STATECONTACT_ADDRESS_ZIPMailing1Mailing2MailingCityMailingStateMailingZipShipping1Shipping2ShippingCityShippingStateShippingZip
246344608 Platte RoadFairless HillsPA19030Father608 Platte RoadFairless HillsPA19030608 Platte RoadFairless HillsPA19030608 Platte RoadFairless HillsPA19030
346344608 Platte RoadFairless HillsPA19030Mother608 Platte RoadFairless HillsPA19030608 Platte RoadFairless HillsPA19030608 Platte RoadFairless HillsPA19030
480173555 Anderson Street ExtensionMount JewettPA16740_MailingAddressPO Box 429Mount JewettPA16740PO Box 429Mount JewettPA16740555 Anderson Street ExtensionMount JewettPA16740
580173555 Anderson Street ExtensionMount JewettPA16740Father555 Anderson Street ExtensionMount JewettPA16740PO Box 429Mount JewettPA16740555 Anderson Street ExtensionMount JewettPA16740
680173555 Anderson Street ExtensionMount JewettPA16740Mother555 Anderson Street ExtensionMount JewettPA16740PO Box 429Mount JewettPA16740555 Anderson Street ExtensionMount JewettPA16740
7200082123 Academy RoadBrownsvillePA15417_MailingAddressPO Box 106MerrittstownPA15463PO Box 106MerrittstownPA15463123 Academy RoadBrownsvillePA15417
8200082123 Academy RoadBrownsvillePA15417Father123 Academy RoadMerrittstownPA15463PO Box 106MerrittstownPA15463123 Academy RoadBrownsvillePA15417
9200082123 Academy RoadBrownsvillePA15417Mother123 Academy RoadMerrittstownPA15463PO Box 106MerrittstownPA15463123 Academy RoadBrownsvillePA15417
10200246102 Division StreetPortlandPA18351Father102 Division StreetPortlandPA18351102 Division StreetPortlandPA18351102 Division StreetPortlandPA18351
11200246102 Division StreetPortlandPA18351Mother102 Division StreetPortlandPA18351102 Division StreetPortlandPA18351102 Division StreetPortlandPA18351
123068562553 Lynnwood DriveEast StroudsburgPA18302_MailingAddress221 Skyline Drive COA PendingEast StroudsburgPA18301221 Skyline Drive COA PendingEast StroudsburgPA18301221 Skyline Drive COA PendingEast StroudsburgPA18301
133068562553 Lynnwood DriveEast StroudsburgPA18302COA111 Aspen CircleCOA PendingEast StroudsburgPA18302221 Skyline Drive COA PendingEast StroudsburgPA18301221 Skyline Drive COA PendingEast StroudsburgPA18301
143068562553 Lynnwood DriveEast StroudsburgPA18302Father2553 Lynnwood DriveCOA PendingEast StroudsburgPA18302221 Skyline Drive COA PendingEast StroudsburgPA18301221 Skyline Drive COA PendingEast StroudsburgPA18301
153068562553 Lynnwood DriveEast StroudsburgPA18302Mother2553 Lynnwood DriveCOA PendingEast StroudsburgPA18302221 Skyline Drive COA PendingEast StroudsburgPA18301221 Skyline Drive COA PendingEast StroudsburgPA18301
163080662553 Lynnwood DriveEast StroudsburgPA18302COA111 Aspen CircleEast StroudsburgPA183022553 Lynnwood DriveCOA PendingEast StroudsburgPA183022553 Lynnwood DriveCOA PendingEast StroudsburgPA18302
173080662553 Lynnwood DriveEast StroudsburgPA18302Father2553 Lynnwood DriveEast StroudsburgPA183022553 Lynnwood DriveCOA PendingEast StroudsburgPA183022553 Lynnwood DriveCOA PendingEast StroudsburgPA18302
183080662553 Lynnwood DriveEast StroudsburgPA18302Mother2553 Lynnwood DriveEast StroudsburgPA183022553 Lynnwood DriveCOA PendingEast StroudsburgPA183022553 Lynnwood DriveCOA PendingEast StroudsburgPA18302
19308438112 Custer AvenueVandergriftPA15690_MailingAddressPO Box 4PAPO Box 4COA PendingPA112 Custer AvenueCOA PendingVandergriftPA15690
20308438112 Custer AvenueVandergriftPA15690COA128 Elder Run RoadLeechburgPA15656PO Box 4COA PendingPA112 Custer AvenueCOA PendingVandergriftPA15690
21308438112 Custer AvenueVandergriftPA15690Mother112 Custer AvenueVandergriftPA15690PO Box 4COA PendingPA112 Custer AvenueCOA PendingVandergriftPA15690
222045615732 Chestnut StreetPhiladelphiaPA19139COA6010 Cedarhurst StreetPhiladelphiaPA19143P.O. Box COA PendingPhiladelphiaPA191395905 Cobbs Creek ParkwayCOA PendingPhiladelphiaPA19149
232045615732 Chestnut StreetPhiladelphiaPA19139_MailingAddressP.O. Box PhiladelphiaPA19139P.O. Box COA PendingPhiladelphiaPA191395905 Cobbs Creek ParkwayCOA PendingPhiladelphiaPA19149
242045615732 Chestnut StreetPhiladelphiaPA19139Other6010 Cedarhurst StreetPhiladelphiaPA19143P.O. Box COA PendingPhiladelphiaPA191395905 Cobbs Creek ParkwayCOA PendingPhiladelphiaPA19149
252045615732 Chestnut StreetPhiladelphiaPA19139ShippingAddress5905 Cobbs Creek ParkwayPhiladelphiaPA19149P.O. Box COA PendingPhiladelphiaPA191395905 Cobbs Creek ParkwayCOA PendingPhiladelphiaPA19149
Output


And here are all of the Conditions:

Sample.xls
A
1SI = Studlent ID
2'MA = _MailingAddress Contact Type
3'SA = ShippingAddress Contact Type
4'COA = COA Contact Type
5
6For each Student ID Matching Group
7
8'First Case: MA, SA, & COA Exist
9
10 'IF _MailingAddress and COA are all listed as a Contact Type THEN
11 'Copy the Contact Address columns where the Contact Type = _MailingAddress to the Mailing columns and add "COA Pending" to Mailing2 column
12 'Copy the Contact Address columns where the Contact Type = ShippingAddress to the Shipping columns and add "COA Pending" to Shipping2 column
13
14'Second Case: MA & SA Exist, COA Does Not
15
16 'IF _MailingAddress and ShippingSAddress ARE listed as a Contact Type but COA is NOT listed as a Contact Type THEN
17 'Copy the Contact Address columns where the Contact Type = _MailingAddress to the Mailing columns
18 'Copy the Contact Address columns where the Contact Type = ShippingAddress to the Shipping columns
19
20'Third Case: MA & COA, but not SA
21
22 'If the _MailingAddress and the COA ARE listed as a Contsact Type but SA is NOT listed as a Contact Type THEN
23 'Copy the Contact Address columns where the Contact Type = _MailingAddress to the Mailing columns and add "COA Pending" to Mailing2 column
24 'Copy the Contact Address columns where the Contact Type = COA to the Shipping columns
25
26'Fourth Case: SA & COA, but not MA
27
28 'If ShippingSAddress and COA ARE listed as a Contact Type but MA is NOT listed as a Contact Type THEN
29 'Copy the Contact Address columns where the Contact Type = COA to the Mailing columns
30 'Copy the Contact Address columns where the Contact Type = ShippingAddress to the Shipping columns
31
32'Fifth Case: COA, but not MA or SA
33 'If COA IS listed as a Contact Type but MA and SA ARE NOT listed as a Contact Type THEN
34 'Copy the Contact Address columns where the Contact Type = COA to the Mailing columns
35 'Copy the Contact Address columns where the Contact Type = COA to the Shipping columns
36
37'Sixth Case: No MA, SA or COA
38 'If the _MailingAddress, ShippingAddress and COA is NOT listed as a Contact Type THEN
39 'Copy the Regular Address columns to the Mailing columns
40 'Copy the Regular Address columns to the Shipping columns
Conditions
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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