Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2011-03-21
9
247 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
ID: 35183514
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
ID: 35183546
Hi Thomas,

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

Kathryn
0
 

Author Comment

by:KathrynT
ID: 35185032
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 39

Accepted Solution

by:
nutsch earned 500 total points
ID: 35185058
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
 

Author Comment

by:KathrynT
ID: 35185104
Thanks, Thomas.

I'll try again.

Kathryn
0
 

Author Comment

by:KathrynT
ID: 35185185
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
ID: 35185207
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
ID: 35185273
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
ID: 35185488
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

856 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