SOLVED

Excel VBA code to copy a range of data from certain tabs into summary tab

%3CLINGO-SUB%20id%3D%22lingo-sub-3271974%22%20slang%3D%22en-US%22%3EExcel%20VBA%20code%20to%20copy%20a%20range%20of%20data%20from%20certain%20tabs%20into%20summary%20tab%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3271974%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20everyone!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20several%20workbooks%20(i%20prepared%20two%20WB1%20and%20WB2%20for%20this%20example%20but%20it%20is%20over%2030%20workbooks)%20that%20have%20common%20tabs%20like%20Summary%20tab%20and%20other%20tabs%20with%20tab%20names%20that%20start%20with%20%22WLD%22%20(it%20could%20be%20WLD%20-%20ABC0123%2C%20WLD%20-%20DEF7845%2C%20etc.).%20I%20need%20a%20universal%20code%20that%20I%20can%20use%20for%20all%20workbooks%20to%20copy%20data%20from%20the%20same%20ranges%20in%20%22WLD%22%20tabs%20into%20a%20table%20in%20Summary%20tabs%20in%20each%20workbook.%20All%20tables%20in%20the%20WLD%20tabs%20are%20on%20the%20same%20range.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20note%20that%20the%20tab%20names%20are%20not%20set%20in%20stone%20and%20they%20are%20generated%20automatically%2C%20but%20the%20names%20always%20start%20with%20%22WLD%22.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20I%20did%20to%20get%20to%20the%20end%20result%20(that%20is%20displayed%20in%20each%20Summary%20tab%20of%20each%20workbook)%20is%20I%20applied%20filter%20in%20each%20tab%20to%20filter%20out%20blanks%20and%20copied%20(copy%2Fpaste%20as%20values)%20the%20data%20one%20by%20one%20from%20each%20tab%20into%20the%20summary%20tab.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20recording%20macros%20and%20modifying%20them%20to%20fit%20my%20demands%20but%20it%20would%20not%20work%20for%20all%20workbooks%20and%20I%20could%20not%20get%20it%20to%20read%20from%20the%20tabs%20with%20different%20names.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20the%20link%20to%20my%20OneDrive%20where%20I%20have%20posted%20two%20example%20workbooks%20and%20also%20attaching%20the%20files%20in%20case%20OneDrive%20does%20not%20function.%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2F1drv.ms%2Fu%2Fs!Aqv4zBSFNKaymUVviJkn91U_xY6u%3Fe%3Di6WuFl%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2F1drv.ms%2Fu%2Fs!Aqv4zBSFNKaymUVviJkn91U_xY6u%3Fe%3Di6WuFl%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20in%20advance.%3C%2FP%3E%3CP%3EAN.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3271974%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3272670%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20VBA%20code%20to%20copy%20a%20range%20of%20data%20from%20certain%20tabs%20into%20summary%20tab%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3272670%22%20slang%3D%22en-US%22%3EHans%2C%20it%20worked!%20Thank%20you%20for%20your%20great%20effort%20and%20help!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3272592%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20VBA%20code%20to%20copy%20a%20range%20of%20data%20from%20certain%20tabs%20into%20summary%20tab%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3272592%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1165762%22%20target%3D%22_blank%22%3E%40alex_n%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETry%20this%20version%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20Summarize()%0A%20%20%20%20Dim%20ws%20As%20Worksheet%0A%20%20%20%20Dim%20wt%20As%20Worksheet%0A%20%20%20%20Dim%20rngs%20As%20Range%0A%20%20%20%20Dim%20rngt%20As%20Range%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Set%20wt%20%3D%20Worksheets(%22Summary%22)%0A%20%20%20%20wt.UsedRange.Offset(1).Clear%0A%20%20%20%20For%20Each%20ws%20In%20Worksheets%0A%20%20%20%20%20%20%20%20If%20ws.Name%20Like%20%22WLD%20-*%22%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20Set%20rngs%20%3D%20ws.Range(%22F2%3AJ147%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20rngs.AutoFilter%20Field%3A%3D1%2C%20Criteria1%3A%3D%22%26lt%3B%26gt%3B%22%0A%20%20%20%20%20%20%20%20%20%20%20%20rngs.Offset(1).Copy%0A%20%20%20%20%20%20%20%20%20%20%20%20Set%20rngt%20%3D%20wt.Range(%22A%22%20%26amp%3B%20Rows.Count).End(xlUp).Offset(1)%0A%20%20%20%20%20%20%20%20%20%20%20%20rngt.PasteSpecial%20Paste%3A%3DxlPasteValues%0A%20%20%20%20%20%20%20%20%20%20%20%20rngs.AutoFilter%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%20ws%0A%20%20%20%20Application.CutCopyMode%20%3D%20False%0A%20%20%20%20wt.UsedRange.EntireColumn.AutoFit%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3272577%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20VBA%20code%20to%20copy%20a%20range%20of%20data%20from%20certain%20tabs%20into%20summary%20tab%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3272577%22%20slang%3D%22en-US%22%3EHans%2C%20thank%20you%20for%20the%20help.%20The%20code%20works%20great!%20Would%20you%20mind%20modifying%20your%20code%20to%20accommodate%20the%20following%3A%201)%20I%20want%20only%20the%20data%20from%20WLDs%20in%20range%20F3%3AJ147%20to%20be%20copied%20over%20to%20Summary%20and%202)%20paste%20as%20values%20only%2C%20not%20the%20formatting.%3CBR%20%2F%3E%3CBR%20%2F%3EThanks%20again!%3CBR%20%2F%3EAN%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3272034%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20VBA%20code%20to%20copy%20a%20range%20of%20data%20from%20certain%20tabs%20into%20summary%20tab%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3272034%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1165762%22%20target%3D%22_blank%22%3E%40alex_n%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHere%20is%20a%20macro.%20It%20assumes%20that%20the%20Summary%20sheet%20already%20exists%2C%20with%20a%20header%20row.%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20Summarize()%0A%20%20%20%20Dim%20ws%20As%20Worksheet%0A%20%20%20%20Dim%20wt%20As%20Worksheet%0A%20%20%20%20Dim%20rng%20As%20Range%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Set%20wt%20%3D%20Worksheets(%22Summary%22)%0A%20%20%20%20wt.UsedRange.Offset(1).Clear%0A%20%20%20%20For%20Each%20ws%20In%20Worksheets%0A%20%20%20%20%20%20%20%20If%20ws.Name%20Like%20%22WLD%20-*%22%20Then%0A%20%20%20%20%20%20%20%20%20%20%20%20ws.UsedRange.AutoFilter%20Field%3A%3D1%2C%20Criteria1%3A%3D%22%26lt%3B%26gt%3B%22%0A%20%20%20%20%20%20%20%20%20%20%20%20Set%20rng%20%3D%20wt.Range(%22A%22%20%26amp%3B%20Rows.Count).End(xlUp).Offset(1)%0A%20%20%20%20%20%20%20%20%20%20%20%20ws.UsedRange.Offset(1).Copy%20Destination%3A%3Drng%0A%20%20%20%20%20%20%20%20%20%20%20%20ws.UsedRange.AutoFilter%0A%20%20%20%20%20%20%20%20End%20If%0A%20%20%20%20Next%20ws%0A%20%20%20%20Application.CutCopyMode%20%3D%20False%0A%20%20%20%20wt.UsedRange.EntireColumn.AutoFit%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Hello everyone!

 

