There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Business Applications
Tag Cloud
audio blue blue screen boot bsod compaq computer cpu crash dell drivers dvd error excel firefox format freeze freezing hard drive install internet internet explorer kb951748 lan laptop loss of internet malware memory motherboard network networking outlook outlook 2007 problem restart screen security slow sound startup trojan update virus vista windows windows xp winxp wireless zone alarm zonealarm
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Business Applications >
concatenate worksheets with different number of variables


Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
rochey's Avatar
Junior Member with 6 posts.
 
Join Date: Aug 2008
25-Aug-2008, 10:06 AM #1
Smile concatenate worksheets with different number of variables
Hi guys, hope someone can help with this, though I'm not sure if it's really possible.

I've got hundreds of Excel sheets which all use the same variable (column header) names, except that they all have different numbers of variables. For example one sheet might have:

S_1 | S_11 | S_111 | S_112 | S_12 | S_121 | S_122 | S_2... etc

Whereas another sheet might just have:

S_1 | S_11 | S_12 | S_2

So if you can imagine, hundreds of sheets which all use the same coding for the variables, but each of which reporting different numbers of variables.

I want to get all of the sheets into a single sheet, the eventual goal is to convert it to just one Stata file, but the fact that the sheets are not of a consistent format is a real problem.

What I really need is for Excel to recognise the header names and move data according to those, rather than according to which cells data occupy. Is there some kind of macro which can do this? Or am I just going to have to do it manually? (which will take all year!)
slurpee55's Avatar
Computer Specs
Distinguished Member with 4,517 posts.
 
Join Date: Oct 2004
Location: Southwest Iowa....
Experience: Currently stupid...
25-Aug-2008, 10:28 AM #2
can you post a sample - fake some data if you need to.
rochey's Avatar
Junior Member with 6 posts.
 
Join Date: Aug 2008
25-Aug-2008, 11:01 AM #3
okay, i've attached a file - it's publicly available data so no worries on that front.
So i want to automate a process whereby i convert the three worksheets into a single sheet. But if I did this through a concatenate type of thing, it would put Cyprus S_2122 underneath Maldives S_211, because it's column L. I suppose I'd need to create a template which has all possible series names, and somehow automate it from there?
slurpee55's Avatar
Computer Specs
Distinguished Member with 4,517 posts.
 
Join Date: Oct 2004
Location: Southwest Iowa....
Experience: Currently stupid...
25-Aug-2008, 11:03 AM #4
I don't see an attachment - if it is there, alas, I probably won't see it - I don't know why, but the system has some problems.
rochey's Avatar
Junior Member with 6 posts.
 
Join Date: Aug 2008
25-Aug-2008, 11:08 AM #5
oops... let's see if the attachment works this time round...
Attached Files
File Type: xls Test Workbook1.xls (135.5 KB, 15 views)
slurpee55's Avatar
Computer Specs
Distinguished Member with 4,517 posts.
 
Join Date: Oct 2004
Location: Southwest Iowa....
Experience: Currently stupid...
25-Aug-2008, 11:11 AM #6
Do you have a full listing of the various possible headings?
rochey's Avatar
Junior Member with 6 posts.
 
Join Date: Aug 2008
25-Aug-2008, 11:14 AM #7
I don't currently, but its possible for me to make one, although it'll take a while
slurpee55's Avatar
Computer Specs
Distinguished Member with 4,517 posts.
 
Join Date: Oct 2004
Location: Southwest Iowa....
Experience: Currently stupid...
25-Aug-2008, 11:20 AM #8
It would probably be possible to collect the data based on the column headings on each sheet and then, once that is done, combine all the data based on that.
rochey's Avatar
Junior Member with 6 posts.
 
Join Date: Aug 2008
25-Aug-2008, 11:29 AM #9
that's what i was thinking... any ideas how i would go about doing that?
slurpee55's Avatar
Computer Specs
Distinguished Member with 4,517 posts.
 
Join Date: Oct 2004
Location: Southwest Iowa....
Experience: Currently stupid...
25-Aug-2008, 11:37 AM #10
Probably a formula using INDIRECT - if you can post the headers, we can do a search and match
see this post for something similar in concept
http://forums.techguy.org/business-a...ls-macros.html
rochey's Avatar
Junior Member with 6 posts.
 
