why VBA Run-time error'1004'. you cant paste this here because the copy area and paste area aren't the same size.

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. Sheet2 is where new clients' data need to be copied.

With the below code, I'm trying to paste new 2022 clients' data (A:D) to non-filled cells of Sheet2 in columns A:D. the condition is if A2<>E2:E20 then paste A2:D2 into Sheet2 non-filled row.

but I'm getting run-time error 104: "you can't paste this here because the copy area and paste area aren't the same sizes. Select just one cell in the paste area or an area that's the same size, and try pasting again." and the VBA line "ActiveSheet.Paste" is gets yellow highlighted when I click the command button.
I don't understand why.
Sheet2 shows the desired outcome where green highlighted rows are new clients.

VBA Code:
Private Sub CommandButton1_Click()

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

  For j = 1 To c
    For jj = 1 To cc
      
            If Worksheets("Sheet1").Cells(j, 1).Value <> Worksheets("Sheet1").Cells(jj, 5).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



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

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
19Jennifer1234876556
20Ruba3254239
21Henry9876780
22Chloe3245345676
23Candy23893565
Sheet2
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Classic example of why, in my opinion, you should always have Option Explicit at the top of every module. You can make that automatically happen by, in the vba window
Tools - Options - Editor tab - Require Variable Declaration.

1662259152027.png


If you had that set you would not have got as far as this problem. :)

The problem is that this line Worksheets("Sheet1").Range("a" & i & ":d" & i).Copy is using the variable "i" which is neither declared nor has a value set for it. vba is treating it as Empty and therefore the code line above is effectively this Worksheets("Sheet1").Range("a:d").Copy
That is, it is copying the whole of columns A:D. Since this is copying 1,048,576 rows and you are trying to paste all those rows starting at row 11 (I think) in Sheet 2, you are trying to fit the paste into only 1,048,566 rows - not the same size.
 
Upvote 0
Classic example of why, in my opinion, you should always have Option Explicit at the top of every module. You can make that automatically happen by, in the vba window
Tools - Options - Editor tab - Require Variable Declaration.

View attachment 73181

If you had that set you would not have got as far as this problem. :)

The problem is that this line Worksheets("Sheet1").Range("a" & i & ":d" & i).Copy is using the variable "i" which is neither declared nor has a value set for it. vba is treating it as Empty and therefore the code line above is effectively this Worksheets("Sheet1").Range("a:d").Copy
That is, it is copying the whole of columns A:D. Since this is copying 1,048,576 rows and you are trying to paste all those rows starting at row 11 (I think) in Sheet 2, you are trying to fit the paste into only 1,048,566 rows - not the same size.
@Peter_SSs Thank you so much!!
i've made the changes you recommended. now I'm getting error "Run time error 1004: Application-defined or object-defined error"
your advice is highly appreciated!!
 
Upvote 0
@Peter_SSs Thank you so much!!
i've made the changes you recommended. now I'm getting error "Run time error 1004: Application-defined or object-defined error"
your advice is highly appreciated!!
@Peter_SSs i realized i had to change fix another "i". I've done that now. :rolleyes:
now when I click on command button, I get below on Sheet2

