Solved

Excel VBA script to stack sheets into one

Posted on 2015-02-13
3
75 Views
Last Modified: 2016-02-10
Hello Experts!

I would like some help with a script that will do the following:

For every sheet that has *棚卸* in the tab name, copy cells from A5:T(end - 2 rows) and append to a new sheet. Add column to the new sheet that contains the name of the source tab.

Thanks!

-Tom
0
Comment
Question by:tomfolinsbee
  • 2
3 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40609495
Hi Tom, could you post a sample workbook? Thanks.

I'm thinking the solution will be very similar to this one. Just a case of identifying the character codes in the tab name.

In my version of Excel/language I can't use "*" in a tab name. Does yours actually include the asterisks or did you use those as wildcards?
0
 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40609750
I've assumed you ARE using it as wildcard and that you want the data from all tabs that contain that pattern written to a single "Results" sheet.

Sub FindSheetsByTabnameWriteToResults()
Dim charString As String
Dim copyRange As Range
Dim shtName As String
Dim sht As Worksheet, resultSht As Worksheet

For Each sht In ActiveWorkbook.Sheets
    If sht.Name = "Results" Then
        Set resultSht = sht
        Exit For
    End If
Next

If resultSht Is Nothing Then
    Set resultSht = ActiveWorkbook.Worksheets.Add
    resultSht.Name = "Results"
    resultSht.Cells(1, 1).Value = "SourceSheet"
End If

charString = "*" & ChrW(26842) & ChrW(21368) & "*"
For Each sht In ActiveWorkbook.Sheets
    If sht.Name Like charString Then
        shtName = sht.Name
        Debug.Print "found sheet " & shtName & " using pattern " & charString
        Set copyRange = Intersect(sht.Range("$A:$T"), sht.UsedRange).Offset(4, 0) 'omit first 4 rows
        Set copyRange = copyRange.Resize(copyRange.Rows.Count - 6) ' adjust for the offset and omit the last two rows
        Debug.Print shtName, copyRange.Address
        With resultSht
            lastrow = .UsedRange.Rows.Count
            .Range(.Cells(lastrow + 1, 1), .Cells(lastrow + copyRange.Rows.Count, 1)).Value = shtName
            .Range(.Cells(lastrow + 1, 2), .Cells(lastrow + copyRange.Rows.Count, copyRange.Columns.Count + 1)).Value = copyRange.Value
        End With
    End If
Next
End Sub

Open in new window

0
 

Author Closing Comment

by:tomfolinsbee
ID: 40619915
yes, using the * as a wildcard. Thanks again for your help!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

696 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