Data consolidation between multiple sheets into one

Ngordyn

New Member
Joined
Nov 11, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have an interesting challenge where I am just returning to the world of Excel after a break of nearly 20 years and I'm jumping straight into VBA, my challenge is all based around how to simplify a process of consolidating over 70 worksheets (in the same workbook) of information into one sheet (keeping it long and narrow)

Since the end user has control over their individual sheet there are variances in the headers, one may call a header Compliance, others Compliant etc, as well as the order of headers.

Instantly that is a large scope of works and I've done some research and my original macro was very simple but effective and then I discovered the flaw in my logic

VBA Code:
Sheets("Datapage1").Range("b2:b2").Copy Destination:=Sheets("Sheet1").Range("a2:a500")
Sheets("Datapage1").Range("A9:d500").Copy Destination:=Sheets("Sheet1").Range("b1")

I had the code repeat for all 70 pages, while rough it did the job. However the flaw which unravelled my logic and as a result the request for assistance.

The columns in the Datapages are not in a set order. One of the critical elements may be in column C in one page, and E on another.

Is there a way to simplify my macro to enable:
  • Copy column if the header contains "compli" (non case sensitive) within a range of cells C9:F11 for example into "Sheet1"
    • I know how annoying it is that the headers are not actually headers
  • Read all worksheets as opposed to writing a line (or several) for each worksheet
For additional context the top line in the macro is to copy the reference name for each page, thankfully that one is standardised

Appreciate any help that can be offered.

Thanks in advance
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

severynm

Active Member
Joined
Jan 8, 2021
Messages
335
Office Version
  1. 365
Platform
  1. Windows
Copy column if the header contains "compli" (non case sensitive) within a range of cells C9:F11 for example into "Sheet1"
  • I know how annoying it is that the headers are not actually headers
If you have a defined range that contains your headers (not that they necessarily are in the same order), you can use .Find to return the cell in the range where a match is found. E.g.
VBA Code:
Public Sub main()
    ColumnOfHeader "compli"
End Sub

Public Function ColumnOfHeader(stringToMatch As String)
    Dim headerRange As Range
    Dim result As Range
    
    Set headerRange = Sheet3.Range("A1:F1")
    Set result = headerRange.Find(stringToMatch)
    ColumnOfHeader = result.Column
End Function
You could pass all the information you needed into the function (the headerRange, sheet, etc.) so you could use it in a loop. This particular function returns the column number of the match, but that can be customized to how you need.

Read all worksheets as opposed to writing a line (or several) for each worksheet
It's pretty straightforward to loop over all the worksheets in the current workbook.
VBA Code:
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        Debug.Print ws.name
    Next

So, inside your loop for each worksheet you can find the column of the desired string and then perform the necessary operations. Let me know if you have any further questions....
 

Forum statistics

Threads
1,176,025
Messages
5,900,962
Members
434,861
Latest member
Coffee_in_a_jiffy

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
Top