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,


I want to Copy Rows as highlighted in red, each time the rows will be different. And then Save a new workbook .

-copy and paste the next row
-Rows 16,34,52 only


Ex.Output
highlighted in Yellow






Test.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAI
1
2Detail Debt Information
3DMSMA004Date: 08/03/2023
4Time: 15:21:58
5
6
7Cust Acc Num :
8SA Num :
9Billing Acc Num :xxxx
10Mobile Num :
11Invoice Num :
12Cust / Tax ID :xxxx
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
16AWNFBBxxxxRxxxxGOOD15xxxxxxxxxxxxxxxxxxxxActivexxxxSuspend - Credit Limitxxxx1xxxx23/09/2022xxxx6509xxxx1351175.93109976.9301175.93
17รวม17055.586594461.5807055.58
18รวม AWN17055.586594461.5807055.58
19รวม All Company 117055.586594461.5807055.58
20Detail Debt Information
21DMSMA004Date: 08/03/2023
22Time: 15:22:41
23
24
25Cust Acc Num :
26SA Num :
27Billing Acc Num :xxxx
28Mobile Num :
29Invoice Num :
30Cust / Tax ID :xxxx
31Company Code :
32
33Com.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
34AWNAWNxxxxRTHAGOOD15xxxxxxxxxxxxxxxxxxxxActivexxxxActive11/12/20221xxxx15/10/2022xxxx6510xxxx121560.8300288.2288.2
35รวม12313.83800561185.22041.2
36รวม AWN12313.83800561185.22041.2
37รวม All Company 112313.83800561185.22041.2
38Detail Debt Information
39DMSMA004Date: 08/03/2023
40Time: 15:23:10
41
42
43Cust Acc Num :
44SA Num :
45Billing Acc Num :xxxx
46Mobile Num :
47Invoice Num :
48Cust / Tax ID :xxxx
49Company Code :
50
51Com.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
52AWNAWNxxxxRTHAHV15xxxxxxxxxxxxxxxxxxxxActivexxxxActivexxxx1xxxx19/11/2022xxxx6511xxxx86648279.4419.56349648
53รวม125921117.7678.2413962592
54รวม AWN125921117.7678.2413962592
55รวม All Company 1125921117.7678.2413962592
56
57
58
59
60Output
61Save New Workbook
62AWNFBBxxxxRxxxxGOOD15xxxxxxxxxxxxxxxxxxxxActivexxxxSuspend - Credit Limitxxxx1xxxx23/09/2022xxxx6509xxxx1351175.93109976.9301175.93
63AWNAWNxxxxRTHAGOOD15xxxxxxxxxxxxxxxxxxxxActivexxxxActivexxxx1xxxx15/10/2022xxxx6510xxxx121560.8300288.2288.2
64AWNAWNxxxxRTHAHV15xxxxxxxxxxxxxxxxxxxxActivexxxxActivexxxx1xxxx19/11/2022xxxx6511xxxx86648279.4419.56349648
65
Sheet1
 

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.
Hi @punnipah : Thanks for posting on the forum.

Consider the following to run the macro:
- Put the macro in the same workbook you have with the data.
- The name of the sheet and according to your example is called "Sheet1"
- The pattern I found is that after the word "Com." on column "A" is the row you want to copy.
- The new book will be saved in the same folder where you have your macro file named "NewBook.xlsx".

So, considering the above, please try the following macro:

VBA Code:
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

Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
 
Upvote 0
Hi @punnipah : Thanks for posting on the forum.

Consider the following to run the macro:
- Put the macro in the same workbook you have with the data.
- The name of the sheet and according to your example is called "Sheet1"
- The pattern I found is that after the word "Com." on column "A" is the row you want to copy.
- The new book will be saved in the same folder where you have your macro file named "NewBook.xlsx".

So, considering the above, please try the following macro:

VBA Code:
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

Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --
Hi @punnipah : Thanks for posting on the forum.

Consider the following to run the macro:
- Put the macro in the same workbook you have with the data.
- The name of the sheet and according to your example is called "Sheet1"
- The pattern I found is that after the word "Com." on column "A" is the row you want to copy.
- The new book will be saved in the same folder where you have your macro file named "NewBook.xlsx".

So, considering the above, please try the following macro:

VBA Code:
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

Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --

not working
@

1678345489663.png

1678345411251.png

 
Upvote 0
Hi @punnipah : Thanks for posting on the forum.

Consider the following to run the macro:
- Put the macro in the same workbook you have with the data.
- The name of the sheet and according to your example is called "Sheet1"
- The pattern I found is that after the word "Com." on column "A" is the row you want to copy.
- The new book will be saved in the same folder where you have your macro file named "NewBook.xlsx".

So, considering the above, please try the following macro:

VBA Code:
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

Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
----- --

Yes,i see Problem
- Put the macro in the same workbook you have with the data.

My macro not the same workbook what should i do?
 
Upvote 0
Yes,i see 2 Problem


1.My macro not the same workbook what should i do?
2. - The pattern I found is that after the word "Com." on column "A" is the row you want to copy.
some time the the word "Com." on column "A" more than 1 Columns what should i do ?

1678348604639.png
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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