How far have you got regarding opening the workbooks you want to check?
once you have got each one to open as needed all you need to do is get the 1st row of each one and compare it (assuming the first row contains the headers).
Simple array would work for this so the first thing to do is get the order of the headers your going to compare against, if this was in a sheet (template sheet with the headers in there as needed) you can do
Private GoodHeaders as variant
Private Sub GetGoodHeaders()
Dim Rng as Range
Set Rng ThisWorkbook.Sheets("Template").UsedRange 'Get the entire block of data
Set Rng = Rng.Rows(1) 'From the block just get the first row, the headers
GoodHeaders = Rng.Value 'put the headers into an array, (#,#) 2 dimensional is what .Value gives you if you have more than 1 cell in the range, lets assume you do
End Sub
ok so now its just a matter of opening a workbook (just let me know if you need that part) and then looking through the sheets
lets assume you just want to check sheet1, and the header row is always the first row of the block of data
Private Function CheckWorkbookHeaders(WorkSheetToCheck as WorkSheet) as variant
Dim Results() as boolean 'Keeping it simple we will just say whether we find good matches against the GoodHeaders and ignore results for anything else, for example if more columns are in the other worksheet we will just ignore them here.
Dim i as integer
Dim Rng as Range
Dim HeadersToCheck as variant
Dim HeaderCount as Integer
Redim Results(lbound(GoodHeaders,2) to UBound(GoodHeaders,2) '1D Array to list the results, using the columns of the good header array to set the size
Set Rng = WorkSheetToCheck.UsedRange
Set Rng = Rng.Rows(1)
HeadersToCheck = Rng.Value
If IsArray(HeaderCount) then 'Remember if the range is only 1 cell this wont be an array it will just be a value
HeaderCount = UBound(HeadersToCheck,2)
Else
HeaderCount = 1
End If
For i = LBound(GoodHeaders,2) to UBound(GoodHeaders,2) 'Loop the good headers
Select Case True
Case HeaderCount = 1 AND i = 1 'If HeaderCount = 1 then its not an array and we need slightly different code too check
results(i) = (HeadersToCheck = GoodHeaders(1,i))
Case HeaderCount < i AND i > 1 'If we ran out of columns to check on the file we are checking then its not a match obviously
Results(i) = false
Case Else 'the rest of cases are just normal checks, if true or false limited to the number of columns in the GoodHeaders array, Ignoring extra columns in the sheet we are checking
results(i) = (HeadersToCheck(1,i) = GoodHeaders(1,i))
End Select
Next i
CheckWorkbookHeaders = Results 'return the results as a 1D array of true and false
End Function
ok so just a little something to look at, there are many ways to do it i just did it this way. just have a look through and if it makes sense just give it a try.
Bookmarks