Help to copy multiple sheet from another WB and rename

nhnn1986

Board Regular
Joined
Oct 12, 2017
Messages
92
Hi all

I want to have macro code to do that:

Brown to folder to choose excel file, copy sheet(array("source1","source2","source3","source4")
then paste in active workbook with sheetname changed to sheets(array("A","B","C","D").

Thanks in advance./.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I guess you mean browse to folder with a open browser window and the 4 first sheets of the selected file

Code:
Sub ImportSheets()
 Dim DestWb As Workbook
 Dim SourceWb As Workbook
  Set DestWb = ActiveWorkbook

[COLOR=#008000]'Let user browse for the source file[/COLOR]
 Dim SourceName As Variant
 SourceName = Application.GetOpenFilename

[COLOR=#008000]'Open It[/COLOR]
 Workbooks.Open fileName:=SourceName
 Set SourceWb = ActiveWorkbook

[COLOR=#008000]'Copy Source 1st sheet and Name it "A"[/COLOR]
  SourceWb.Sheets(1).Copy After:=DestWb.Sheets(DestWb.Sheets.Count)
  ActiveSheet.Name = "A"
[COLOR=#008000]'Copy Source 2nd sheet and Name it "B"[/COLOR]
  SourceWb.Sheets(2).Copy After:=DestWb.Sheets(DestWb.Sheets.Count)
  ActiveSheet.Name = "B"
[COLOR=#008000]'Copy Source 3rd sheet and Name it "C"[/COLOR]
  SourceWb.Sheets(3).Copy After:=DestWb.Sheets(DestWb.Sheets.Count)
  ActiveSheet.Name = "C"
[COLOR=#008000]'Copy Source 4th sheet and Name it "D"[/COLOR]
  SourceWb.Sheets(1).Copy After:=DestWb.Sheets(DestWb.Sheets.Count)
  ActiveSheet.Name = "D"

[COLOR=#008000]'Close Source Workbook[/COLOR]
  SourceWb.Close
End Sub

If the sheets are called source1, source 2, ... simply replace
Code:
SourceWb.Sheets(1)
by
Code:
SourceWb.Sheets("source1")
 
Last edited:
Upvote 0
Code:
Sub ImportSheets()
 Dim DestWb As Workbook
 Dim SourceWb As Workbook
  Set DestWb = ActiveWorkbook

[COLOR=#008000]'Let user browse for the source file[/COLOR]
 Dim SourceName As Variant
 SourceName = Application.GetOpenFilename

[COLOR=#008000]'Open It[/COLOR]
 Workbooks.Open fileName:=SourceName
 Set SourceWb = ActiveWorkbook

  SourceWb.Sheets(array("source1","source2","source3","source4").Copy After:=DestWb.Sheets(DestWb.Sheets.Count)
  ActiveSheet.Name = array("A","B","C","D")
[COLOR=#008000]'Close Source Workbook[/COLOR]
  SourceWb.Close
End Sub
I've just change code shorter like above, but this code only copy sheets and paste but don't rename.
Could you have anothe way to fix this code
 
Upvote 0
THe easiest way is to replace
Code:
[COLOR=#333333] ActiveSheet.Name = array("A","B","C","D")[/COLOR]
by one of those macros
Code:
   Sub ArraySh()
   Dim oldSh()
    Dim NewSh()
    Dim ws As Worksheet
    Dim numSh As Long
    NewSh = Array("A", "B", "C", "D")
    oldSh = Array("source1", "source2", "source3", "source4")
    On Error Resume Next
    For numSh = LBound(oldSh) To UBound(oldSh)
        Set ws = Nothing
        Set ws = Sheets(oldSh(numSh))
        If Not ws Is Nothing Then ws.Name = NewSh(numSh)
    Next numSh
End Sub
or
Code:
Sub ArraySh2()
    Dim ArrSh
    Dim uSh
    Dim shName
    shName = Array("A", "B", "C", "D")
    Set ArrSh = Sheets(Array("source1", "source2", "source3", "source4"))
    For uSh = 1 To ArrSh.Count
        ArrSh(uSh).Name = shName(uSh - 1)
    Next
End Sub
or if you want to keep it short,
Code:
call ArraySh
but I hardly see the point of the effort
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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