Combining select columns from multiple worksheets into one new worksheet

Renny

New Member
Joined
May 28, 2014
Messages
10
What I want to do seems frustratingly simple. I have about 10 sheets within one workbook, one for each department I am dealing with. Each sheet is structured the same way, with the same column headings. I want to create an overview worksheet that would pull the basics from each sheet and combine into one large table. Ideally, the worksheet name would be in column A on my overview page, and then I'd pull the text from columns A,B,D,G and K from each sheet to create the master list. Macro?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I'm using Excel for Mac 2011 - 14.4.3

Hi Renny,
. I have no experience with mac and I was not aware of a 2011 version of Excel. I have done the stuff for you in XL 2007 (PC with Vista). The code is fairly simple so I expect there should be no compatibility problems.
. I return you file to you with the code in it (Via Filesnack and box)
FileSnack | Easy file sharing
https://app.box.com/s/8k4k54tqsrtg7993q6eo

Here is the code (Which is in the OverviewSheet Module in your returned File)

<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><SPAN style="color:#00007F">Sub</SPAN> RennyTMW()<br><SPAN style="color:#007F00">'......sheet in my workbook, one of which is the Overview, and one is a</SPAN><br><SPAN style="color:#007F00">'reference page (that's where I keep all my data for lookup functions).</SPAN><br><SPAN style="color:#007F00">'The other 13 are all various departments for a festival I work for.</SPAN><br><SPAN style="color:#007F00">'First is the Overview page- fewer columns to condense info a bit.</SPAN><br><SPAN style="color:#007F00">'Second is the department page- all 13 have one identical to this,</SPAN><br><SPAN style="color:#007F00">'though they range in size from 1 or 2 rows to more than twenty.</SPAN><br><SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> TheEnd <SPAN style="color:#007F00">'Not necerssary but a good idea: Tells the computer what to do if an error occour rathe than possibly crashing badly!</SPAN><br>Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#007F00">'Not necerssary but speeds thimgs up a bit by not updating the screen every time.</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> MaxDeptRowNumber  <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, OverviewSheetLastEntryRowNumber <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN> <SPAN style="color:#007F00">'last Row Number used</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> RowCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, OverviewHeadingNumber <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, DeptHeadingNumber <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN> <SPAN style="color:#007F00">'Integers used as counts in loops</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> NumberOfDepts <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN> <SPAN style="color:#007F00">'Number of Departments =....</SPAN><br><SPAN style="color:#00007F">Let</SPAN> NumberOfDepts = Worksheets.Count - 2 <SPAN style="color:#007F00">'....number of worksheets - 2</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> MaxOverviewHeadingNumber <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN>, MaxDeptHeadingNumber <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN> <SPAN style="color:#007F00">' ' Number of headings you have</SPAN><br><SPAN style="color:#00007F">Let</SPAN> MaxOverviewHeadingNumber = Worksheets("Overview").Cells(3, Columns.Count).End(xlToLeft).Column<br><SPAN style="color:#00007F">Let</SPAN> MaxDeptHeadingNumber = Worksheets(2).Cells(3, Columns.Count).End(xlToLeft).Column<br><SPAN style="color:#00007F">Dim</SPAN> wkshtNumber <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Byte</SPAN> <SPAN style="color:#007F00">' Number of the Worksheet counting from the left</SPAN><br>  <SPAN style="color:#00007F">For</SPAN> wkshtNumber = 2 <SPAN style="color:#00007F">To</SPAN> NumberOfDepts + 1 <SPAN style="color:#00007F">Step</SPAN> 1 <SPAN style="color:#007F00">'Go througth all your Departments and....</SPAN><br>  <SPAN style="color:#00007F">Let</SPAN> OverviewSheetLastEntryRowNumber = Worksheets("Overview").Cells(Rows.Count, 2).End(xlUp).Row <SPAN style="color:#007F00">' First check the last entry in Overview Sheet so you know where to start putting new enries in</SPAN><br>  <SPAN style="color:#00007F">Let</SPAN> MaxDeptRowNumber = Worksheets(wkshtNumber).Cells(Rows.Count, 1).End(xlUp).Row <SPAN style="color:#007F00">' get the maximum row number in the department</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> RowCount = 4 <SPAN style="color:#00007F">To</SPAN> MaxDeptRowNumber <SPAN style="color:#007F00">' Go through every Row in the department and..</SPAN><br>    Worksheets("Overview").Cells(RowCount + OverviewSheetLastEntryRowNumber - 3, 1).Value = Worksheets(wkshtNumber).Name <SPAN style="color:#007F00">'Put the worksheet name in the first column</SPAN><br>      <SPAN style="color:#00007F">For</SPAN> OverviewHeadingNumber = 2 <SPAN style="color:#00007F">To</SPAN> MaxOverviewHeadingNumber <SPAN style="color:#00007F">Step</SPAN> 1 <SPAN style="color:#007F00">' go through every Overview heading...</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> DeptHeadingNumber = 1 <SPAN style="color:#00007F">To</SPAN> 14 <SPAN style="color:#007F00">'and for each Overview heading look at each department heading...</SPAN><br>          <SPAN style="color:#00007F">If</SPAN> Worksheets("Overview").Cells(3, OverviewHeadingNumber).Value = Worksheets(wkshtNumber).Cells(3, DeptHeadingNumber).Value <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#007F00">'If a match is found...</SPAN><br>          Worksheets("Overview").Cells(RowCount + OverviewSheetLastEntryRowNumber - 3, OverviewHeadingNumber).Value = Worksheets(wkshtNumber).Cells(RowCount, DeptHeadingNumber).Value <SPAN style="color:#007F00">'Put the appropriate value from the department in the Overview Sheet</SPAN><br>          <SPAN style="color:#00007F">Else</SPAN> <SPAN style="color:#007F00">' Otherwise do nothing and..</SPAN><br>          <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> DeptHeadingNumber <SPAN style="color:#007F00">'Go to the next Department heading until you have been through them all, then</SPAN><br>      <SPAN style="color:#00007F">Next</SPAN> OverviewHeadingNumber <SPAN style="color:#007F00">' start again for the next Overview heading</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> RowCount <SPAN style="color:#007F00">' When you finish going through the row go to the next row</SPAN><br>  <SPAN style="color:#00007F">Next</SPAN> wkshtNumber <SPAN style="color:#007F00">' when all the rows are finished go to the next Worksheet</SPAN><br>TheEnd: <SPAN style="color:#007F00">'Not necerssary but a good idea to have this: Computer comes here if there is an error</SPAN><br>Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN> <SPAN style="color:#007F00">' Important that this is always done, even in the event of an error: Turns the screen updating back on</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'RennyTMW()</SPAN></FONT>


And here again, the basic code without my ‘Green comment graffit!

Code:
Sub RennyTMW()
Let NumberOfDepts = Worksheets.Count - 2
Let MaxOverviewHeadingNumber = Worksheets("Overview").Cells(3, Columns.Count).End(xlToLeft).Column
Let MaxDeptHeadingNumber = Worksheets(2).Cells(3, Columns.Count).End(xlToLeft).Column


  For wkshtNumber = 2 To NumberOfDepts + 1 Step 1
  Let OverviewSheetLastEntryRowNumber = Worksheets("Overview").Cells(Rows.Count, 2).End(xlUp).Row
  Let MaxDeptRowNumber = Worksheets(wkshtNumber).Cells(Rows.Count, 1).End(xlUp).Row
    For RowCount = 4 To MaxDeptRowNumber
    Worksheets("Overview").Cells(RowCount + OverviewSheetLastEntryRowNumber - 3, 1).Value = Worksheets(wkshtNumber).Name
      For OverviewHeadingNumber = 2 To MaxOverviewHeadingNumber Step 1
        For DeptHeadingNumber = 1 To 14
          If Worksheets("Overview").Cells(3, OverviewHeadingNumber).Value = Worksheets(wkshtNumber).Cells(3, DeptHeadingNumber).Value Then
          Worksheets("Overview").Cells(RowCount + OverviewSheetLastEntryRowNumber - 3, OverviewHeadingNumber).Value = Worksheets(wkshtNumber).Cells(RowCount, DeptHeadingNumber).Value
          Else
          End If
        Next DeptHeadingNumber
      Next OverviewHeadingNumber
    Next RowCount
  Next wkshtNumber
End Sub

. It is very basic (written by a beginner!) but it gives you the basic idea. A more experienced Board member may follow up with a better code. (I hope so!- that is a great help in my learning!)

. Note:
. - You can add sheets or add extra headings in your overview Sheet and the Program will automatically take care of that.
. - As it is written you are limited to about 255 rows, columns, sheets etc. But that is a fairly simple modification to change that.


. I chopped out some of your data, limiting to 2-3 rows in each department, just to make it a bit easier to test and look at. With that in mind, I think you will see that the end result below (which you get in the Overview sheet after running the Macro) is what you wanted?


Book1
ABCDEFGH
1
2
3DepartmentDriverModelPU DateDO DateEstimated CostApprovedNotes
4DVTBDStandard SUV9.1.1410.1.14264
5DVTBDCompact07.09.201401.10.2014216
6DVTBD15 Pass Van09.09.201401.10.2014192
7OPsTBDCompact04.09.201404.10.2014264
8OPsTBDStandard SUV04.09.201404.10.2014264
9MobilityTBD#NV
10ManagementTBDCompact0
11ManagementTBDCompact
12ManagementTBD
13ManagementTBD
14ManagementTBD
15ManagementTBDCompact
16FinanceTBD#NV
17Stage Co#1Minivan8.30.149.17.14168
18Stage Co#2Minivan8.30.149.17.14168
19Stage Co#3Minivan8.30.149.17.14168
20Production- Art DecoTBD#NV
21Overseas crewTBDMinivan8.31.1410.4.14300
22Overseas crewTBDMinivan9.10.1410.2.14192
23Overseas crewTBDMinivan9.9.1410.2.14204
24
25
Overview


. I am not sure of your exact knowledge of VBA. If you need more help in the Basics of getting started and running the code then get back to me.

Alan
 
Upvote 0
That looks like exactly what I want! I put the code in, but I'm getting this error message: Compile error: Invalid outside procedure. Any thoughts?
 
Upvote 0
That looks like exactly what I want! I put the code in, but I'm getting this error message: Compile error: Invalid outside procedure. Any thoughts?


As you "put the code in" I expect you used the method of pasting tha code into a module?. That should work. I do not recognise the compile error

Try the alternative method of selecting the code I put in the sheet module in the file I sent you:

To do this

'1. Right-click the worksheet tab that you are interested in (In your case the first sheet, OverviewSheet) and choose 'View Code'. This will open the VBE (Visual basic Editor) window. (Sometimes called the <acronym title="vBulletin" style="border-width: 0px 0px 1px; border-bottom-style: dotted; border-bottom-color: rgb(0, 0, 0); cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VB</acronym> development window)
'2. Place the cursor anywhere in the actual code you want to run. (The only one there in this case!)
'3. Hit F5. The code should run!! (IMPORTANT: always save your program just before you run any program. -Because you cannot use the Excel backward thing to clear any changes made by a macro)
'4. Make sure you have enabled macros whenever you open the file or the code will not run.
 
Upvote 0
That's basically how I did it in the first place. . . still getting the error.



. Hi,

Unfortunately, I lack the computer experience to help you with such a problem at this distance. It might be a mac compatibility problem that I do not know about. I have posted about up to now about 30 codes which have all worked up until now.

. You could try the Conventional method for inserting a macro:


- Open a fresh file of yours which has a similar format to the one you sent me. Resave it, but not in the conventional excel workbook format .xlsx. Instead selsct workbook with macros .xlsm

- Copy the code from this Thread

- Press simaltaneously keys ALT and F11 to get into the Visual Basic Editor

- Insert a Module: Either right click in the project Window on your file, then >> Insert >> module ; or select the insert tab above and select module

- Simply paste in the code where the cursor is flashing

- Press keys ALT and q simaltaneously to get back into Excel

- Press keys ALT and F8. Select in the dialog window macro RennyTMW and then run (Or alternatively just double click on RennyTMW)

- Again (IMPORTANT: always save your program just before you run any program. -Because you cannot use the Excel backward thing to clear any changes made by a macro)


. I have to go off line shortly. But let me know how you get on. I will try to help you further if and when I can. Maybe somebody else will read this thread and help in the meantime

Alan
 
Upvote 0
Sorted! I was having some compatibility issues, figured it out eventually.
Thank you, works great!
 
Upvote 0
Sorted! I was having some compatibility issues, figured it out eventually.
Thank you, works great!

Hi,
. Thanks for the feedback. Glad it worked.

. One last thing. Could you elaborate a little on the problem you had: - I have not helped anyone with a mac until now. It would be useful for the future if I do so again to warn somebody of a possible problem.
. Could you also check again your version of Excel. I am still puzzled that there is a 2011 version. Again this could be something to do with mac that I do not know about.

Thanks
Alan.

P.s.
Come back if you have problems in the future with the code. It was a Beginner’s attempt! There are certainly infinite improvement that could be made. Problems could also come up in the practice when using for lots of actual data.

One thing springs to mind: As I have written the code, the last entry is checked only for the First row. So it assumes that if you have an entry at all in a row then there must also be a driver entered. How many of the other columns are filled in or empty is not important
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,050
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top