VBA Excel: Copy headers from multiple worksheets

Hello Experts,

I have a workbook with multiple worksheets.

I am looking for a procedure that convers the following requirements:

Copy and transpose the various headers from the various worksheets:
Report them in a Consolidation sheet like this :

Sheet1:

2015-10-02-14_05_03-Microsoft-Excel-.png
Sheet2:

2015-10-02-14_05_15-Microsoft-Excel-.png
Consolidation:
2015-10-02-14_05_32-Microsoft-Excel-.png



The filter should be done by Header and we can have the same header name in different worksheets

It would be great when I click in a cell of the Consolidation worksheet, to redirect  to the field related.

Thank you again for your help.
Copy_Headers.xlsm
LVL 1
LD16Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try

Sub macro()
Dim myArray
Dim DestRng As Range
For Each sh In Worksheets
    If sh.Name <> "Consolidation" Then
        Set OrigRng = sh.Range(sh.Range("A1"), sh.Cells(Cells.Columns.Count).End(xlToLeft))
        ReDim myArray(OrigRng.Count - 1, 2)
        For Idx = 0 To OrigRng.Count - 1
            myArray(Idx, 0) = OrigRng.Cells(1, Idx + 1)
            myArray(Idx, 1) = Split(OrigRng.Cells(1, Idx + 1).Address, "$")(1)
            myArray(Idx, 2) = sh.Name
        Next

        Set DestRng = Sheets("Consolidation").Range("A" & Cells.Rows.Count).End(xlUp).Offset(1)
        Set DestRng = DestRng.Resize(UBound(myArray, 1) + 1, UBound(myArray, 2) + 1)
        DestRng = myArray
    End If
Next
Sheets("Consolidation").Columns("A:C").Sort key1:=Range("A2"), _
      order1:=xlAscending, Header:=xlYes
      
For Each c In Sheets("Consolidation").Range(Range("A2"), Range("A" & Cells.Rows.Count).End(xlUp)) '=HYPERLINK("#Sheet2!B2","Budget")
    c.Formula = "=HYPERLINK(" & Chr(34) & "#" & c.Offset(, 2).Value & "!" & c.Offset(, 1) & "1" & Chr(34) & "," & Chr(34) & c.Value & Chr(34) & ")"
Next
End Sub

Open in new window

Regards
0
LD16Author Commented:
Hello,

Thank you for this code.

I will test it in a while.
0
LD16Author Commented:
I have tested and it works.

I have some remarks:
-Is there a way to add Consolidation if it doesn't exist
-Clear UsedRange of Consolidation otherwise when I change the header name of one sheet because when I relaunch the macro I have the previous headers which are kept
-Apply borders of UsedRange and deactivate gridlines for all Worksheets in Workbook

Thank you again for your help.
0
Rgonzo1971Commented:
then try

Sub macro()
Dim myArray
Dim DestRng As Range
If Not Evaluate("=ISREF(Consolidation!A1)") Then
    Set wsC = Worksheets.Add(after:=Sheets(Sheets.Count))
    wsC.Name = "Consolidation"
    wsC.Range("A1:C1") = Array("Header", "Column letter", "Sheet Name")
    wsC.Range("A1:C1").Font.Bold = True
Else
    Set wsC = Worksheets("Consolidation")
    wsC.UsedRange.Offset(1).EntireRow.Delete
End If
For Each sh In Worksheets
    sh.Activate
    ActiveWindow.DisplayGridlines = False
    If sh.Name <> "Consolidation" Then
        Set OrigRng = sh.Range(sh.Range("A1"), sh.Cells(Cells.Columns.Count).End(xlToLeft))
        ReDim myArray(OrigRng.Count - 1, 2)
        For Idx = 0 To OrigRng.Count - 1
            myArray(Idx, 0) = OrigRng.Cells(1, Idx + 1)
            myArray(Idx, 1) = Split(OrigRng.Cells(1, Idx + 1).Address, "$")(1)
            myArray(Idx, 2) = sh.Name
        Next

        Set DestRng = wsC.Range("A" & Cells.Rows.Count).End(xlUp).Offset(1)
        Set DestRng = DestRng.Resize(UBound(myArray, 1) + 1, UBound(myArray, 2) + 1)
        DestRng = myArray
    End If
Next
wsC.Activate
wsC.Columns("A:C").Sort key1:=Range("A2"), _
      order1:=xlAscending, Header:=xlYes
      
For Each c In Range(Range("A2"), Range("A" & Cells.Rows.Count).End(xlUp)) '=HYPERLINK("#Sheet2!B2","Budget")
    c.Formula = "=HYPERLINK(" & Chr(34) & "#" & c.Offset(, 2).Value & "!" & c.Offset(, 1) & "1" & Chr(34) & "," & Chr(34) & c.Value & Chr(34) & ")"
Next
With wsC.UsedRange
    With .Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .Weight = xlThin
    End With
    With .Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlThin
    End With
    With .Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThin
    End With
    With .Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .Weight = xlThin
    End With
    With .Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .Weight = xlThin
    End With
    With .Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .Weight = xlThin
    End With
    .EntireColumn.AutoFit
End With
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Author Commented:
I tested and it works, thank you again for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.