VBA code for multiple xls files

bruggied

New Member
Joined
Sep 2, 2014
Messages
2
i want that a vba code can be repeated to multiple xls files
what is the meaning of it, it needs to copy a sheet from the original file C95_TEAM.XLS to 15 other xls files
I have this vba code to copy the sheet but I want to repeat this to the other files
I used a vba code to look into a folder but also includes the file C95_TEAM.XLS
So I don’t a way to run this code to several xls files in the same folder
this is the code :

sheetname = InputBox("Vul de sheetname in.")
Windows("C95_TEAM.XLS").Activate
ChDir "C:\noliko-msk-2014-2015-vb"
Workbooks.Open Filename:="C:\noliko-msk-2014-2015-vb\C95_3_3.XLS"
Windows("C95_TEAM.XLS").Activate
Sheets(sheetname).Select
Sheets(sheetname).Copy After:=Workbooks("C95_3_3.XLS").Sheets(1)
Windows("C95_3_3.XLS").Activate
Sheets("3_3").Select
Range("A1").Select
Workbooks("C95_3_3.XLS").Close savechanges:=True
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try this. Put the code in a module in the C95_TEAM.XLS workbook.
Code:
Public Sub Copy_Sheet_To_Workbooks()

    Dim sheetName As String
    Dim fileName As String
    Dim wb As Workbook
    
    sheetName = InputBox("Name of sheet to be copied")
    If sheetName = "" Then Exit Sub
    
    fileName = Dir(ThisWorkbook.Path & "\*.xls")
    While fileName <> ""
        If fileName <> ThisWorkbook.Name Then
            Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & fileName)
            ThisWorkbook.Sheets(sheetName).Copy After:=wb.Sheets(1)
            wb.Close True
        End If
        fileName = Dir()
    Wend

End Sub
 
Upvote 0
Try this. Put the code in a module in the C95_TEAM.XLS workbook.
Code:
Public Sub Copy_Sheet_To_Workbooks()

    Dim sheetName As String
    Dim fileName As String
    Dim wb As Workbook
    
    sheetName = InputBox("Name of sheet to be copied")
    If sheetName = "" Then Exit Sub
    
    fileName = Dir(ThisWorkbook.Path & "\*.xls")
    While fileName <> ""
        If fileName <> ThisWorkbook.Name Then
            Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & fileName)
            ThisWorkbook.Sheets(sheetName).Copy After:=wb.Sheets(1)
            wb.Close True
        End If
        fileName = Dir()
    Wend

End Sub


tnx John i'll try this out and let you know it it's worked
 
Upvote 0

Forum statistics

Threads
1,224,315
Messages
6,177,845
Members
452,810
Latest member
jeffrey0409

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