Hi Team,
I want to check whether all headers are present in Input sheet.
I am using below code, is there any other way. Thanks in advance!
My attempted code
I want to check whether all headers are present in Input sheet.
I am using below code, is there any other way. Thanks in advance!
My attempted code
VBA Code:
Sub Header_Validation()
Dim sh As Worksheet
Set sh = ThisWorkbook.Worksheets(1)
Dim headers As String
headers = "Sales,Total,Region,SalesPerson"
If Header_Check(sh, headers) = False Then
MsgBox "Header not found" & Chr(10) & headers, 16, "Header Missing in " & sh.Name
End If
End Sub
Function Header_Check(ByVal sh As Worksheet, headers As String, Optional hrow As Long = 1) As Boolean
Dim Header_Array() As String
Dim i As Integer
Dim cnt As Integer
Header_Array = Split(headers, ",")
headers = ""
cnt = 0
For i = 0 To UBound(Header_Array)
cnt = 0
On Error Resume Next
cnt = Application.WorksheetFunction.Match(Header_Array(i), sh.Rows(hrow), 0)
On Error GoTo 0
If cnt = 0 Then
If headers = "" Then
headers = Header_Array(i)
Else
headers = Header_Array(i) & "," & Header_Array(i)
End If
End If
Next i
Header_Check = CBool(Len(headers) = 0)
End Function