I want to Exit Sub
if my Function
procedure (Colvalidation
) doesn't find a column from the array below.
I know it's impossible to End Sub
within the Function
, and I have no idea how to handle it. Could you help me with that?
Sub test()
(...)
'Check if "Earnings", "Deductions", "Employer" headers exist
vals = Array("Earnings", "Deductions", "Employer")
vals = Array("Earnings", "Deductions", "Employer Paid Benefits and Taxes")
Set myRng = Worksheets(PayrollWS).Range(Worksheets(PayrollWS).Cells(MyR, 1), Worksheets(PayrollWS).Cells(MyR, 25))
For Each val In vals
Colvalidation myRng, val
If ColFound = False Then 'error = "Variable not defined"
Exit Sub
Next
(...)
End Sub
Function Colvalidation(Rng As Range, value As Variant)
Dim rngX As Range, ColFound as Boolean
Set rngX = Rng.Find(what:=value, lookat:=xlPart, LookIn:=xlValues)
If rngX Is Nothing Then
MsgBox value & " - Column Not Found" 'if column not found, Exit Sub after MsgBox
ColFound = False
Exit Function
End If
End Function