0

I need to move some worksheets from one workbook to another. However, the worksheet names are not always the same so I would need to set up a series with the "FOR". Below is the code I wrote which is applicable only to the worksheets named "template" and I need to make it applicable to all the worksheets of a workbook.

Thanks in advance

Private Sub CommandButton1_Click()

Dim Path As String
Dim Filename As String
Dim master As String
Dim sh As Worksheet
Dim ActiveWB As Workbook
Set ActiveWB = ActiveWorkbook

Dim FileToOpen As String

Application.ScreenUpdating = False
Application.DisplayAlerts = False

For x = 0 To ListBox1.ListCount - 1

If ListBox1.Selected(x) = True Then

Path = "A:\"
Filename = Dir(Path & ListBox1.List(x) & ".xl??")
Workbooks.Open (Path & Filename)

Workbooks(Filename).Worksheets("template").Copy after:=ActiveWB.Sheets(1)
Windows(Filename).Activate
ActiveWindow.Close
 
End If

Next x

DFo
  • 1

0 Answers0