Worksheets - Copy.xlsm
ABCD
12022 Clients2022 Data2022 Data22022 Data3
2Peter6543230
3Sally98542356
4Michelle1265125664
5Anna7834230
6Ben12345634
7Michael123412342345
8David127612343
9Anthony1234230
10Sue87653478
112022 Clients2022 Data2022 Data22022 Data3
122022 Clients2022 Data2022 Data22022 Data3
132022 Clients2022 Data2022 Data22022 Data3
142022 Clients2022 Data2022 Data22022 Data3
152022 Clients2022 Data2022 Data22022 Data3
162022 Clients2022 Data2022 Data22022 Data3
172022 Clients2022 Data2022 Data22022 Data3
182022 Clients2022 Data2022 Data22022 Data3
192022 Clients2022 Data2022 Data22022 Data3
202022 Clients2022 Data2022 Data22022 Data3
212022 Clients2022 Data2022 Data22022 Data3
222022 Clients2022 Data2022 Data22022 Data3
232022 Clients2022 Data2022 Data22022 Data3
242022 Clients2022 Data2022 Data22022 Data3
252022 Clients2022 Data2022 Data22022 Data3
262022 Clients2022 Data2022 Data22022 Data3
272022 Clients2022 Data2022 Data22022 Data3
282022 Clients2022 Data2022 Data22022 Data3
292022 Clients2022 Data2022 Data22022 Data3
302022 Clients2022 Data2022 Data22022 Data3
31Michael123412342345
32Michael123412342345
33Michael123412342345
34Michael123412342345
35Michael123412342345
36Michael123412342345
37Michael123412342345
38Michael123412342345
39Michael123412342345
40Michael123412342345
41Michael123412342345
42Michael123412342345
43Michael123412342345
44Michael123412342345
45Michael123412342345
46Michael123412342345
47Michael123412342345
48Michael123412342345
49Michael123412342345
50Sarah43215467
51Sarah43215467
52Sarah43215467
53Sarah43215467
54Sarah43215467
55Sarah43215467
56Sarah43215467
57Sarah43215467
58Sarah43215467
59Sarah43215467
60Sarah43215467
61Sarah43215467
62Sarah43215467
63Sarah43215467
64Sarah43215467
65Sarah43215467
66Sarah43215467
67Sarah43215467
68Sarah43215467
69Sarah43215467
70Mary9876034
71Mary9876034
72Mary9876034
73Mary9876034
74Mary9876034
75Mary9876034
76Mary9876034
77Mary9876034
78Mary9876034
79Mary9876034
80Mary9876034
81Mary9876034
82Mary9876034
83Mary9876034
84Mary9876034
85Mary9876034
86Mary9876034
87Mary9876034
88Mary9876034
89Mary9876034
90Rachel56781245
91Rachel56781245
92Rachel56781245
93Rachel56781245
94Rachel56781245
95Rachel56781245
96Rachel56781245
97Rachel56781245
98Rachel56781245
99Rachel56781245
100Rachel56781245
101Rachel56781245
102Rachel56781245
103Rachel56781245
104Rachel56781245
105Rachel56781245
106Rachel56781245
107Rachel56781245
108Rachel56781245
109Rachel56781245
110Anna7834230
111Anna7834230
112Anna7834230
113Anna7834230
114Anna7834230
115Anna7834230
116Anna7834230
117Anna7834230
118Anna7834230
119Anna7834230
120Anna7834230
121Anna7834230
122Anna7834230
123Anna7834230
124Anna7834230
125Anna7834230
126Anna7834230
127Anna7834230
128Anna7834230
129Monica23564545
130Monica23564545
131Monica23564545
132Monica23564545
133Monica23564545
134Monica23564545
135Monica23564545
136Monica23564545
137Monica23564545
138Monica23564545
139Monica23564545
140Monica23564545
141Monica23564545
142Monica23564545
143Monica23564545
144Monica23564545
145Monica23564545
146Monica23564545
147Monica23564545
148Monica23564545
149Charles123400
150Charles123400
151Charles123400
152Charles123400
153Charles123400
154Charles123400
155Charles123400
156Charles123400
157Charles123400
158Charles123400
159Charles123400
160Charles123400
161Charles123400
162Charles123400
163Charles123400
164Charles123400
165Charles123400
166Charles123400
167Charles123400
168Charles123400
169Peter6543230
170Peter6543230
171Peter6543230
172Peter6543230
173Peter6543230
174Peter6543230
175Peter6543230
176Peter6543230
177Peter6543230
178Peter6543230
179Peter6543230
180Peter6543230
181Peter6543230
182Peter6543230
183Peter6543230
184Peter6543230
185Peter6543230
186Peter6543230
187Peter6543230
188Anthony1234230
189Anthony1234230
190Anthony1234230
191Anthony1234230
192Anthony1234230
193Anthony1234230
194Anthony1234230
195Anthony1234230
196Anthony1234230
197Anthony1234230
198Anthony1234230
199Anthony1234230
200Anthony1234230
201Anthony1234230
202Anthony1234230
203Anthony1234230
204Anthony1234230
205Anthony1234230
206Anthony1234230
207Ben12345634
208Ben12345634
209Ben12345634
210Ben12345634
211Ben12345634
212Ben12345634
213Ben12345634
214Ben12345634
215Ben12345634
216Ben12345634
217Ben12345634
218Ben12345634
219Ben12345634
220Ben12345634
221Ben12345634
222Ben12345634
223Ben12345634
224Ben12345634
225Ben12345634
226Elizabeth6543078
227Elizabeth6543078
228Elizabeth6543078
229Elizabeth6543078
230Elizabeth6543078
231Elizabeth6543078
232Elizabeth6543078
233Elizabeth6543078
234Elizabeth6543078
235Elizabeth6543078
236Elizabeth6543078
237Elizabeth6543078
238Elizabeth6543078
239Elizabeth6543078
240Elizabeth6543078
241Elizabeth6543078
242Elizabeth6543078
243Elizabeth6543078
244Elizabeth6543078
245Elizabeth6543078
246Wong12762345612
247Wong12762345612
248Wong12762345612
249Wong12762345612
250Wong12762345612
251Wong12762345612
252Wong12762345612
253Wong12762345612
254Wong12762345612
255Wong12762345612
256Wong12762345612
257Wong12762345612
258Wong12762345612
259Wong12762345612
260Wong12762345612
261Wong12762345612
262Wong12762345612
263Wong12762345612
264Wong12762345612
265Wong12762345612
266Sally98542356
267Sally98542356
268Sally98542356
269Sally98542356
270Sally98542356
271Sally98542356
272Sally98542356
273Sally98542356
274Sally98542356
275Sally98542356
276Sally98542356
277Sally98542356
278Sally98542356
279Sally98542356
280Sally98542356
281Sally98542356
282Sally98542356
283Sally98542356
284Sally98542356
285Jay4325876589
286Jay4325876589
287Jay4325876589
288Jay4325876589
289Jay4325876589
290Jay4325876589
291Jay4325876589
292Jay4325876589
293Jay4325876589
294Jay4325876589
295Jay4325876589
296Jay4325876589
297Jay4325876589
298Jay4325876589
299Jay4325876589
300Jay4325876589
301Jay4325876589
302Jay4325876589
303Jay4325876589
304Jay4325876589
305Michelle1265125664
306Michelle1265125664
307Michelle1265125664
308Michelle1265125664
309Michelle1265125664
310Michelle1265125664
311Michelle1265125664
312Michelle1265125664
313Michelle1265125664
314Michelle1265125664
315Michelle1265125664
316Michelle1265125664
317Michelle1265125664
318Michelle1265125664
319Michelle1265125664
320Michelle1265125664
321Michelle1265125664
322Michelle1265125664
323Michelle1265125664
324David127612343
325David127612343
326David127612343
327David127612343
328David127612343
329David127612343
330David127612343
331David127612343
332David127612343
333David127612343
334David127612343
335David127612343
336David127612343
337David127612343
338David127612343
339David127612343
340David127612343
341David127612343
342David127612343
343Jennifer1234876556
344Jennifer1234876556
345Jennifer1234876556
346Jennifer1234876556
347Jennifer1234876556
348Jennifer1234876556
349Jennifer1234876556
350Jennifer1234876556
351Jennifer1234876556
352Jennifer1234876556
353Jennifer1234876556
354Jennifer1234876556
355Jennifer1234876556
356Jennifer1234876556
357Jennifer1234876556
358Jennifer1234876556
359Jennifer1234876556
360Jennifer1234876556
361Jennifer1234876556
362Jennifer1234876556
363Sue87653478
364Sue87653478
365Sue87653478
366Sue87653478
367Sue87653478
368Sue87653478
369Sue87653478
370Sue87653478
371Sue87653478
372Sue87653478
373Sue87653478
374Sue87653478
375Sue87653478
376Sue87653478
377Sue87653478
378Sue87653478
379Sue87653478
380Sue87653478
381Sue87653478
382Ruba3254239
383Ruba3254239
384Ruba3254239
385Ruba3254239
386Ruba3254239
387Ruba3254239
388Ruba3254239
389Ruba3254239
390Ruba3254239
391Ruba3254239
392Ruba3254239
393Ruba3254239
394Ruba3254239
395Ruba3254239
396Ruba3254239
397Ruba3254239
398Ruba3254239
399Ruba3254239
400Ruba3254239
401Ruba3254239
402Henry9876780
403Henry9876780
404Henry9876780
405Henry9876780
406Henry9876780
407Henry9876780
408Henry9876780
409Henry9876780
410Henry9876780
411Henry9876780
412Henry9876780
413Henry9876780
414Henry9876780
415Henry9876780
416Henry9876780
417Henry9876780
418Henry9876780
419Henry9876780
420Henry9876780
421Henry9876780
422Chloe3245345676
423Chloe3245345676
424Chloe3245345676
425Chloe3245345676
426Chloe3245345676
427Chloe3245345676
428Chloe3245345676
429Chloe3245345676
430Chloe3245345676
431Chloe3245345676
432Chloe3245345676
433Chloe3245345676
434Chloe3245345676
435Chloe3245345676
436Chloe3245345676
437Chloe3245345676
438Chloe3245345676
439Chloe3245345676
440Chloe3245345676
441Chloe3245345676
442Candy23893565
443Candy23893565
444Candy23893565
445Candy23893565
446Candy23893565
447Candy23893565
448Candy23893565
449Candy23893565
450Candy23893565
451Candy23893565
452Candy23893565
453Candy23893565
454Candy23893565
455Candy23893565
456Candy23893565
457Candy23893565
458Candy23893565
459Candy23893565
460Candy23893565
461Candy23893565
Sheet2

