why VBA code not working for copying data to next Sheet

Noni

Board Regular
Joined
Aug 27, 2022
Messages
63
Office Version
  1. 2021
Platform
  1. Windows
In Sheet1, A:D has data about this year’s clients and column E has names of last year’s clients. I want to copy 2022 clients’ data who were with us in the year 2021 as well into Sheet2, in non-filled cells of columns A:D.
Secondly, columns A and E don’t have the same number of filled cells. Let’s say last year we had 23 clients but this year it may increase to 23 or decrease to 15. ( I don’t know below code will take care of this or not)
In the below code, you see the condition in IF statement is if E2=A2, then copy A2:D2 to Sheet2. The logic behind this is the order we need to keep in Sheet2. 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 E. i.e. Peter who is a client in both year 2021-2022 gets copied first in Sheet2. Then Sally goes to Sheet2, and then Michelle ….
Below screenshots: 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(not yet coded). And in column E in Sheet1, non-highlighted ones are no longer our clients, so we don’t copy them to Sheet2.
The VBA code for the above situation is down below. But when I click the command button, nothing happens.
Sheet1
Worksheets.xlsm
ABCDE
12022 Clients2022 Data2022 Data22022 Data32021 Clients
2Michael123412342345James
3Sarah43215467Sam
4Mary9876034Peter
5Rachel56781245Shaw
6Anna7834230Sally
7Monica23564545Michelle
8Charles123400Ivona
9Peter6543230Anna
10Anthony1234230Claire
11Ben12345634Ben
12Elizabeth6543078Michael
13Wong12762345612David
14Sally98542356Annaleise
15Jay4325876589Chris
16Michelle1265125664John
17David127612343Bob
18Jennifer1234876556Anthony
19Sue87653478Sue
20Ruba3254239Robin
21Henry9876780
22Chloe3245345676
23Candy23893565
24
25
26
27
28
29
30
Sheet1


Sheet2
Worksheets.xlsm
ABCD
12022 Clients2022 Data2022 Data22022 Data3
2Peter6543230
3Sally98542356
4Michelle1265125664
5Anna7834230
6Ben12345634
7Michael123412342345
8David127612343
9Anthony1234230
10Sue87653478
Sheet2



VBA code that is not working

VBA Code:
Private Sub CommandButton1_Click()
    
    a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
      
  
    For i = 2 To a
    
        If Worksheets("Sheet1").Cells(i, 5).Value = Worksheets("Sheet1").Cells(i, 2).Value Then
    
            Worksheets("Sheet1").Range("A:A,B:B,C:C,D:D").Copy
    
            Worksheets("Sheet2").Activate
    
    
            b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
     
            Worksheets("Sheet2").Cells(b + 1).Select
     
            ActiveSheet.Paste
     
            Worksheets("Sheet1").Activate
     
     End If
     
    
Next

Application.CutCopyMode = False

ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select


End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try..
VBA Code:
Worksheets("Sheet2").Cells(b + 1, 1).Select
 
Upvote 0
I have not checked the whole code, but for one thing ..

In the below code, you see the condition in IF statement is if E2=A2, then
Rich (BB code):
If Worksheets("Sheet1").Cells(i, 5).Value = Worksheets("Sheet1").Cells(i, 2).Value Then
No, the code is comparing E2 with B2, not A2 and of course E2 and B2 will never be equal.
 
Upvote 0
VBA Code:
Private Sub CommandButton1_Click()

    a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    aa = Worksheets("Sheet1").Cells(Rows.Count, 5).End(xlUp).Row

    For i = 1 To a
        For ii = 1 To aa
            If Worksheets("Sheet1").Cells(ii, 5).Value = Worksheets("Sheet1").Cells(i, 1).Value Then

                Worksheets("Sheet1").Range("a" & i & ":d" & i).Copy

                Worksheets("Sheet2").Activate


                b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row

                Worksheets("Sheet2").Cells(b + 1, 1).Select

                ActiveSheet.Paste

                Worksheets("Sheet1").Activate

            End If


        Next: Next

    Application.CutCopyMode = False

    ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select


End Sub
 
