+ Reply to Thread
Results 1 to 3 of 3

Consolidating Worksheets with VBA

  1. #1
    Registered User
    Join Date
    07-21-2017
    Location
    NY
    MS-Off Ver
    16.0
    Posts
    25

    Smile Consolidating Worksheets with VBA

    Hi Everyone,

    I am working on a project for fun but I am struggling to consolidate a few worksheets (not all worksheets) with VBA into a master worksheet.

    The idea is to copy all data over from tabs with "ABC" in the worksheet name, i.e. copy from "ABC 1", "ABC 2", "ABC A" but not "XYZ".

    All the ABC worksheets have the same header, so I would like to retain this only once / the first time when consolidating the data.

    If anyone can figure out the code, that would be really helpful!

    Thanks!

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    Excel 2010 on Linux - O365
    Posts
    12,687

    Re: Consolidating Worksheets with VBA

    Have a look at https://www.rondebruin.nl/win/s3/win002.htm
    or
    https://www.rondebruin.nl/win/section3.htm

  3. #3
    Registered User
    Join Date
    07-21-2017
    Location
    NY
    MS-Off Ver
    16.0
    Posts
    25

    Re: Consolidating Worksheets with VBA

    I have come across that site, however, the code I copied and altered does not seem to show an output in the "Master ABC" tab.

    This is what I have so far. Any thoughts on where it is going wrong or how to fix it?

    Thanks!

    Module 1

    Function LastRow(sh As Worksheet)
    On Error Resume Next
    LastRow = sh.Cells.Find(What:="*", _
    After:=sh.Range("A1"), _
    Lookat:=xlPart, _
    LookIn:=xlFormulas, _
    SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, _
    MatchCase:=False).Row
    On Error GoTo 0
    End Function


    Module 2

    Sub Consolidate_ABC()
    Dim sh As Worksheet
    Dim DestSh As Worksheet
    Dim Last As Long
    Dim shLast As Long
    Dim CopyRng As Range
    Dim StartRow As Long

    With Application
    .ScreenUpdating = False
    .EnableEvents = False
    End With

    'Delete the sheet "Master ABC" if it exist
    Application.DisplayAlerts = False
    On Error Resume Next
    ActiveWorkbook.Worksheets("Master ABC").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True

    'Add a worksheet with the name "Master ABC"
    Set DestSh = ActiveWorkbook.Worksheets.Add
    DestSh.Name = "Master ABC"

    'Fill in the start row
    StartRow = 2

    'loop through all worksheets and copy the data to the DestSh
    For Each sh In ActiveWorkbook.Worksheets
    If LCase(Left(sh.Name, 4)) = "ABC " Then

    'Find the last row with data on the DestSh
    Last = LastRow(DestSh)

    'Fill in the range that you want to copy
    Set CopyRng = sh.Range("A1").CurrentRegion

    'Test if there enough rows in the DestSh to copy all the data
    If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
    MsgBox "There are not enough rows in the Destsh"
    GoTo ExitTheSub
    End If

    'Copy header row, change the range if you use more columns
    If WorksheetFunction.CountA(DestSh.UsedRange) = 0 Then
    sh.Range("A1:AZ1").Copy DestSh.Range("A1")
    End If

    'This example copies values/formats, if you only want to copy the
    'values or want to copy everything look at the example below this macro
    CopyRng.Copy
    With DestSh.Cells(Last + 1, "A")
    .PasteSpecial xlPasteValues
    .PasteSpecial xlPasteFormats
    Application.CutCopyMode = False
    End With

    End If
    Next

    ExitTheSub:

    Application.Goto DestSh.Cells(1)

    'AutoFit the column width in the DestSh sheet
    DestSh.Columns.AutoFit

    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With

    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Consolidating worksheets
    By Tendekai in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-12-2018, 12:43 PM
  2. [SOLVED] Consolidating worksheets
    By davidman1 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-06-2012, 02:26 PM
  3. Consolidating worksheets and then referencing the worksheets themselves in a new column
    By zed commander in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-09-2012, 05:14 AM
  4. Consolidating worksheets
    By Cand in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-09-2009, 05:43 PM
  5. Consolidating Worksheets
    By ctf in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-28-2008, 04:48 PM
  6. [SOLVED] Consolidating worksheets
    By Joeflo in forum Excel General
    Replies: 1
    Last Post: 04-22-2006, 06:47 PM
  7. [SOLVED] Consolidating multiple worksheets
    By Laura T. in forum Excel General
    Replies: 1
    Last Post: 08-16-2005, 07:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

X vBulletin 4.1.8 Debug Information

  • Page Generation 0.05819 seconds
  • Memory Usage 8,936KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)footer
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (3)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (4)navbar_noticebit
  • (6)option
  • (3)postbit_legacy
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (4)showthread_bookmarksite
  • (7)showthread_similarthreadbit
  • (1)showthread_similarthreads
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper
  • (1)vbseo_linkbackmenu_entry 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (39):
  • ./vbseo.php
  • ./env.php
  • ./vbseo/includes/functions_vbseo.php
  • ./vbseo/includes/functions_vbseo_pre.php
  • ./vbseo/includes/functions_vbseo_url.php
  • ./vbseo/includes/functions_vbseo_createurl.php
  • ./vbseo/includes/functions_vbseo_db.php
  • ./vbseo/includes/functions_vbseo_vb.php
  • ./vbseo/includes/functions_vbseo_seo.php
  • ./vbseo/includes/functions_vbseo_misc.php
  • ./vbseo/includes/functions_vbseo_crr.php
  • ./vbseo/includes/functions_vbseo_cache.php
  • ./vbseo/includes/functions_vbseo_hook.php
  • ./vbseo/includes/functions_vbseo_startup.php
  • ./includes/config.php
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/functions.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/functions_cforum.php
  • ./includes/functions_facebook.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (49):
  • init_startup
  • friendlyurl_resolve_class
  • database_pre_fetch_array
  • database_post_fetch_array
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • fetch_musername
  • cache_templates
  • template_register_var
  • parse_templates
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • reputation_image
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • showthread_similarthread_query
  • showthread_similarthreadbit
  • forumrules
  • showthread_bookmarkbit
  • navbits
  • navbits_complete
  • showthread_complete


Search Engine Friendly URLs by vBSEO 3.6.0 RC 1