code is s

VBA Code:
Private Sub CommandButton2_Click()
    c = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    cc = Worksheets("Sheet1").Cells(Rows.Count, 5).End(xlUp).Row

  For j = 1 To c
    For jj = 1 To cc
      
            If Worksheets("Sheet1").Cells(j, 1).Value <> Worksheets("Sheet1").Cells(jj, 5).Value Then

                Worksheets("Sheet1").Range("a" & j & ":d" & j).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
@Peter_SSs i realized i had to change fix another "i". I've done that now. :rolleyes:
now when I click on command button, I get below on Sheet2

Worksheets - Copy.xlsm
ABCD
12022 Clients2022 Data2022 Data22022 Data3
2Peter6543230
3Sally98542356
4Michelle1265125664
5Anna7834230
6Ben12345634
7Michael123412342345
8David127612343
9Anthony1234230
10Sue87653478
112022 Clients2022 Data2022 Data22022 Data3
122022 Clients2022 Data2022 Data22022 Data3
132022 Clients2022 Data2022 Data22022 Data3
142022 Clients2022 Data2022 Data22022 Data3
152022 Clients2022 Data2022 Data22022 Data3
162022 Clients2022 Data2022 Data22022 Data3
172022 Clients2022 Data2022 Data22022 Data3
182022 Clients2022 Data2022 Data22022 Data3
192022 Clients2022 Data2022 Data22022 Data3
202022 Clients2022 Data2022 Data22022 Data3
212022 Clients2022 Data2022 Data22022 Data3
222022 Clients2022 Data2022 Data22022 Data3
232022 Clients2022 Data2022 Data22022 Data3
242022 Clients2022 Data2022 Data22022 Data3
252022 Clients2022 Data2022 Data22022 Data3
262022 Clients2022 Data2022 Data22022 Data3
272022 Clients2022 Data2022 Data22022 Data3
282022 Clients2022 Data2022 Data22022 Data3
292022 Clients2022 Data2022 Data22022 Data3
302022 Clients2022 Data2022 Data22022 Data3
31Michael123412342345
32Michael123412342345
33Michael123412342345
34Michael123412342345
35Michael123412342345
36Michael123412342345
37Michael123412342345
38Michael123412342345
39Michael123412342345
40Michael123412342345
41Michael123412342345
42Michael123412342345
43Michael123412342345
44Michael123412342345
45Michael123412342345
46Michael123412342345
47Michael123412342345
48Michael123412342345
49Michael123412342345
50Sarah43215467
51Sarah43215467
52Sarah43215467
53Sarah43215467
54Sarah43215467
55Sarah43215467
56Sarah43215467
57Sarah43215467
58Sarah43215467
59Sarah43215467
60Sarah43215467
61Sarah43215467
62Sarah43215467
63Sarah43215467
64Sarah43215467
65Sarah43215467
66Sarah43215467
67Sarah43215467
68Sarah43215467
69Sarah43215467
70Mary9876034
71Mary9876034
72Mary9876034
73Mary9876034
74Mary9876034
75Mary9876034
76Mary9876034
77Mary9876034
78Mary9876034
79Mary9876034
80Mary9876034
81Mary9876034
82Mary9876034
83Mary9876034
84Mary9876034
85Mary9876034
86Mary9876034
87Mary9876034
88Mary9876034
89Mary9876034
90Rachel56781245
91Rachel56781245
92Rachel56781245
93Rachel56781245
94Rachel56781245
95Rachel56781245
96Rachel56781245
97Rachel56781245
98Rachel56781245
99Rachel56781245
100Rachel56781245
101Rachel56781245
102Rachel56781245
103Rachel56781245
104Rachel56781245
105Rachel56781245
106Rachel56781245
107Rachel56781245
108Rachel56781245
109Rachel56781245
110Anna7834230
111Anna7834230
112Anna7834230
113Anna7834230
114Anna7834230
115Anna7834230
116Anna7834230
117Anna7834230
118Anna7834230
119Anna7834230
120Anna7834230
121Anna7834230
122Anna7834230
123Anna7834230
124Anna7834230
125Anna7834230
126Anna7834230
127Anna7834230
128Anna7834230
129Monica23564545
130Monica23564545
131Monica23564545
132Monica23564545
133Monica23564545
134Monica23564545
135Monica23564545
136Monica23564545
137Monica23564545
138Monica23564545
139Monica23564545
140Monica23564545
141Monica23564545
142Monica23564545
143Monica23564545
144Monica23564545
145Monica23564545
146Monica23564545
147Monica23564545
148Monica23564545
149Charles123400
150Charles123400
151Charles123400
152Charles123400
153Charles123400
154Charles123400
155Charles123400
156Charles123400
157Charles123400
158Charles123400
159Charles123400
160Charles123400
161Charles123400
162Charles123400
163Charles123400
164Charles123400
165Charles123400
166Charles123400
167Charles123400
168Charles123400
169Peter6543230
170Peter6543230
171Peter6543230
172Peter6543230
173Peter6543230
174Peter6543230
175Peter6543230
176Peter6543230
177Peter6543230
178Peter6543230
179Peter6543230
180Peter6543230
181Peter6543230
182Peter6543230
183Peter6543230
184Peter6543230
185Peter6543230
186Peter6543230
187Peter6543230
188Anthony1234230
189Anthony1234230
190Anthony1234230
191Anthony1234230
192Anthony1234230
193Anthony1234230
194Anthony1234230
195Anthony1234230
196Anthony1234230
197Anthony1234230
198Anthony1234230
199Anthony1234230
200Anthony1234230
201Anthony1234230
202Anthony1234230
203Anthony1234230
204Anthony1234230
205Anthony1234230
206Anthony1234230
207Ben12345634
208Ben12345634
209Ben12345634
210Ben12345634
211Ben12345634
212Ben12345634
213Ben12345634
214Ben12345634
215Ben12345634
216Ben12345634
217Ben12345634
218Ben12345634
219Ben12345634
220Ben12345634
221Ben12345634
222Ben12345634
223Ben12345634
224Ben12345634
225Ben12345634
226Elizabeth6543078
227Elizabeth6543078
228Elizabeth6543078
229Elizabeth6543078
230Elizabeth6543078
231Elizabeth6543078
232Elizabeth6543078
233Elizabeth6543078
234Elizabeth6543078
235Elizabeth6543078
236Elizabeth6543078
237Elizabeth6543078
238Elizabeth6543078
239Elizabeth6543078
240Elizabeth6543078
241Elizabeth6543078
242Elizabeth6543078
243Elizabeth6543078
244Elizabeth6543078
245Elizabeth6543078
246Wong12762345612
247Wong12762345612
248Wong12762345612
249Wong12762345612
250Wong12762345612
251Wong12762345612
252Wong12762345612
253Wong12762345612
254Wong12762345612
255Wong12762345612
256Wong12762345612
257Wong12762345612
258Wong12762345612
259Wong12762345612
260Wong12762345612
261Wong12762345612
262Wong12762345612
263Wong12762345612
264Wong12762345612
265Wong12762345612
266Sally98542356
267Sally98542356
268Sally98542356
269Sally98542356
270Sally98542356
271Sally98542356
272Sally98542356
273Sally98542356
274Sally98542356
275Sally98542356
276Sally98542356
277Sally98542356
278Sally98542356
279Sally98542356
280Sally98542356
281Sally98542356
282Sally98542356
283Sally98542356
284Sally98542356
285Jay4325876589
286Jay4325876589
287Jay4325876589
288Jay4325876589
289Jay4325876589
290Jay4325876589
291Jay4325876589
292Jay4325876589
293Jay4325876589
294Jay4325876589
295Jay4325876589
296Jay4325876589
297Jay4325876589
298Jay4325876589
299Jay4325876589
300Jay4325876589
301Jay4325876589
302Jay4325876589
303Jay4325876589
304Jay4325876589
305Michelle1265125664
306Michelle1265125664
307Michelle1265125664
308Michelle1265125664
309Michelle1265125664
310Michelle1265125664
311Michelle1265125664
312Michelle1265125664
313Michelle1265125664
314Michelle1265125664
315Michelle1265125664
316Michelle1265125664
317Michelle1265125664
318Michelle1265125664
319Michelle1265125664
320Michelle1265125664
321Michelle1265125664
322Michelle1265125664
323Michelle1265125664
324David127612343
325David127612343
326David127612343
327David127612343
328David127612343
329David127612343
330David127612343
331David127612343
332David127612343
333David127612343
334David127612343
335David127612343
336David127612343
337David127612343
338David127612343
339David127612343
340David127612343
341David127612343
342David127612343
343Jennifer1234876556
344Jennifer1234876556
345Jennifer1234876556
346Jennifer1234876556
347Jennifer1234876556
348Jennifer1234876556
349Jennifer1234876556
350Jennifer1234876556
351Jennifer1234876556
352Jennifer1234876556
353Jennifer1234876556
354Jennifer1234876556
355Jennifer1234876556
356Jennifer1234876556
357Jennifer1234876556
358Jennifer1234876556
359Jennifer1234876556
360Jennifer1234876556
361Jennifer1234876556
362Jennifer1234876556
363Sue87653478
364Sue87653478
365Sue87653478
366Sue87653478
367Sue87653478
368Sue87653478
369Sue87653478
370Sue87653478
371Sue87653478
372Sue87653478
373Sue87653478
374Sue87653478
375Sue87653478
376Sue87653478
377Sue87653478
378Sue87653478
379Sue87653478
380Sue87653478
381Sue87653478
382Ruba3254239
383Ruba3254239
384Ruba3254239
385Ruba3254239
386Ruba3254239
387Ruba3254239
388Ruba3254239
389Ruba3254239
390Ruba3254239
391Ruba3254239
392Ruba3254239
393Ruba3254239
394Ruba3254239
395Ruba3254239
396Ruba3254239
397Ruba3254239
398Ruba3254239
399Ruba3254239
400Ruba3254239
401Ruba3254239
402Henry9876780
403Henry9876780
404Henry9876780
405Henry9876780
406Henry9876780
407Henry9876780
408Henry9876780
409Henry9876780
410Henry9876780
411Henry9876780
412Henry9876780
413Henry9876780
414Henry9876780
415Henry9876780
416Henry9876780
417Henry9876780
418Henry9876780
419Henry9876780
420Henry9876780
421Henry9876780
422Chloe3245345676
423Chloe3245345676
424Chloe3245345676
425Chloe3245345676
426Chloe3245345676
427Chloe3245345676
428Chloe3245345676
429Chloe3245345676
430Chloe3245345676
431Chloe3245345676
432Chloe3245345676
433Chloe3245345676
434Chloe3245345676
435Chloe3245345676
436Chloe3245345676
437Chloe3245345676
438Chloe3245345676
439Chloe3245345676
440Chloe3245345676
441Chloe3245345676
442Candy23893565
443Candy23893565
444Candy23893565
445Candy23893565
446Candy23893565
447Candy23893565
448Candy23893565
449Candy23893565
450Candy23893565
451Candy23893565
452Candy23893565
453Candy23893565
454Candy23893565
455Candy23893565
456Candy23893565
457Candy23893565
458Candy23893565
459Candy23893565
460Candy23893565
461Candy23893565
Sheet2

