VBA – Copy rows to a new workbook based on a cell value

Testsubject

New Member
Joined
May 19, 2022
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hi

I'm new to VBA, still learning the ropes. Please can I get some assistance and would appreciate help with the below code.
  1. The macro runs but copies the same row of data to row 3 in the destination workbook (TEST1), instead of copying it to the next row based on the last row that contains data.
  2. If Cell A17 = 7710 on that sheet, it needs to go to sheet 1 in the destination workbook ; If cell A17 = 3810 goes to sheet 2.
  3. This needs to work for any number of worksheets present in the active workbook.
----------------------------------------------------------------------------------------------------------------

Sub copyrow()
Dim MyBook As Workbook, newBook As Workbook
Dim FileNm As String
Dim WS As Worksheet
Dim wsCopy As Worksheet

Set MyBook = ThisWorkbook

FileNm = ThisWorkbook.Path & "\" & "TEST1.xls"
Set newBook = Workbooks.Add

With newBook
MyBook.Sheets("(1)").Range("A15", "L15").Copy
newBook.Sheets("Sheet1").Rows("1").PasteSpecial xlPasteAll

For Each WS In Worksheets
With WS.Range("A16", "L17").Copy
newBook.Sheets("Sheet1").Rows("3").PasteSpecial xlPasteValues

End With
Next
'Save new wb with XLS extension
.SaveAs Filename:=FileNm, FileFormat:=xlNormal, CreateBackup:=False

.Close Savechanges:=False
End With

End Sub

----------------------------------------------------------------------------------------------------------------



1654794921201.png
1654794937735.png
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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