Join Date: Aug 2008
25-Aug-2008, 02:23 PM #11
okay, here is a template which (hopefully) has every possible header, at least in the context of the example sheet. So, it's possible to write a macro that will move the data in the three sheets to this new template sheet, and place the data according to the header names?
Attached Files
File Type: xls template 1.xls (13.5 KB, 13 views)
OBP's Avatar
OBP OBP is offline
Computer Specs
Distinguished Member with 6,863 posts.
 
Join Date: Mar 2005
Location: UK
Experience: An old Basic Programmer
25-Aug-2008, 03:59 PM #12
I would add Names to your Column Headers that basically match the Header title, that should make writing the macro that much easier to implement.
slurpee55's Avatar
Computer Specs
Distinguished Member with 4,517 posts.
 
Join Date: Oct 2004
Location: Southwest Iowa....
Experience: Currently stupid...
25-Aug-2008, 06:28 PM #13
Hmmm, gave you the wrong link....
Oh well, here is a start - thanks to AJ_Old in another thread, this formula looks up the value in the top of the column and finds it in the column with the data below it.
Do this for each page - keeping all the headings in always the same place (start at column AR) and then you will have the data with the same formatting for each page. It should be easy to combine them later.
Oh, and it says there is a circular reference in a formula - I can't find it....
Attached Files
File Type: zip Test Workbook2.zip (40.7 KB, 9 views)
__________________
Iowa? I could have sworn this was heaven.
Well, I think I can answer this question most successfully in mime.

My theme song...
Aj_old's Avatar
Computer Specs
Senior Member with 624 posts.
 
Join Date: Sep 2007
Location: Moldova
Experience: Intermediate
27-Aug-2008, 07:58 AM #14
Rochey, try this code:
Code:
Sub DoWhatIWanna()
Application.ScreenUpdating = False

'Adds a new sheet and names it to "Master"
Sheets.Add before:=Sheets(1)
Set shm = ActiveSheet
shm.Name = "Master"

' Freezes names (first row, and first 9 columns, to the right of column I)
Range("J2").Select
ActiveWindow.FreezePanes = True

' Copy the header form the first line, of the second sheet, to the master sheet
Sheets(2).Rows("1:1").Copy
shm.Cells(1, 1).PasteSpecial xlPasteValues
Application.CutCopyMode = False

'Copy data from all the sheets to the master one and put it in right place!
For Each sh In ActiveWorkbook.Sheets
    If sh.Name <> "Master" And sh.Name = sh.Cells(2, 1).Value Then
        rws = sh.Range("A65536").End(xlUp).Row
        cls = sh.Range("IV1").End(xlToLeft).Column
        rw = shm.Cells(65536, 1).End(xlUp).Row + 1
        With WorksheetFunction
        cl = 1
            For i = 1 To cls
                countt = .CountIf(shm.Rows("1:1"), sh.Cells(1, i).Value)
                If countt = 1 Then
                    cl = .Match(sh.Cells(1, i).Value, shm.Rows("1:1"), 0)
                    shm.Cells(rw, cl).Resize(rws - 1, 1).Value = sh.Cells(2, i).Resize(rws - 1, 1).Value
                ElseIf countt = 0 Then
                    cl = .Match(sh.Cells(1, i).Value, shm.Rows("1:1"), 1) + 1 'cl + 1
                    shm.Cells(rw, cl).EntireColumn.Insert shift:=xlToRight
                    shm.Cells(rw, cl).Resize(rws - 1, 1).Value = sh.Cells(2, i).Resize(rws - 1, 1).Value
                    shm.Cells(1, cl).Value = sh.Cells(1, i)
                Else
                
                End If
            Next i
        End With
    End If
Next sh
shm.Select
Range("A1").Select
Application.ScreenUpdating = True
End Sub
Please tell us how it works!
slurpee55's Avatar
Computer Specs
Distinguished Member with 4,517 posts.
 
Join Date: Oct 2004
Location: Southwest Iowa....
Experience: Currently stupid...
27-Aug-2008, 10:20 AM #15
AJ, would your code here

Sheets(2).Rows("1:1").Copy
shm.Cells(1, 1).PasteSpecial xlPasteValues
Application.CutCopyMode = False

require that the sheet have all the possible values for headers in it?
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 04:10 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.