code is s

VBA Code:
Private Sub CommandButton2_Click()
    c = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    cc = Worksheets("Sheet1").Cells(Rows.Count, 5).End(xlUp).Row

  For j = 1 To c
    For jj = 1 To cc
     
            If Worksheets("Sheet1").Cells(j, 1).Value <> Worksheets("Sheet1").Cells(jj, 5).Value Then

                Worksheets("Sheet1").Range("a" & j & ":d" & j).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
I've 2 command buttons. first one: copies existing clients data to Sheet2 and second button is supposed to copy new client's data to Sheet2.
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

Private Sub CommandButton2_Click()
    c = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    cc = Worksheets("Sheet1").Cells(Rows.Count, 5).End(xlUp).Row

  For j = 1 To c
    For jj = 1 To cc
      
            If Worksheets("Sheet1").Cells(j, 1).Value <> Worksheets("Sheet1").Cells(jj, 5).Value Then

                Worksheets("Sheet1").Range("a" & j & ":d" & j).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
desired outcome is below
Worksheets - Copy.xlsm
ABCD
12022 Clients2022 Data2022 Data22022 Data3
2Peter6543230
3Sally98542356
4Michelle1265125664
5Anna7834230
6Ben12345634
7Michael123412342345
8David127612343
9Anthony1234230
10Sue87653478
11Sarah43215467
12Mary9876034
13Rachel56781245
14Monica23564545
15Charles123400
16Elizabeth6543078
17Wong12762345612
18Jay4325876589
19Jennifer1234876556
20Ruba3254239
21Henry9876780
22Chloe3245345676
23Candy23893565
Sheet2


