Solved

Add sheet names, VBA macro consolidate sheets

Posted on 2016-11-04
8
48 Views
Last Modified: 2016-11-06
Hello,

I need to modify the below code to add sheet names.  I have 8 sheets to copy.  Typing from phone...please use generic sheet names and i can edit later.  

Grateful for your kind assistance.

This is a follow up to this question yesterday:   https://www.experts-exchange.com/questions/28980993/Code-to-combine-sheets.html?anchor=a41873839¬ificationFollowed=178705137#a41873839


Sub combinesheets()
    Dim ws As Worksheet
    Dim tws As Worksheet
    Dim headerdone As Boolean
    Dim sr As Range
    Dim tr As Range
    headerdone = False
    Application.DisplayAlerts = False
        On Error Resume Next
            ActiveWorkbook.Worksheets("Combined").Delete
        On Error GoTo 0
    Application.DisplayAlerts = False
    Set tws = ActiveWorkbook.Worksheets.Add
    tws.Name = "Combined"
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "Combined" Then
            If Not headerdone Then
                ws.Range("A6:A7").EntireRow.Resize(, tws.Columns.Count - 1).Copy tws.Range("B6")
                tws.Range("B7").Copy tws.Range("A7")
                tws.Range("A7") = "Sheet name"
                headerdone = True
            End If
            Set sr = ws.Range("A8:A" & ws.Range("A8").End(xlDown).Row).Resize(, tws.Columns.Count - 1)
            Set tr = tws.Range("A" & Rows.Count).End(xlUp).Offset(1)
            sr.Copy tr.Offset(, 1)
            tr.Resize(sr.Rows.Count) = ws.Name
        End If
    Next ws
End Sub
0
Comment
Question by:pdvsa
  • 5
  • 3
8 Comments
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 41875595
This is already listing sheet names in column A of the Combined sheet
0
 

Author Comment

by:pdvsa
ID: 41875605
Ok.  I have yet to test.  In the file, there are many sheets.  In the initial question, i removed the sheets i didnt need.  

If kot mistaken, In the combined sheet, the code combines ALL sheets?
0
 

Author Comment

by:pdvsa
ID: 41875608
Not sure if clear...I only want to combine some of the sheets.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 41875609
Yes, All sheets.

How to know "which" sheets?
0
 

Author Comment

by:pdvsa
ID: 41875627
They would have certain names.  8 of the sheets out of 20 is the split.
0
 

Author Comment

by:pdvsa
ID: 41875850
Saqib,

OK I have tested and I can work with the code the way it stands.  I can filter for the sheets I do not need.  It returns the correct answer.

One thing I would like to request a modification:
Paste Values.  Is this possible?  AS of right now, the code is copying the formulas and after copying, the formulas are referencing an incorrect cell.  

Thank you so much for the help.  
Very nice.
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 41875895
This should take of both your requirements.
The 8 sheets may be modified in the case statement
Sub combinesheets()
    Dim ws As Worksheet
    Dim tws As Worksheet
    Dim headerdone As Boolean
    Dim sr As Range
    Dim tr As Range
    headerdone = False
    Application.DisplayAlerts = False
        On Error Resume Next
            ActiveWorkbook.Worksheets("Combined").Delete
        On Error GoTo 0
    Application.DisplayAlerts = False
    Set tws = ActiveWorkbook.Worksheets.Add
    tws.Name = "Combined"
    For Each ws In ActiveWorkbook.Worksheets
        Select Case ws.Name
        Case "SA-Refined Prod", "SCA-Monomer", "SCA - Poly", "ATC-Mono", "ATC-200075", "ATC - Poly", "Middle East - Poly", "Middle East-Mono"
            If Not headerdone Then
                ws.Range("A6:A7").EntireRow.Resize(, tws.Columns.Count - 1).Copy tws.Range("B6")
                tws.Range("B7").Copy tws.Range("A7")
                tws.Range("A7") = "Sheet name"
                headerdone = True
            End If
            Set sr = ws.Range("A8:A" & ws.Range("A8").End(xlDown).Row).Resize(, tws.Columns.Count - 1)
            Set tr = tws.Range("A" & Rows.Count).End(xlUp).Offset(1)
            sr.Copy tr.Offset(, 1)
            tr.Offset(, 1).Resize(sr.Rows.Count, sr.Columns.Count).Value = sr.Value
            tr.Resize(sr.Rows.Count) = ws.Name
        End Select
    Next ws
End Sub

Open in new window

0
 

Author Comment

by:pdvsa
ID: 41875928
that worked perfectly.  Thank you very much the expert assistance.  Have a good day.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel Formula to split product code 5 15
Update a text value in another table 10 39
Excel Question 17 15
Return Column Number based on a specific value 25 22
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

776 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