Verify Column Headers have not been Altered

MARKG

Board Regular
Joined
Jul 3, 2002
Messages
56
Hello; I currently have a spreadsheet that contains 84 column headers (A1:CF1). What I want to do is ensure the source file has not been altered (columns added, deleted, moved) before I start scraping and wrangling data.

My current approach I know is a very long way around the problem where I'd end up typing 84 separate If statements making sure each column header is named accordingly (TransactionNo, FileNo, Importer ...etc) then based on the outcome, I'd concatenate all the results into a MsgBox with LineFeed separating each. I currently started this for my test code of 4 columns and it works. Example:
If Range("A1") <> "TransactionNo" Then
A = "Column A: """ & Range("A1") & """ does not match: ""TransactionNo"""
HeaderError = True
Else
A = "Column A: OK"
End If
(repeat 84 times for each column and concatenate all into a msgbox)

Given the scope of the project (I have to do this over 4 other files about the same size) that is a lot of If Statements. I was thinking a better approach is to have a verification file that contains a single row of the Header Names, and then compare that against the Source File Header Names and return a msgbox that indicates what Columns do not match. Can I somehow Read Source1 FirstCell, Read Source2 FirstCell, make sure they match, then Read Source1 SecondCell, Read Source2 SecondCell, etc. 84 times?

Any suggestions on how I can achieve my goal?
Thank-you. Mark
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
FYI - Figured out a solution...posting in case someone else needs help. Modified some code I found after googled FileSystemObject . Just activate your Microsoft Scripting Runtime (in references). I'll transpose my Source Headers to a txt file and validate against a Master Header file.

Code:
Sub Read_text_File()

Dim oFSO As New FileSystemObject
Dim oFSmaster
Dim oFSsource
Dim endMaster As Boolean

Set oFSmaster = oFSO.OpenTextFile("C:\Users\yyz-User1\Documents\Account Management\MP_Header_Validation.txt")
Set oFSsource = oFSO.OpenTextFile("C:\Users\yyz-User1\Documents\Account Management\MP_Header_Source.txt")

x = 1
Do Until oFSsource.AtEndOfStream
    sTextSource = oFSsource.ReadLine
    
    If oFSmaster.AtEndOfStream Then
       endMaster = True
    Else
       sTextMaster = oFSmaster.ReadLine
    End If
    
    If (sTextSource <> sTextMaster) And endMaster = True Then
        MsgBox ("Source file has more columns than Master starting at column " & x & vbNewLine & vbNewLine & "Should be blank but it is " & sTextSource)
    ElseIf sTextSource <> sTextMaster Then
        MsgBox ("ERROR!  Column " & x & " header should be " & vbNewLine & sTextMaster & "{from Master Header. Not...}" & vbNewLine & sTextSource & "{from Source Record}")
    End If
    x = x + 1
Loop
MsgBox ("Header Validation Completed. Cycled " & x & " columns")

End Sub

Cheers!
Mark
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,676
Members
452,937
Latest member
Bhg1984

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