Solved

Correct consolidated data from mulltiple worksheets into a worksheet of a same workbook.

Posted on 2011-03-21
9
244 Views
Last Modified: 2012-05-11
Hello Experts,

Thomas and Dave from EE were so kind to help me consolidate multiple worksheets.  I'm very happy with the results.  But when I run the macro  I experience some errors as following:
Please see my file attached.

1/  Please note I highlighted in yellow the names in column B.  These names are represent the tabs name for each worksheet in the workbook.  I don't want these names here.

2/  Please note the data roll up from SWIFT worksheet are missing.  It begins with 2 blank SWIFT row, and end with #12 data.  Missing #13 and #14.

3/  The data also are missing from PEG worksheet.  It begins with 2 blank PEG rows like SWIFT above, and end with #4.  Missing #5 and #6 data.

4/  It would be nice if the header can roll over too.

Can you please help.

Thanks.

Kathryn

Test-3.xls
0
Comment
Question by:KathrynT
  • 6
  • 3
9 Comments
 
LVL 39

Expert Comment

by:nutsch
Comment Utility
Hi Kathryn,

Here is an update that will fix #1 and #4 for you. #2 and 3 come because you have two empty lines in row 151 and 152 of both these tabs? Should the code take care of that too?

Thomas




Sub consolidateSheets()
Dim shtDone As Worksheet, lstRow As Long
Dim wksht As Worksheet, firstSheet As Boolean

Const bolTitles As Boolean = True 'True if sheets have titles, false if they don't
Const strSummary As String = "All" ' update to the name of the consolidated destination
Const bolTab As Boolean = f 'get data from tab name ? True / False
Const strTabTitle As String = "Type" 'title of column from tab name if bolTab=true
Dim lgTabCol As Long

'Call TurnOffAllUpdates(True)

Set shtDone = Sheets.Add

On Error Resume Next
shtDone.Name = strSummary

If Err.Number <> 0 Then
    ActiveWorkbook.Sheets(strSummary).Delete
    shtDone.Name = strSummary
    Err.Clear
End If
Application.ScreenUpdating = False
firstSheet = True

For Each wksht In ActiveWorkbook.Sheets
    
    If wksht.Name = strSummary Or Not wksht.Visible = xlSheetVisible Then GoTo nxtSht
    
    lstRow = shtDone.Range("A" & Rows.Count).End(xlUp).Row
    
    wksht.[a152].CurrentRegion.Resize(wksht.Cells(Rows.Count, 1).End(xlUp).Row - 150).Copy _
            shtDone.Cells(lstRow + 1, 1)

    If bolTitles = True And firstSheet = False Then
        Rows(lstRow + 1).Delete
        Rows(lstRow + 1).Delete 'MERGED TITLE ROW
    Else
        If bolTab = True And firstSheet = True Then
            lgTabCol = shtDone.UsedRange.Columns.Count + 1
            shtDone.Cells(2, lgTabCol) = strTabTitle
            lstRow = lstRow + 1
        End If
    End If
    
    If bolTab = True Then
        shtDone.Cells(lstRow + 1, lgTabCol) = wksht.Name
    End If
        
    firstSheet = False

nxtSht:
Next

If bolTab = True Then
    Intersect(ActiveSheet.UsedRange, Columns(lgTabCol)).Offset(1, 0).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
    Columns(lgTabCol).Copy
    ActiveSheet.PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
End If

shtDone.Cells.EntireColumn.AutoFit

Application.ScreenUpdating = True
End Sub

Open in new window

0
 

Author Comment

by:KathrynT
Comment Utility
Hi Thomas,

Thanks for your quick response.  I'm going to try out now.

Kathryn
0
 

Author Comment

by:KathrynT
Comment Utility
Hi Thomas,

I got an error message "can't execute code in brake mode".

Can you please review my attached workbook.

Thanks

Kathryn
Test-4.xls
0
 