I have several workbooks (i prepared two WB1 and WB2 for this example but it is over 30 workbooks) that have common tabs like Summary tab and other tabs with tab names that start with "WLD" (it could be WLD - ABC0123, WLD - DEF7845, etc.). I need a universal code that I can use for all workbooks to copy data from the same ranges in "WLD" tabs into a table in Summary tabs in each workbook. All tables in the WLD tabs are on the same range.

 

Please note that the tab names are not set in stone and they are generated automatically, but the names always start with "WLD".

 

What I did to get to the end result (that is displayed in each Summary tab of each workbook) is I applied filter in each tab to filter out blanks and copied (copy/paste as values) the data one by one from each tab into the summary tab.

 

I tried recording macros and modifying them to fit my demands but it would not work for all workbooks and I could not get it to read from the tabs with different names.

 

Here is the link to my OneDrive where I have posted two example workbooks and also attaching the files in case OneDrive does not function.

https://1drv.ms/u/s!Aqv4zBSFNKaymUVviJkn91U_xY6u?e=i6WuFl

 

Thanks in advance.

AN.

 

4 Replies

@alex_n 

Here is a macro. It assumes that the Summary sheet already exists, with a header row.

Sub Summarize()
    Dim ws As Worksheet
    Dim wt As Worksheet
    Dim rng As Range
    Application.ScreenUpdating = False
    Set wt = Worksheets("Summary")
    wt.UsedRange.Offset(1).Clear
    For Each ws In Worksheets
        If ws.Name Like "WLD -*" Then
            ws.UsedRange.AutoFilter Field:=1, Criteria1:="<>"
            Set rng = wt.Range("A" & Rows.Count).End(xlUp).Offset(1)
            ws.UsedRange.Offset(1).Copy Destination:=rng
            ws.UsedRange.AutoFilter
        End If
    Next ws
    Application.CutCopyMode = False
    wt.UsedRange.EntireColumn.AutoFit
    Application.ScreenUpdating = True
End Sub
Hans, thank you for the help. The code works great! Would you mind modifying your code to accommodate the following: 1) I want only the data from WLDs in range F3:J147 to be copied over to Summary and 2) paste as values only, not the formatting.

Thanks again!
AN
best response confirmed by alex_n (Occasional Contributor)
Solution

@alex_n 

Try this version:

Sub Summarize()
    Dim ws As Worksheet
    Dim wt As Worksheet
    Dim rngs As Range
    Dim rngt As Range
    Application.ScreenUpdating = False
    Set wt = Worksheets("Summary")
    wt.UsedRange.Offset(1).Clear
    For Each ws In Worksheets
        If ws.Name Like "WLD -*" Then
            Set rngs = ws.Range("F2:J147")
            rngs.AutoFilter Field:=1, Criteria1:="<>"
            rngs.Offset(1).Copy
            Set rngt = wt.Range("A" & Rows.Count).End(xlUp).Offset(1)
            rngt.PasteSpecial Paste:=xlPasteValues
            rngs.AutoFilter
        End If
    Next ws
    Application.CutCopyMode = False
    wt.UsedRange.EntireColumn.AutoFit
    Application.ScreenUpdating = True
End Sub
Hans, it worked! Thank you for your great effort and help!