VBA to copy contents of newly added multiple worksheets to other specific worksheets in the same workbooks

Hi guys,

I have a macro code that opens up excel files and adds them up to an existing workbook (that is already open) with already 19 worksheets (worksheet names as follows: “result”, “1”, “2”, “3”,.. until “18”).

Now after adding the new worksheets (18 total as well with varying sheet names, and is always changing), I will filter the column AG (criteria =1) fo each newly added worksheet and copy them accordingly to existing worksheets “1” until “18”. However, this is how I want it to go: The filtered contents of 1st newly added worksheet gets copied to sheet “1”, the 2nd newly added worksheets filtered contents get copied to sheet “2”, and so on until the 18th newly added worksheet filtered contents gets copied onto sheet “18”.

Appreciate if you could help me write a code based on this.thanks a lot!

Top Contributor Asked on July 19, 2015 in VBA: Macros.
Add Comment
2 Answer(s)
Best answer

Assuming your order of worksheets start from left:  “result”, “1”, “2”, “3”,.. until “18”, New 1, New 2,…, New 18. This macro do not consider the worksheets and it depends on the order.
i.e;  Worksheet #2 is “1” and Worksheet#19 is “18”, Worksheet#20 is New 1 , Worksheet#21 is New 2, so on.

So, Worksheet#20 data will be updated into Worksheet#2.
Here is the macro to filter the new worksheet and copy & paste the filtered data into the respective target Sheet:

Sub sbUpdatedSheets()
Dim i, NumSheets As Integer
Dim lastRow As Long
NumSheets = 18
For i = 1 To NumSheets
'Filter the new source sheet and copy the content
Sheets(NumSheets + 1 + i).Activate
'Find the last free row in destination sheet and paste
Sheets(i + 1).Activate
lastRow = ActiveSheet.Cells.SpecialCells(xlLastCell).Row
Do While Application.CountA(ActiveSheet.Rows(lastRow)) = 0 And lastRow <> 1
lastRow = lastRow - 1
Range("A" & lastRow + 1).Select
End Sub
Sub sbFilter()
FieldToFilter = 33 ' = Column AG
ActiveSheet.Cells.AutoFilter Field:=FieldToFilter, Criteria1:="1"
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
End Sub


Keymaster Answered on July 19, 2015.

Thanks, PNRao! The code works perfectly. I just had to move sheets according to your code, since originally when the new sheets are added, they are placed into worksheets#2-#20. I don’t know how to automatically placed the new worksheets into the worksheet#20 and so on..

on July 19, 2015.

You are most welcome! You can use sheets.count and add the new sheets after #Sheets. So that your new sheets will be always adding at end.

You can share the existing code to import, I will help in modifying it.


on July 19, 2015.

Hi Mr. PNRao, thanks for your help. WIth your comment, i managed to figure out how to do it. I basically changed the index number reference for the sheet from Sheets(1) to Sheet(19). Here is the final working code.

Sub LoadSheets()
Path = "C:AOI database"
Filename = Dir(Path & "*.CSV")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=False
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(19)
Next Sheet
Filename = Dir()
ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
End Sub
on July 20, 2015.
Add Comment

Thanks for your feedback.  I am glad to know that you found our codes helpful.


Keymaster Answered on July 20, 2015.
Add Comment
  • Found this useful?

    Please share using the share button above.

    If you found the answer is best answer for your question, Please mark as 'best answer' by clicking on the right tick mark icon at the left side of the answer.

    Found the answer useful and wants to credit the user, then vote the answer (vote up).

  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.