Upvote 0
VBA Code:
Private Sub CommandButton1_Click()

    a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    aa = Worksheets("Sheet1").Cells(Rows.Count, 5).End(xlUp).Row

    For i = 1 To a
        For ii = 1 To aa
            If Worksheets("Sheet1").Cells(ii, 5).Value = Worksheets("Sheet1").Cells(i, 1).Value Then

                Worksheets("Sheet1").Range("a" & i & ":d" & i).Copy

                Worksheets("Sheet2").Activate


                b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row

                Worksheets("Sheet2").Cells(b + 1, 1).Select

                ActiveSheet.Paste

                Worksheets("Sheet1").Activate

            End If


        Next: Next

    Application.CutCopyMode = False

    ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select


End Sub
@mohadin . I really appreciated your post, but with above code I'm getting below result on Sheet2

Worksheets.xlsm
ABCD
12022 Clients2022 Data2022 Data22022 Data3
2Michael123412342345
3Anna7834230
4Peter6543230
5Anthony1234230
6Ben12345634
7Sally98542356
8Michelle1265125664
9David127612343
10Sue87653478
Sheet2


but the order I 'm looking for in Sheet 2 is

Worksheets.xlsm
ABCD
12022 Clients2022 Data2022 Data22022 Data3
2Peter6543230
3Sally98542356
4Michelle1265125664
5Anna7834230
6Ben12345634
7Michael123412342345
8David127612343
9Anthony1234230
10Sue87653478
Sheet2


Sheet 2: Since Peter is client in 2021 and 2022, his 2022 detail (row A9:D9) gets copied first in sheet2. then Sally is client in both 2021 and 2022 so her data (A14:D14) gets copied next in Sheet2. we need to keep the order in Sheet 2 same as it appears in Column E of Sheet1 ( 2021 Clients).
I hope I'm making sense.

Please amend the code for me, really appreciated :)

Sheet1
Worksheets.xlsm
ABCDE
12022 Clients2022 Data2022 Data22022 Data32021 Clients
2Michael123412342345James
3Sarah43215467Sam
4Mary9876034Peter
5Rachel56781245Shaw
6Anna7834230Sally
7Monica23564545Michelle
8Charles123400Ivona
9Peter6543230Anna
10Anthony1234230Claire
11Ben12345634Ben
12Elizabeth6543078Michael
13Wong12762345612David
14Sally98542356Annaleise
15Jay4325876589Chris
16Michelle1265125664John
17David127612343Bob
18Jennifer1234876556Anthony
19Sue87653478Sue
20Ruba3254239Robin
21Henry9876780
22Chloe3245345676
23Candy23893565
Sheet1
 
Upvote 0
VBA Code:
Private Sub CommandButton1_Click()

    a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    aa = Worksheets("Sheet1").Cells(Rows.Count, 5).End(xlUp).Row
  For ii = 1 To aa
    For i = 1 To a
      
            If Worksheets("Sheet1").Cells(ii, 5).Value = Worksheets("Sheet1").Cells(i, 1).Value Then

                Worksheets("Sheet1").Range("a" & i & ":d" & i).Copy

                Worksheets("Sheet2").Activate


                b = Worksheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row

                Worksheets("Sheet2").Cells(b + 1, 1).Select

                ActiveSheet.Paste

                Worksheets("Sheet1").Activate

            End If


        Next: Next

    Application.CutCopyMode = False

    ThisWorkbook.Worksheets("Sheet1").Cells(1, 1).Select


End Sub
 
Upvote 0
@mohadin Thank you so much!! it worked

Is there a way to copy highlighted green rows (A:D) who have become clients in the year 2022 to get copied next in Sheet2? Do I need to create another command button for this or above code can be modified? could you assist please :)

Worksheets.xlsm
ABCD
12022 Clients2022 Data2022 Data22022 Data3
2Peter6543230
3Sally98542356
4Michelle1265125664
5Anna7834230
6Ben12345634
7Michael123412342345
8David127612343
9Anthony1234230
10Sue87653478
11Sarah43215467
12Mary9876034
13Rachel56781245
14Monica23564545
15Charles123400
16Elizabeth6543078
17Wong12762345612
18Jay4325876589
19Ruba3254239
20Henry9876780
21Chloe3245345676
22Candy23893565
Sheet2
 
Upvote 0

Forum statistics

Threads
1,224,808
Messages
6,181,072
Members
453,020
Latest member
mattg2448

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