Iteration: matching headers with worksheet names

Tyrolpath

New Member
Joined
Jan 27, 2017
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have a fairly complex macro (for me) to program.

The Workbook contains the following:
  • The first Worksheet called “Export” contains 53 columns with headers in row 1 with names such as “Apples”, “Bananas”, “Cherry”, etc.
  • The Workbook also contains 53 hidden Worksheets named like the headers in the “Export” Worksheet, i.e. “Apples”, “Bananas”, “Cherry”, etc.
The idea of the macro would be to:
  • loop through the columns in Worksheet “Export”
  • check if there is data in the column, e.g. in column “Apple” below the header in row 2
  • copy all the data in this column
  • loop though the hidden worksheets
  • find and unhide the “Apple” worksheet
  • paste the data in cell A1 in Worksheet “Apple”
  • continue the loop and do the same for all the 53 columns in Worksheet “Export”
To go around the fact that I don’t know how to program this macro, I use a formula to link the content of the cells from Worksheet “Export” in the cells in Worksheets “Apple”, “Bananas”, etc. And I run a simple macro to unhide the worksheets if data is present in row 2 in the Worksheet “Export”. This is sub-optimal, especially if columns are added or removed in the Worksheet “Export”.

--> I would really appreciate if someone could give me hint on how to loop through the headers and more importantly on how to match the names of the headers and the name of the worksheets.

Thank you in advance for the help!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Sheets can remain hidden, you don't need to show them.
Try this:

VBA Code:
Sub matching_headers_with_sheets()
  Dim shE As Worksheet
  Dim c As Range
  Dim lr As Long
  
  Set shE = Sheets("Export")
  For Each c In shE.Range("A1", shE.Cells(1, Columns.Count).End(1))
    If Evaluate("ISREF('" & c.Value & "'!A1)") Then
      lr = shE.Cells(Rows.Count, c.Column).End(3).Row
      If lr > 1 Then
        Sheets(c.Value).Range("A1").Resize(lr - 1).Value = shE.Cells(2, c.Column).Resize(lr - 1).Value
      End If
    End If
  Next
End Sub
 
Upvote 0
Dear Dante,

thanks a lot for taking the time to look into this.

The ISREF did the trick! thank you!
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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