Question - How to search through the first row for a column header, copy the data, paste into another sheet and also paste the worksheet name the data

database_coder

New Member
Joined
Feb 6, 2014
Messages
35
Hi All,

Thank you very much for everyone who has posted to this website. I have been using it for sometime now, and finally decided to make an account because I have a question that I can not seem to answer.

I have a Workbook consisting of 30 Worksheets. In each worksheet, my headers go from A1:Z1, but the number of observations are not consistent, meaning every worksheet has a different set of data. On each worksheet, I am only worried about the data in the Columns (Number, and Person), but the order is different throughout each worksheet.

What my goal here is - copy all the data for Number, and Person from each worksheet, and paste into a SUMMARY worksheet. Also, on each worksheet data is copied I need to save the name of the worksheet next to the data in the SUMMARY worksheet.

I know there is a way to copy the data all into an array, and paste to SUMMARY at the end. But I can not find a proper way to do it.

My code is a mess, which I have pasted below. Is there anyway we can do this? Any help will be most appreciated.


Code:
Sub B_IDs()
    Dim ws As Worksheet
    Dim wb As Workbook
    Dim sum As Worksheet
    Dim Leng As Long
    Dim x As Long
        Set sum = Sheets("Summary")
    
    sum.Cells.Clear
    
    Application.ScreenUpdating = False
    
    
    For Each ws In ActiveWorkbook.Worksheets
    Leng = ws.Range("A2").End(xlDown).Row + 1
        If ws.Name <> "Summary" Then
            If ws.Cells(1, 1).Value = "ID" Then
                ws.Range("A2:A100").Copy
                Worksheets("Summary").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValues)
            End If
        End If
  
'        x = 1
'        For x = x To 20
'            If ws.Cells(1, x).Value = "PERSON" Then
'
'
''                Selection.Copy.Column
''                Worksheets("Summary").Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValues)
'            End If
'        Next x
    Next ws
    
   
        
End Sub

