How to copy and paste the next row VBA Help.

punnipah

Board Regular
Joined
Nov 3, 2021
Messages
134
Office Version
  1. 2019
Platform
  1. Windows
Hi,All

my code :VBA not working if i want to copy more than 1 Row the word "Com." on column "A" is the row i want to copy
How can i do it please help.

Sub copyrows()
Dim r As Range, f As Range, cell As String
Dim wb As Workbook
Dim sh As Worksheet

Set sh = ThisWorkbook.Sheets("Sheet1")
Set wb = Workbooks.Add
Set r = sh.Range("A:A")
Set f = r.Find("Com.", , xlValues, xlWhole, , , True)
If Not f Is Nothing Then
cell = f.Address
Do
sh.Rows(f.Row + 1).Copy wb.Sheets(1).Range("A" & Rows.Count).End(3)(2)
Set f = r.FindNext(f)
Loop While Not f Is Nothing And f.Address <> cell
End If

wb.SaveAs ThisWorkbook.Path & "\NewBook.xlsx"
End Sub



Test.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
1
2Detail Debt Information
3DMSMA004Date: 08/03/2023
4Time: 15:21:58
5
6
7Cust Acc Num :
8SA Num :
9Billing Acc Num : xxxxx
10Mobile Num :
11Invoice Num :
12Cust / Tax ID : 3119900369657
13Company Code :
14
15Com.Company TypeTax IDCateSub CateCollec. SegmentCACA NameCIS TypeSASA StatusBABA NameAccount NameBA StatusMobile NumMobile StatusMobile Status DateCount MobileStart DateEnd DateInvoice NumMonthDue dateDebt AgeInvoice AmountCurrent Before VATVATGood & ServiceTotal Amount
16AWNFBB xxxxxRTHA xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxSuspend - Credit Limit27/02/2023124/08/202223/09/2022 xxxxx650924/10/20221351175.93109976.9301175.93
17AWNFBB xxxxxRTHA xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxSuspend - Credit Limit27/02/2023124/09/202223/10/2022 xxxxx651024/11/20221041175.93109976.9301175.93
18AWNFBB xxxxxRTHA xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxSuspend - Credit Limit27/02/2023124/10/202223/11/2022 xxxxx651124/12/2022741175.93109976.9301175.93
19AWNFBB xxxxxRTHA xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxSuspend - Credit Limit27/02/2023124/11/202223/12/2022 xxxxx651224/01/2023431175.93109976.9301175.93
20AWNFBB xxxxxRTHA xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxSuspend - Credit Limit27/02/2023124/12/202223/01/2023 xxxxx660124/02/2023121175.93109976.9301175.93
21AWNFBB xxxxxRTHA xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxSuspend - Credit Limit27/02/2023124/01/202323/02/2023 xxxxx660224/03/2023-161175.93109976.9301175.93
22รวม67055.586594461.5807055.58
23รวม AWN67055.586594461.5807055.58
24รวม All Company 167055.586594461.5807055.58
25Detail Debt Information
26DMSMA004Date: 08/03/2023
27Time: 15:22:41
28
29
30Cust Acc Num :
31SA Num :
32Billing Acc Num : 32000034797020
33Mobile Num :
34Invoice Num :
35Cust / Tax ID : 3100502755940
36Company Code :
37
38Com.Company TypeTax IDCateSub CateCollec. SegmentCACA NameCIS TypeSASA StatusBABA NameAccount NameBA StatusMobile NumMobile StatusMobile Status DateCount MobileStart DateEnd DateInvoice NumMonthDue dateDebt AgeInvoice AmountCurrent Before VATVATGood & ServiceTotal Amount
39 xxxxx xxxxx xxxxxRTHA xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxActive11/12/2022116/09/202215/10/2022 xxxxx651007/11/2022121560.8300288.2288.2
40 xxxxx xxxxx xxxxxRTHA xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxActive11/12/2022116/10/202215/11/2022 xxxxx651108/12/202290511.9319913.93299511.93
41 xxxxx xxxxx xxxxxRTHA xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxActive11/12/2022116/11/202215/12/2022 xxxxx651207/01/202360516.2120314.21299516.21
42 xxxxx xxxxx xxxxxRTHA xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxActive11/12/2022116/12/202215/01/2023 xxxxx660107/02/202329511.9319913.93299511.93
43 xxxxx xxxxx xxxxxRTHA xxxxx xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxActive11/12/2022116/01/202315/02/2023 xxxxx660210/03/2023-2212.9319913.930212.93
44รวม52313.83800561185.22041.2
45รวม AWN52313.83800561185.22041.2
46รวม All Company 152313.83800561185.22041.2
47Detail Debt Information
48DMSMA004Date: 08/03/2023
49Time: 15:23:10
50
51
52Cust Acc Num :
53SA Num :
54Billing Acc Num : xxxxx
55Mobile Num :
56Invoice Num :
57Cust / Tax ID : 3101801263730
58Company Code :
59
60Com.Company TypeTax IDCateSub CateCollec. SegmentCACA NameCIS TypeSASA StatusBABA NameAccount NameBA StatusMobile NumMobile StatusMobile Status DateCount MobileStart DateEnd DateInvoice NumMonthDue dateDebt AgeInvoice AmountCurrent Before VATVATGood & ServiceTotal Amount
61 xxxxx xxxxx xxxxxRTHAHV15 xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxActive19/05/2022120/10/202219/11/2022 xxxxx651112/12/202286648279.4419.56349648
62 xxxxx xxxxx xxxxxRTHAHV15 xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxActive19/05/2022120/11/202219/12/2022 xxxxx651211/01/202356648279.4419.56349648
63 xxxxx xxxxx xxxxxRTHAHV15 xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxActive19/05/2022120/12/202219/01/2023 xxxxx660111/02/202325648279.4419.56349648
64 xxxxx xxxxx xxxxxRTHAHV15 xxxxx xxxxx xxxxx xxxxx xxxxxActive xxxxxActive19/05/2022120/01/202319/02/2023 xxxxx660214/03/2023-6648279.4419.56349648
65รวม425921117.7678.2413962592
66รวม AWN425921117.7678.2413962592
67รวม All Company 1425921117.7678.2413962592
68
69
70
Sheet1
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi,
Think problem is only located in your Copy instruction ...
You could test
VBA Code:
sh.Range(Sheet1.Cells(f.Row + 1, 1), Sheet1.Cells(f.Row + (f.End(xlDown).Row - f.Row), 30)).Copy wb.Sheets(1).Range("A" & Rows.Count).End(3)(2)
 
