Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Excel VBA script to stack sheets into one

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

828 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