Solved

Excel VBA script to stack sheets into one

Posted on 2015-02-13
3
65 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:SimonAdept
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:
SimonAdept 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

12 Experts available now in Live!

Get 1:1 Help Now