Solved

Excel VBA script to stack sheets into one

Posted on 2015-02-13
3
69 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

920 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

15 Experts available now in Live!

Get 1:1 Help Now