VBA - Copy worksheets and rename automatically in another workbook

cheongmarcus

New Member
Joined
Mar 14, 2020
Messages
16
Office Version
  1. 365
  2. 2016
  3. 2013
  4. 2011
Platform
  1. Windows
Hi, I encounter a problem and not sure how to continue from here. What i would like to do is:
First, Copy sheet 1 from all the worksheets into this new workbook where i have my macro run. (this i have no issue, here is the code)
Next, as I copy these sheets from their respective workbooks, I would like to rename these sheet copied according to its respective workbook name? (Here I do not know how to accomplish this)

So in my folder, there is 5 excel workbooks (namely A,B,C,D,E), with 10 excel worksheets within each workbook.
I am taking the first sheet of each workbook.

However, as i perform the copy of these sheets, I want to rename it according to A,B,C,D,E. So for instance, in this new workbook, as the code copies sheet 1 from workbook A, that sheet gets renamed as A, then as the code copies sheet 1 from workbook B, that sheet gets renamed as B, so on..

Any help would be very much appreciated. Thanks!!

VBA Code:
Sub GetSheets()
Application.DisplayAlerts = False
Path = "C:\Users\cheong\Desktop\New Folder\"
Filename = Dir(Path & "*.xlsx")
Do While Filename <> ""
    Workbooks.Open Filename:=Path & Filename
    With ActiveWorkbook
        .Worksheets(1).Copy After:=ThisWorkbook.Sheets(1)
    End With
Workbooks(Filename).Close
    Filename = Dir()
Loop

End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Add this just below With ActiveWorkbook

.Sheets(1).Name = ActiveWorkbook.Name
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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