VBA: Check Column Headers

SteveOranjin

Board Regular
Joined
Dec 18, 2017
Messages
170
I am starting to develop a tool that will enforce good practices in data loading. I'm new to VBA, so I probably shouldn't be doing this. Having said that, I'm getting an error on this code.

Rich (BB code):
Sub Check()    Application.EnableEvents = False
    
    Dim ColumnHeaderArr(0 To 2) As String
    
    ColumnHeaderArr(0) = "SKU"
    ColumnHeaderArr(1) = "BrandName"
    ColumnHeaderArr(2) = "BrandCode"
    
    If VerifyHeaders(ColumnHeaderArr) = True Then
        Msg = "All headers are present"
    Else
        Msg = "You are missing headers"
        
    End If
    
    Application.EnableEvents = True
    
End Sub

The error i get is, "Compile Error: Sub or Function Not Defined" and it is highlighting the section of the code that I have bolded and underlined.

Can someone please help me get this to work, and to work in such a way that it doesn't wig out if there are other headers/more columns than just three? Also, if there are no columns I'd like it to work as well.

Steve
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Is VerifyHeaders some sort of function I don't know about?
How about instead:

Code:
sub check
on error resume next
chk1 = activesheet.Cells.Find(What:="[COLOR=#ff0000]SKU[/COLOR]", After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column
chk2 = activesheet.Cells.Find(What:="[COLOR=#ff0000]BrandName[/COLOR]", After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column
chk3 = activesheet.Cells.Find(What:="[COLOR=#ff0000]BrandCode[/COLOR]", After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column


resume next
if chk1 = 0 then
errmsg = "No SKU header"
endif
if chk2 = 0 then
errmsg = errmsg & vbcr & "No BrandName header"
endif
if chk3 = 0 then
errmsg = errmsg & vbcr & "No BrandCode header"
endif
if errmsg<>"" then
msgbox errsmg, vbcritical, "ALERT"
exit sub
endif
end sub
 
Upvote 0
It was. I took care of it and got it fixed up.

Thanks

Is VerifyHeaders some sort of function I don't know about?
How about instead:

Code:
sub check
on error resume next
chk1 = activesheet.Cells.Find(What:="[COLOR=#ff0000]SKU[/COLOR]", After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column
chk2 = activesheet.Cells.Find(What:="[COLOR=#ff0000]BrandName[/COLOR]", After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column
chk3 = activesheet.Cells.Find(What:="[COLOR=#ff0000]BrandCode[/COLOR]", After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Column


resume next
if chk1 = 0 then
errmsg = "No SKU header"
endif
if chk2 = 0 then
errmsg = errmsg & vbcr & "No BrandName header"
endif
if chk3 = 0 then
errmsg = errmsg & vbcr & "No BrandCode header"
endif
if errmsg<>"" then
msgbox errsmg, vbcritical, "ALERT"
exit sub
endif
end sub
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,753
Members
449,094
Latest member
dsharae57

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