button 1 does the job but when I press button 2messes up everythin. it copies same row multiple times..
 
Upvote 0
I've 2 command buttons. first one: copies existing clients data to Sheet2 and second button is supposed to copy new client's data to Sheet2.
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

Private Sub CommandButton2_Click()
    c = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    cc = Worksheets("Sheet1").Cells(Rows.Count, 5).End(xlUp).Row

  For j = 1 To c
    For jj = 1 To cc
     
            If Worksheets("Sheet1").Cells(j, 1).Value <> Worksheets("Sheet1").Cells(jj, 5).Value Then

                Worksheets("Sheet1").Range("a" & j & ":d" & j).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
desired outcome is below
Worksheets - Copy.xlsm
ABCD
12022 Clients2022 Data2022 Data22022 Data3
2Peter6543230
3Sally98542356
4Michelle1265125664
5Anna7834230
6Ben12345634
7Michael123412342345
8David127612343
9Anthony1234230
10Sue87653478
11Sarah43215467
12Mary9876034
13Rachel56781245
14Monica23564545
15Charles123400
16Elizabeth6543078
17Wong12762345612
18Jay4325876589
19Jennifer1234876556
20Ruba3254239
21Henry9876780
22Chloe3245345676
23Candy23893565
Sheet2


button 1 does the job but when I press button 2messes up everythin. it copies same row multiple times..
@Peter_SSs @mohadin could you please assist with the coding of command button2. much appreciated!!
 
