Function to Validate Headers
We've all been there, you've been downloading the same csv for the last 6 months every single damn day and blindly loading it into your model with every belief it's not going to change and bang, a workbook full of errors or your VBA falls over.
Save yourself the misery and make sure that you validate that your field headers haven't changed. Here's how:
Function ValidateHeaders(ByRef varRequiredHeaders As Variant, ByRef rngFoundHeaders As Range, Optional ByRef blnCheckOrder = False) As Boolean
Dim lngHeader As Long
Dim varMatches As Variant
varMatches = Application.Match(varRequiredHeaders, rngFoundHeaders, 0)
ValidateHeaders = Application.CountA(varRequiredHeaders) = _
Application.Count(varMatches)
If blnCheckOrder And ValidateHeaders Then
For lngHeader = LBound(varMatches) To UBound(varMatches)
If varMatches(lngHeader) <> lngHeader Then
ValidateHeaders = False
Exit Function
End If
Next lngHeader
End If
End Function
This function will work within the Excel Grid or as part of your VBA and will return a Boolean result (TRUE or FALSE) to report it's success or failure.
Simply pass an array or a range of cells into the first two parameters and an optional TRUE in the blnCheckOrder parameter if field order is important and boom, immediate validation.
Business System Analyst,BSA (JDE-E1/World), MBA Logistics
8 年It took a bit understand the logic...you mentioned you would explain more in detail how the match looks for the last cell...when you send the article I will sure to read it.
Data Analyst COG P&C at Chubb
8 年Ruben, good man. Shout if you have any issues, I think it's fairly straight forward.
Business System Analyst,BSA (JDE-E1/World), MBA Logistics
8 年Looks interesting I will guve it a try
Data Analyst COG P&C at Chubb
8 年MATCH will also retrieve the index number of any matches so you can check for movement, I'd use that.
Data Analyst COG P&C at Chubb
8 年You can use MATCH or COUNTIF to check for matches. Anywhere you get an error or 0 there is no match.