Vba to open all workbooks and copy a specif sheet to master workbook

Diras

New Member
Joined
Mar 16, 2020
Messages
8
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have about 100 files and i need to extract one specific sheet name "ADDBC" to a Master Workbook.
The ideia is to have the master workbook with the the sheetname+fileorigin.
So on master workbook i would have sheets:

ADDBC File 1 / ADDBC Files 2 / ADDBC Files 2.

I have tried several macros found but no luck
...
Could anyone help me please .
Thank you
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this macro. Change the folder path and file extension (in red) to suit your needs. Keep in mind that sheet names can have no more than 31 characters so if you have a file name of more than 25 characters (this takes into account "ADDBC "), an error will be generated. The other alternative would be to modify the macro to truncate the name to 31 characters. Please advise.
Rich (BB code):
Sub CopySheet()
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook, wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Const strPath As String = "C:\Test\" 'change folder path to suit your needs
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            .Sheets("ADDBC").Copy after:=wkbDest.Sheets(wkbDest.Sheets.Count)
            wkbDest.Sheets(wkbDest.Sheets.Count).Name = "ADDBC" & " " & Left(wkbSource.Name, InStr(wkbSource.Name, ".") - 1)
            .Close savechanges:=True
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this macro. Change the folder path and file extension (in red) to suit your needs. Keep in mind that sheet names can have no more than 31 characters so if you have a file name of more than 25 characters (this takes into account "ADDBC "), an error will be generated. The other alternative would be to modify the macro to truncate the name to 31 characters. Please advise.
Rich (BB code):
Sub CopySheet()
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook, wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Const strPath As String = "C:\Test\" 'change folder path to suit your needs
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            .Sheets("ADDBC").Copy after:=wkbDest.Sheets(wkbDest.Sheets.Count)
            wkbDest.Sheets(wkbDest.Sheets.Count).Name = "ADDBC" & " " & Left(wkbSource.Name, InStr(wkbSource.Name, ".") - 1)
            .Close savechanges:=True
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub

Thank you very much for the code.
I works well for the files which the names is short.
Who i would increde the file name to more caracteres?

Thank you in advance.
 
Upvote 0
Excel does not allow any sheet names to be longer than 31 characters. Do you want the macro to shorten all the sheet names to 31 characters?
 
Upvote 0
Try:
VBA Code:
Sub CopySheet()
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook, wkbSource As Workbook
    Set wkbDest = ThisWorkbook
    Const strPath As String = "C:\Test\" 'change folder path to suit your needs
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            .Sheets("ADDBC").Copy after:=wkbDest.Sheets(wkbDest.Sheets.Count)
            wkbDest.Sheets(wkbDest.Sheets.Count).Name = Left("ADDBC" & " " & Left(wkbSource.Name, InStr(wkbSource.Name, ".") - 1), 31)
            .Close savechanges:=True
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks a lot for your help budy. Worked well for my needs.
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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