Upvote 0
I've had a look over your code and done a re-write using a more logical method. It looks a bit longer because I've tried to use variable names that describe what they are doing and added a few comments so that you can see what is happening.

This code will work with both buttons, you don't need separate procedures when the task is so similar for both. If your buttons are not named "Button 1" and "Button 2" then you will need to change those names in the code so that they match correctly.

After pasting the code into your module, to assign the code to both buttons, right click on the first button, then choose 'Assign Macro' from the menu and choose "CopyClientList" from the list of available procedures. Repeat these steps for the second button.

Having said that, if you always run both together then I can easily edit the code so that it completes both lists from one button (if desired).

Note that there appears to be an anomaly when running the code on the sample provided. I am looking into this and will post a correction later when I find the cause. For some reason, "Peter" is not being found on the 2021 client list and is being added with the new clients instead of the existing.

VBA Code:
Option Explicit
Sub CopyClientList()

' Declare variables

Dim ws1 As Worksheet, ws2 As Worksheet
Dim ClientFound As Range, ClientList As Range, NextClient As Range, OldClients As Range
Dim EndOfList As Long, PasteRow As Long, EndOfOld As Long
Dim WhichButton As String

' Set worksheet objects

Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")

' find last row for each range to be used

EndOfList = ws1.Cells(Rows.Count, 1).End(xlUp).Row
PasteRow = ws2.Cells(Rows.Count, 1).End(xlUp).Row + 1
EndOfOld = ws1.Cells(Rows.Count, 5).End(xlUp).Row