Sub WorksheetLoop()
    Dim wsCount As Integer
    Dim I As Integer
    Dim ws As Worksheet
    Dim RowCount As Long
    Dim x As Long
        
    wsCount = ActiveWorkbook.Worksheets.Count
    'RowCount = ActiveWorksheet.Count
    
    For Each ws In ActiveWorkbook.Worksheets
    
        For x = 1 To 100
            If Cells(x, "A") = "ID" And Cells(x, "F") = "PERSON" Then
                Cells(x, "A").Resize(, 3).Copy
                Worksheets("Summary").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
                Cells(x
            
            
            
            End If
            
        Next x
        
    Next ws
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
database_coder,

Welcome to the MrExcel forum.

What version of Excel and Windows are you using?


I have a Workbook consisting of 30 Worksheets. In each worksheet, my headers go from A1:Z1, but the number of observations are not consistent, meaning every worksheet has a different set of data. On each worksheet, I am only worried about the data in the Columns (Number, and Person), but the order is different throughout each worksheet.

1. Can we have a screenshot of two of the raw data worksheets?

2. Can we have a screenshot of the SUMMARY worksheet, before, and after the data from the two raw data worksheets have been manually formatted by you for the results you are looking for?


To post your data, you can download and install one of the following two programs:
Excel Jeanie
MrExcel HTML Maker

Or, when using Internet Explorer, just put borders around your data in Excel and copy those cells into your post.
See reply #2 the BLUE text in the following link:
http://www.mrexcel.com/forum/about-board/444901-how-create-table-like-aladin.html#post2198045


If you are not able to give us screenshots:
You can upload your workbook to Box Net,
sensitive data changed
mark the workbook for sharing
and provide us with a link to your workbook.
 
Upvote 0
Hi Hiker95,

I am currently using Windows 7 with Excel 2010 at work. I have just created a sample workbook at home, using my Mac (the link is located on the bottom of this post).

Now, my goal here is to create a Macro that copies the column labeled Number (in each worksheet it is column A) and Person (this column is not the same in every worksheet throughout the workbook), pastes it into the Summary Worksheet at the end of the Workbook, placing Number in Column A, Person in Column B, and the Name of the spreadsheet from which the data was copied from in Column C. The Summary spreadsheet is not populated, and is currently empty. The reason why I want to create a Macro is because the Workbook I am using is linked to Microsoft Access, and each Worksheet that needs to be copied is a table from the Microsoft Access database. Currently there are 30 worksheets, but going further this number is subject to increase.

My problem is when Person is in different columns. I will continue to try and accomplish this task, but any help would be most appreciated.

The link to my sample workbook is : https://app.box.com/s/2k655mxi4f4ong4ivigf

Thanks
database_coder
 
Last edited:
Upvote 0
I could probably use a Do While Loop, but I still do not know how to search the First Row to find 'Person', and then put the name of the worksheet next to the pasted data. Copying and Pasting the data from one worksheet to another is very simple. Or, one could use a For statement and copy the data into an array, paste the data in Summary, but I still question how I would be able to put the worksheet name next to the pasted data.

How could we implement:

Code:
 Activesheet.[A1] = Activesheet.Name

into this Macro?
 
Last edited:
Upvote 0
database_coder,

Please post another workbook, with worksheet Summary manually formatted by you for the results you are looking for.
 
Upvote 0
NumberPersonWorksheet Name
10MikeWorksheetName1Example
25DonWorksheetName2Example
3JordanWorksheetName3Example
13SarshWorksheetName4Example
16TomWorksheetName5Example
2ElizaWorksheetName6Example
1FrankWorksheetName7Example
8TimWorksheetName8Example
40JessicaWorksheetName9Example

<tbody>
</tbody>
 
Upvote 0
Above is how I would like the summary worksheet to be formatted, Number in Column A, Person in Column B, and Worksheet Name in Column C.
 
Upvote 0
Although there may be several Observations for each worksheet. I do not think this is possible but if anyone has any insight please share.
 
Upvote 0
database_coder,

Thanks for the workbook and the screenshot.

I assume that in each raw data worksheet, the column title Name exists in row 1, and, Name does not contain any leading or trailing space characters.

In the following screenshots of the raw data worksheets I am only displaying the Number, and, the Name columns.


Excel 2007
AD
1NumberName
22Pete
33Tim
46Tom
57Frank
68Ed
710Sarah
815Jessica
923Angie
1019Fred
11132Mike
12135Fred
13137Mike
14139Doug
15142Pete
16144Tim
17147Tom
18149Frank
19152Ed
20154Sarah
21157Jessica
22159Angie
23162Fred
24164Mike
25
Breaks



Excel 2007
AC
1NumberName
238Fred
340Mike
443Doug
545Pete
648Tim
750Tom
853Frank
955Ed
1058Fred
1160Mike
1262Doug
1365Pete
1467Tim
1570Tom
1672Frank
1775Ed
18
OctBreaks



Excel 2007
AH
1NumberName
2100Ed
3102Fred
4105Mike
5107Fred
6110Mike
7112Doug
8115Pete
9117Tim
10120Tom
11122Frank
12125Ed
13127Fred
14130Mike
15
NovBreaks



Excel 2007
AI
1NumberName
22Fred
33Mike
46Doug
57Pete
68Tim
710Tom
815Frank
923Ed
1019Fred
1123Mike
1225Doug
1328Pete
1430Tim
1533Tom
1635Frank
1777Ed
1880Fred
1982Mike
2085Doug
2187Pete
2290Tim
2392Tom
2495Frank
2597Ed
26
DecBreaks


Before the macro worksheet Summary is empty:


Excel 2007
ABC
1NumberPersonWorksheet Name
2
3
4
Summary


After the macro:


Excel 2007
ABC
1NumberPersonWorksheet Name
22PeteBreaks
33TimBreaks
46TomBreaks
57FrankBreaks
68EdBreaks
710SarahBreaks
815JessicaBreaks
923AngieBreaks
1019FredBreaks
11132MikeBreaks
12135FredBreaks
13137MikeBreaks
14139DougBreaks
15142PeteBreaks
16144TimBreaks
17147TomBreaks
18149FrankBreaks
19152EdBreaks
20154SarahBreaks
21157JessicaBreaks
22159AngieBreaks
23162FredBreaks
24164MikeBreaks
2538FredOctBreaks
2640MikeOctBreaks
2743DougOctBreaks
2845PeteOctBreaks
2948TimOctBreaks
3050TomOctBreaks
3153FrankOctBreaks
3255EdOctBreaks
3358FredOctBreaks
3460MikeOctBreaks
3562DougOctBreaks
3665PeteOctBreaks
3767TimOctBreaks
3870TomOctBreaks
3972FrankOctBreaks
4075EdOctBreaks
41100EdNovBreaks
42102FredNovBreaks
43105MikeNovBreaks
44107FredNovBreaks
45110MikeNovBreaks
46112DougNovBreaks
47115PeteNovBreaks
48117TimNovBreaks
49120TomNovBreaks
50122FrankNovBreaks
51125EdNovBreaks
52127FredNovBreaks
53130MikeNovBreaks
542FredDecBreaks
553MikeDecBreaks
566DougDecBreaks
577PeteDecBreaks
588TimDecBreaks
5910TomDecBreaks
6015FrankDecBreaks
6123EdDecBreaks
6219FredDecBreaks
6323MikeDecBreaks
6425DougDecBreaks
6528PeteDecBreaks
6630TimDecBreaks
6733TomDecBreaks
6835FrankDecBreaks
6977EdDecBreaks
7080FredDecBreaks
7182MikeDecBreaks
7285DougDecBreaks
7387PeteDecBreaks
7490TimDecBreaks
7592TomDecBreaks
7695FrankDecBreaks
7797EdDecBreaks
78
Summary


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Option Explicit
Sub GetNumberPersonSheetName()
' hiker95, 02/07/2014, ME755957
Dim ws As Worksheet
Dim lr As Long, fprng As Range, nr As Long
Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Worksheets
  If ws.Name <> "Summary" Then
    With ws
      nr = Sheets("Summary").Range("A" & Rows.Count).End(xlUp).Offset(1).Row
      lr = .Cells(Rows.Count, 1).End(xlUp).Row
      Sheets("Summary").Range("A" & nr).Resize(lr - 1).Value = .Range(.Cells(2, 1), .Cells(lr, 1)).Value
      Sheets("Summary").Range("C" & nr).Resize(lr - 1).Value = ws.Name
      Set fprng = .Rows(1).Find("Name", LookAt:=xlWhole)
      If Not fprng Is Nothing Then
        Sheets("Summary").Range("B" & nr).Resize(lr - 1).Value = .Range(.Cells(2, fprng.Column), .Cells(lr, fprng.Column)).Value
      End If
    End With
  End If
Next ws
With Sheets("Summary")
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  .Range("A2:A" & lr).NumberFormat = "0"
  .Columns.AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the GetNumberPersonSheetName macro.
 
Upvote 0
Hiker95,

Wow, I am astonished by how clean the code is and how successful it runs. Very good work.

Would it be possible to extend this one step further by stating Number to change columns between each worksheet. How would you go about changing this?
 
Upvote 0

Forum statistics

Threads
1,216,577
Messages
6,131,511
Members
449,653
Latest member
andz

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