Upvote 1
Solution
This doesn't impact you now because you are only doing find and not a replace but or future reference your end of loop condition Loop While Not f Is Nothing And f.Address <> cell doesn't work.
If in this line "Not f is Nothing" is False (ie f is not found) then "f.Address" would not exist and the code would error out.

If you are doing a "replace' then use Not f Is Nothing, since you are not doing a replace use f.Address <> cell
 
Upvote 0
Hi,
Think problem is only located in your Copy instruction ...
You could test
VBA Code:
sh.Range(Sheet1.Cells(f.Row + 1, 1), Sheet1.Cells(f.Row + (f.End(xlDown).Row - f.Row), 30)).Copy wb.Sheets(1).Range("A" & Rows.Count).End(3)(2)
Thank you verymuch
 
Upvote 0
This doesn't impact you now because you are only doing find and not a replace but or future reference your end of loop condition Loop While Not f Is Nothing And f.Address <> cell doesn't work.
If in this line "Not f is Nothing" is False (ie f is not found) then "f.Address" would not exist and the code would error out.

If you are doing a "replace' then use Not f Is Nothing, since you are not doing a replace use f.Address <> cell
Thank you verymuch
You are welcome

@

James006


i so sorry i trype to Run Macro Again it not working


1678686949760.png





1678687014545.png
 
Upvote 0
Hi,
You have to make sure in your set-up that there is no confusion between :
- sh or Set sh = ThisWorkbook.Sheets("Sheet1")
- Sheet1
- Sheets(1)
 
Upvote 0
Hi,
You have to make sure in your set-up that there is no confusion between :
- sh or Set sh = ThisWorkbook.Sheets("Sheet1")
- Sheet1
- Sheets(1)



Set sh = ActiveWorkbook.Sheets("Sheet1")
Set wb = Workbooks.Add
Set r = sh.Range("A:A")
Set f = r.Find("Com.", , xlValues, xlWhole, , , True)
If Not f Is Nothing Then
cell = f.Address
Do
sh.Range(Sheet1.Cells(f.Row + 1, 1), Sheet1.Cells(f.Row + (f.End(xlDown).Row - f.Row), 30)).Copy wb.Sheets(1).Range("A" & Rows.Count).End(3)(2)
Set f = r.FindNext(f)
Loop While Not f Is Nothing And f.Address <> cell
End If

wb.SaveAs ThisWorkbook.path & "\Datafile.xlsx"
 
Upvote 0
Sorry but ... just tested the Copy instruction, at my end, and it is working as expected ... :)
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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