LVL 39

Accepted Solution

by:
nutsch earned 500 total points
Comment Utility
oops

line 7 should be = False, not =f

Press F5 in Visual Basic editor to get out of the Can't execute in break mode.

Sub consolidateSheets()
Dim shtDone As Worksheet, lstRow As Long
Dim wksht As Worksheet, firstSheet As Boolean

Const bolTitles As Boolean = True 'True if sheets have titles, false if they don't
Const strSummary As String = "All" ' update to the name of the consolidated destination
Const bolTab As Boolean = False 'get data from tab name ? True / False
Const strTabTitle As String = "Type" 'title of column from tab name if bolTab=true
Dim lgTabCol As Long

'Call TurnOffAllUpdates(True)

Set shtDone = Sheets.Add

On Error Resume Next
shtDone.Name = strSummary

If Err.Number <> 0 Then
    ActiveWorkbook.Sheets(strSummary).Delete
    shtDone.Name = strSummary
    Err.Clear
End If
Application.ScreenUpdating = False
firstSheet = True

For Each wksht In ActiveWorkbook.Sheets
    
    If wksht.Name = strSummary Or Not wksht.Visible = xlSheetVisible Then GoTo nxtSht
    
    lstRow = shtDone.Range("A" & Rows.Count).End(xlUp).Row
    
    wksht.[a152].CurrentRegion.Resize(wksht.Cells(Rows.Count, 1).End(xlUp).Row - 150).Copy _
            shtDone.Cells(lstRow + 1, 1)

    If bolTitles = True And firstSheet = False Then
        Rows(lstRow + 1).Delete
        Rows(lstRow + 1).Delete 'MERGED TITLE ROW
    Else
        If bolTab = True And firstSheet = True Then
            lgTabCol = shtDone.UsedRange.Columns.Count + 1
            shtDone.Cells(2, lgTabCol) = strTabTitle
            lstRow = lstRow + 1
        End If
    End If
    
    If bolTab = True Then
        shtDone.Cells(lstRow + 1, lgTabCol) = wksht.Name
    End If
        
    firstSheet = False

nxtSht:
Next

If bolTab = True Then
    Intersect(ActiveSheet.UsedRange, Columns(lgTabCol)).Offset(1, 0).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
    Columns(lgTabCol).Copy
    ActiveSheet.PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
End If

shtDone.Cells.EntireColumn.AutoFit
shtDone.Cells.EntireRow.AutoFit
Application.ScreenUpdating = True
End Sub

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:KathrynT
Comment Utility
Thanks, Thomas.

I'll try again.

Kathryn
0
 

Author Comment

by:KathrynT
Comment Utility
Hi Thomas,

I ran the test.  It's great!!

You have solved the mystery of my  question # 1 and # 4 above.  But line #34 supposed to roll over the data from tab "SWIFT" and line # 48 to roll over the data from tab "PEG".  Both of these rows are left blank.

In summary,  the data roll up from 'SWIFT" and "PEG" tabs are not correct.

Can you please help.

Thanks again, Thomas.

Kathryn
Test-4.xls
0
 
LVL 39

Assisted Solution

by:nutsch
nutsch earned 500 total points
Comment Utility
Agreed, my initial  comment was:
#2 and 3 come because you have two empty lines in row 151 and 152 of both these tabs? Should the code take care of that too?

Let me know if you want the code to handle this, or if you will handle it prior to running the code?

Thomas
0
 

Author Comment

by:KathrynT
Comment Utility
Oop!  It's my mistake.  Let me take care of these lines and rerun your code.

I'll let you know soon.

Thanks again.

Kathryn
0
 

Author Comment

by:KathrynT
Comment Utility
Hi Thomas,

This is great !!  It works very well.

My nanager ask me if we update the rolup worksheet which we will do all the works to it, can the others worksheet update automatically.

Thanks.

Kathryn
Test-4.xls
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now