copying rows to other sheet if a condition is met- by using VBA code

Noni

Board Regular
Joined
Aug 27, 2022
Messages
63
Office Version
  1. 2021
Platform
  1. Windows
If in Sheet1, A2 exists in B2 then copy B2:F2 to Sheet2. If it doesn’t exist then ignore A2 and jump to A3 and check if it exists in B2. If True then copy B2:F2 to Sheet2, False then jump to A4………. Once this condition is run on all cells of Col A, then copy the remaining of Col B:F to Sheet 3.

In other words:

If 2022 client is also 2023 client then I want to move this old client’ detail to the next sheet. Once done checking 2022 clients then move new clients (2023)to sheet 3. Sheets 3 contains old clients(2022 and 2023) first then new clients(2023) details.

When we have run the above condition on all 2022 client addresses (in this example A2:A20), then copy the remaining 2023 client details at the bottom of Sheet2.

It is also important to keep the same order. By this I mean, that the table in Sheet3 must have the old client details (which have met the above condition) first and in the same order as they appear in Sheet1 A column. Then new clients’ details go to the bottom of the table in Sheet3.


Below is Sheet 1

VBA.xlsx
ABCDEF
12022 client address2023 client Address2023 client BB2023 client Cons2023 client Bld2023 client Other
29 Millow Road Alba VIC 7609215/20b Lexi Dr Bella Otta VIC 908712341234234512
334 Queen Street Kucha VIC 45677 Winter Street VIC 69784321546734
412 Winter Ave TSA 9876Cnr Hunter Road QLD 304598760340
576 Cat Street Martha QLD 345656 Spring Cres QLD 876756781245877
67 Queen Cres NSW 765412 Winter Ave TSA 98767834230345
79/54 Dairy Ave TSA 12343C Villa Street QLD 7686235645451245
876 Green Square Bella NSW 65478 Auburn Cres VIC 65431234000
93 Stanley Street Chilom TAS 798656 Spring Cres QLD 876765432300
1056 Hunter Road Chilom ACT 12349/54 Dairy Ave TSA 123412342300
1156 Spring Cres QLD 87677 Queen Cres NSW 7654123456340
12436 Power Street Quon DW 8765Cnr Hunter Road QLD 3045654307845
13215/20b Lexi Dr Bella Otta VIC 9087322 Lizardwood Road Maccarthur SA 09871276234561267
141234 Byor Road Narro VIC 2345Klirnc Avenue BALLA BALLA DW 12349854235689
156/8 Ryan Street Barrow QLD 765410 Somerset Street Aaron QLD 1245432587658934
167 Queen Cres NSW 76543 Stanley Street Chilom TAS 7986126512566456
1712 Alroy Road NSW 1234436 Power Street Quon DW 876512761234323
18Emu Pow/Quor Valley Way. Darra ACT 76541438 Alma Park Berro NSW 1209123487655656
19Klirnc Avenue BALLA BALLA DW 1234Emu Pow/Quor Valley Way. Darra ACT 76548765347834
2056 Spring Cres QLD 8767Bethel Road Wong SA 0987325423978
2198 Campbell Court Darrek QLD 564798767800
22Filo Centre Campo DC VIC 123432453456760
2322 Watson Ave PURNER VIC 1234238935650
Sheet1




Sheet 2

VBA.xlsx
ABCDE
12023 client Address2023 client BB2023 client Cons2023 client Bld2023 client Other
212 Winter Ave TSA 98767834230345
37 Queen Cres NSW 7654123456340
49/54 Dairy Ave TSA 123412342300
53 Stanley Street Chilom TAS 7986126512566456
656 Spring Cres QLD 876765432300
7436 Power Street Quon DW 876512761234323
8215/20b Lexi Dr Bella Otta VIC 908712341234234512
9Emu Pow/Quor Valley Way. Darra ACT 76548765347834
10Klirnc Avenue BALLA BALLA DW 12349854235689
117 Winter Street VIC 69784321546734
12Cnr Hunter Road QLD 304598760340
1356 Spring Cres QLD 876756781245877
143C Villa Street QLD 7686235645451245
158 Auburn Cres VIC 65431234000
16Cnr Hunter Road QLD 3045654307845
17322 Lizardwood Road Maccarthur SA 09871276234561267
1810 Somerset Street Aaron QLD 1245432587658934
191438 Alma Park Berro NSW 1209123487655656
20Bethel Road Wong SA 0987325423978
2198 Campbell Court Darrek QLD 564798767800
22Filo Centre Campo DC VIC 123432453456760
2322 Watson Ave PURNER VIC 1234238935650
Sheet2
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi there,

Data is in Sheet1 and Sheet2 is where I am copying rows if a condition is met.

The data (cell range) that need to be copied is in B:F (2022 client data).

If the last year’s client is also a client in this year 2022 then I want to move that client’s detail (which is in B:F) to Sheet2. Once all the last year’s 2021 clients are checked then copy new clients (2022) data to Sheet 2.

Below: blue highlighted rows are those which are clients in both 2021 and 2022 so their data gets copied first in Sheet2. Green highlighted ones are new clients, so their details get copied after in Sheet2. And in column A, non-highlighted ones are no longer our client, so we don’t copy them to Sheet2.

It is also important to keep the same order. By this I mean, that Sheet2 must have the old client details (which have met the above condition) first and in the same order as they appear in Sheet1 column A. Then new clients’ details go at the bottom of Sheet2. i.e. Peter who is a client in 2021-2022 gets copied first in sheet2. Then Sally goes to Sheet2, and then Michelle ….

Sheet 1
VBA.xlsx
ABCDEF
12021 client 2022 client 2022 client BB2022 client Cons2022 client Bld2022 client Other
2JamesMichael12341234234512
3SamSarah4321546734
4PeterMary98760340
5ShawRachel56781245877
6SallyAnna7834230345
7MichelleMonica235645451245
8IvonaCharles1234000
9AnnaPeter65432300
10ClaireAnthony12342300
11BenBen123456340
12MichaelElizabeth654307845
13DavidWong1276234561267
14AnnaleiseSally9854235689
15ChrisJay432587658934
16JohnMichelle126512566456
17BobDavid12761234323
18AnthonyJennifer123487655656
19SueSue8765347834
20RobinRuba325423978
21Henry98767800
22Chloe32453456760
23Candy238935650
Sheet1


Sheet2
VBA.xlsx
ABCDE
12022 client 2022 client BB2022 client Cons2022 client Bld2022 client Other
2Peter65432300
3Sally9854235689
4Michelle126512566456
5Anna7834230345
6Ben123456340
7Michael12341234234512
8David12761234323
9Anthony12342300
10Sue8765347834
11Sarah4321546734
12Mary98760340
13Rachel56781245877
14Monica235645451245
15Charles1234000
16Elizabeth654307845
17Wong1276234561267
18Jay432587658934
19Jennifer123487655656
20Ruba325423978
21Henry98767800
22Chloe32453456760
23Candy238935650
Sheet2
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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