Function to Validate Headers

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.

Ruben Villanueva

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.

Michael Blackman

Data Analyst COG P&C at Chubb

8 年

Ruben, good man. Shout if you have any issues, I think it's fairly straight forward.

回复
Ruben Villanueva

Business System Analyst,BSA (JDE-E1/World), MBA Logistics

8 年

Looks interesting I will guve it a try

回复
Michael Blackman

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.

回复
Michael Blackman

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.

回复

要查看或添加评论,请登录

Michael Blackman的更多文章

社区洞察

其他会员也浏览了