Extract columns based on Column name

Miguelluis

New Member
Joined
Jan 29, 2013
Messages
45
Hi,

I've got a workbook with over 150 sheets all in different layouts. I need to extract two columns from all of the different sheets into one, the two columns are titled 'ID Client' and 'ID Holder' and unfortunately some rows are blank and the columns are not in the same position for all the sheets and contain numbers and text.

Is there a way to extract all this data into one sheet and have it copied down, similar to the append option. They can be extracted to a new sheet in the same workbook.

Many thanks
Miguel
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
How about
Code:
Sub CopyCols()
   Dim Ws As Worksheet
   Dim Sws As Worksheet
   Dim Fnd1 As Range
   Dim Fnd2 As Range
   
   Sheets.Add(Sheets(1)).Name = "Summary"
   Set Sws = Sheets("Summary")
   For Each Ws In Worksheets
      If Not Ws.Name = "Summary" Then
         Set Fnd1 = Ws.Range("1:1").Find("ID Client", , , xlWhole, , , False, , False)
         Set Fnd2 = Ws.Range("1:1").Find("ID Holder", , , xlWhole, , , False, , False)
         Intersect(Ws.UsedRange, Fnd1.EntireColumn).Copy Sws.Range("A" & Rows.Count).End(xlUp).Offset(1)
         Intersect(Ws.UsedRange, Fnd2.EntireColumn).Copy Sws.Range("B" & Rows.Count).End(xlUp).Offset(1)
      End If
   Next Ws
End Sub
 
Upvote 0
I think it's nearly there, however, I get a runtime error 91 on this line:

Intersect(Ws.UsedRange, Fnd1.EntireColumn).Copy Sws.Range("A" & Rows.Count).End(xlUp).Offset(1)
 
Upvote 0
Are your headers in row 1?
It sounds like the code can't find ID Client
 
Upvote 0
When I run the Macro it creates the Summary sheet but copies the column header into row 2. All existing headers are on row 1.
 
Upvote 0
OK, how about
Code:
Sub CopyCols()
   Dim Ws As Worksheet
   Dim Sws As Worksheet
   Dim Fnd1 As Range
   Dim Fnd2 As Range
   
   Sheets.Add(Sheets(1)).Name = "Summary"
   Set Sws = Sheets("Summary")
   Sws.Range("A1:B1").Value = Array("ID Client", "ID Holder")
   For Each Ws In Worksheets
      If Not Ws.Name = "Summary" Then
         Set Fnd1 = Ws.Range("1:1").Find("ID Client", , , xlWhole, , , False, , False)
         Set Fnd2 = Ws.Range("1:1").Find("ID Holder", , , xlWhole, , , False, , False)
         Intersect(Ws.UsedRange.Offset(1), Fnd1.EntireColumn).Copy Sws.Range("A" & Rows.Count).End(xlUp).Offset(1)
         Intersect(Ws.UsedRange.Offset(1), Fnd2.EntireColumn).Copy Sws.Range("B" & Rows.Count).End(xlUp).Offset(1)
      End If
   Next Ws
End Sub
 
Upvote 0
It now puts the header in row 1, however, it stops merging/extracting after 5 or 6 sheets and gives me the same error.
 
Upvote 0
When you get the error, check which sheet the code is looking at & make sure you have both headers in row 1 of that sheet.
 
Upvote 0
Hi,

I found that it stops when it gets to a sheet that does not contain the header. I looked at this and some sheets do not contain the headers, is it possible to add something where if the header is not found it moves to the next sheet?

Thanks
Miguel
 
Upvote 0
Modify the last 2 lines like
Code:
         [COLOR=#ff0000]If Not Fnd1 Is Nothing Then [/COLOR]Intersect(Ws.UsedRange.Offset(1), Fnd1.EntireColumn).Copy Sws.Range("A" & Rows.Count).End(xlUp).Offset(1)
        [COLOR=#ff0000] If Not Fnd2 Is Nothing Then [/COLOR]Intersect(Ws.UsedRange.Offset(1), Fnd2.EntireColumn).Copy Sws.Range("B" & Rows.Count).End(xlUp).Offset(1)
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,668
Members
449,463
Latest member
Jojomen56

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