' set tables that need to be searched as range objects

Set ClientList = ws1.Range("A2:A" & EndOfList)
Set OldClients = ws1.Range("E5:E" & EndOfOld)

' check which button was clicked

WhichButton = Application.Caller

' loop through the names in the 2022 list

For Each NextClient In ClientList

    ' check if client is found in the 2021 list
    
    Set ClientFound = OldClients.Find(NextClient, , xlValues, xlWhole, xlByRows, , False, False, False)
    
    ' check if the client should be copied to sheet 2 or skipped based on which button was clicked
    ' if it is copied then increae the PasteRow ready for the next cycle, otherwise nothing happens
    ' note that the 2 negative commands in the line "Not ClientFound Is Nothing" cancel each other out, this is used to confirm that the record exists
    
    ' **** Change the names of "Button 1" and "Button 2" below to match the names of your actual buttons if different ****
    ' **** Note that the names are case sensitive, "Button" and "button" are not the same ****
    
    If WhichButton = "Button 1" Then
        If Not ClientFound Is Nothing Then
            NextClient.Resize(, 4).Copy ws2.Cells(PasteRow, 1)
            PasteRow = PasteRow + 1
        End If
    ElseIf WhichButton = "Button 2" Then
        If ClientFound Is Nothing Then
            NextClient.Resize(, 4).Copy ws2.Cells(PasteRow, 1)
            PasteRow = PasteRow + 1
        End If
    End If
Next NextClient

End Sub
 
Upvote 0
I've had a look over your code and done a re-write using a more logical method. It looks a bit longer because I've tried to use variable names that describe what they are doing and added a few comments so that you can see what is happening.

This code will work with both buttons, you don't need separate procedures when the task is so similar for both. If your buttons are not named "Button 1" and "Button 2" then you will need to change those names in the code so that they match correctly.

After pasting the code into your module, to assign the code to both buttons, right click on the first button, then choose 'Assign Macro' from the menu and choose "CopyClientList" from the list of available procedures. Repeat these steps for the second button.

Having said that, if you always run both together then I can easily edit the code so that it completes both lists from one button (if desired).

Note that there appears to be an anomaly when running the code on the sample provided. I am looking into this and will post a correction later when I find the cause. For some reason, "Peter" is not being found on the 2021 client list and is being added with the new clients instead of the existing.

VBA Code:
Option Explicit
Sub CopyClientList()

' Declare variables

Dim ws1 As Worksheet, ws2 As Worksheet
Dim ClientFound As Range, ClientList As Range, NextClient As Range, OldClients As Range
Dim EndOfList As Long, PasteRow As Long, EndOfOld As Long
Dim WhichButton As String

' Set worksheet objects

Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")

