Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VBA script to stack sheets into one

Posted on 2015-02-13
3
Medium Priority
?
86 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 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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 describes a serious pitfall that can happen when deleting shapes using VBA.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

963 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