Используйте Application.WorksheetFunction.Match
, чтобы найти номер столбца имени, которое вы ищете. Затем выполните проверки для столбцов.
Вот пример:
Option Explicit
Public Sub ValidateData()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet")
Dim ColumnNames() As Variant
ColumnNames = Array("Filename", "FileNumber", "Author") 'add all Column header that you want to check
Dim Headers As Variant 'read all headers into an array
Headers = ws.Range("A1", ws.Cells(1, ws.Columns.Count).End(xlToLeft)).Value
Dim HeaderColumn As Long 'this is the column number where the header was found
Dim ColName As Variant
For Each ColName In ColumnNames 'loop through your list of names
HeaderColumn = 0 'initialize
On Error Resume Next 'next line throws error if it does not match
HeaderColumn = Application.WorksheetFunction.Match(ColName, Headers, 0)
On Error GoTo 0 're-activate error reporting
If HeaderColumn <> 0 Then
'header name was found
MsgBox ColName & " Column found"
'perform different checks on each column
Select Case ColName
Case "FileNumber"
CheckFileNumberColumn ws.Range(ws.Cells(2, HeaderColumn), ws.Cells(ws.Rows.Count, HeaderColumn).End(xlUp))
'Case "Author" 'add other cases as needed
'CheckAuthorColumn ws.Range(ws.Cells(2, HeaderColumn), ws.Cells(ws.Rows.Count, HeaderColumn).End(xlUp))
End Select
Else
'header name was not found
MsgBox ColName & " Column not found"
End If
Next ColName
End Sub
'this is the procedure to check the FileNumber column
Private Sub CheckFileNumberColumn(DataToValidate As Range)
Dim iRow As Long
For iRow = 1 To DataToValidate.Rows.Count
If DataToValidate.Cells(iRow, 1).Value <> 101 Then
DataToValidate.Cells(iRow, 1).Interior.Color = RGB(255, 0, 0)
End If
Next iRow
End Sub