' find last row for each range to be used

EndOfList = ws1.Cells(Rows.Count, 1).End(xlUp).Row
PasteRow = ws2.Cells(Rows.Count, 1).End(xlUp).Row + 1
EndOfOld = ws1.Cells(Rows.Count, 5).End(xlUp).Row

' set tables that need to be searched as range objects

Set ClientList = ws1.Range("A2:A" & EndOfList)
Set OldClients = ws1.Range("E5:E" & EndOfOld)

' check which button was clicked

WhichButton = Application.Caller

' loop through the names in the 2022 list

For Each NextClient In ClientList

    ' check if client is found in the 2021 list
   
    Set ClientFound = OldClients.Find(NextClient, , xlValues, xlWhole, xlByRows, , False, False, False)
   
    ' check if the client should be copied to sheet 2 or skipped based on which button was clicked
    ' if it is copied then increae the PasteRow ready for the next cycle, otherwise nothing happens
    ' note that the 2 negative commands in the line "Not ClientFound Is Nothing" cancel each other out, this is used to confirm that the record exists
   
    ' **** Change the names of "Button 1" and "Button 2" below to match the names of your actual buttons if different ****
    ' **** Note that the names are case sensitive, "Button" and "button" are not the same ****
   
    If WhichButton = "Button 1" Then
        If Not ClientFound Is Nothing Then
            NextClient.Resize(, 4).Copy ws2.Cells(PasteRow, 1)
            PasteRow = PasteRow + 1
        End If
    ElseIf WhichButton = "Button 2" Then
        If ClientFound Is Nothing Then
            NextClient.Resize(, 4).Copy ws2.Cells(PasteRow, 1)
            PasteRow = PasteRow + 1
        End If
    End If
Next NextClient

End Sub
Thank you so much! yes one button would be more convenient. By pressing this button, first old clients(2021 and 2022) should get copied in Sheet2 then new clients(2022) gets copied . this order is very important.

Book1
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

Book1
ABCD
12022 Clients2022 Data2022 Data22022 Data3
2Peter6543230
3Sally98542356
4Michelle1265125664
5Anna7834230
6Ben12345634
7Michael123412342345
8David127612343
9Anthony1234230
10Sue87653478
11Sarah43215467
12Mary9876034
13Rachel56781245
14Monica23564545
15Charles123400
16Elizabeth6543078
17Wong12762345612
18Jay4325876589
19Jennifer1234876556
20Ruba3254239
21Henry9876780
22Chloe3245345676
23Candy23893565
Sheet2
 
Upvote 0
Give this a quick try and see if it works correctly for you, I'm still getting the same error with Peter being copied to the wrong list but can see no reason why it is happening. There is no obvious reason for it to be happening so I'm wondering if it's a problem on my laptop as it has been crashing a lot recently.
VBA Code:
Option Explicit
Sub CopyClientList()

' Declare variables

Dim ws1 As Worksheet, ws2 As Worksheet
Dim ClientFound As Range, ClientList As Range, NextClient As Range, OldClients As Range
Dim EndOfList As Long, EndOfOld As Long
Dim Clients2022 As Range, Clients2021 As Range

' Set worksheet objects

Set ws1 = Worksheets("Sheet1")
Set ws2 = Worksheets("Sheet2")

' find last row for each range to be used

EndOfList = ws1.Cells(Rows.Count, 1).End(xlUp).Row
EndOfOld = ws1.Cells(Rows.Count, 5).End(xlUp).Row

' set tables that need to be searched as range objects

Set ClientList = ws1.Range("A2:A" & EndOfList)
Set OldClients = ws1.Range("E5:E" & EndOfOld)

' loop through the names in the 2022 list

For Each NextClient In ClientList

    ' check if client is found in the 2021 list
    
    Set ClientFound = OldClients.Find(NextClient, , xlValues, xlWhole, xlByRows, , False, False, False)
    
        ' assign client to temporary 2021 or 2022 lists based on result of search above
        
        If Not ClientFound Is Nothing Then
            If Clients2021 Is Nothing Then Set Clients2021 = NextClient.Resize(, 4) Else Set Clients2021 = Union(Clients2021, NextClient.Resize(, 4))
        Else
            If Clients2022 Is Nothing Then Set Clients2022 = NextClient.Resize(, 4) Else Set Clients2022 = Union(Clients2022, NextClient.Resize(, 4))
        End If
Next NextClient
    
    ' copy the client lists to sheet 2 by year
    
Clients2021.Copy ws2.Cells(Rows.Count, 1).End(xlUp).Offset(1)
Clients2022.Copy ws2.Cells(Rows.Count, 1).End(xlUp).Offset(1)

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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