First approach - using Application.Match
(faster one):
Dim colNum
With ws2
colNum = Application.Match("Transaction ID", .Range("1:1"), 0)
If IsError(colNum) Then
MsgBox "Column with header 'Transaction ID' not found"
Exit Sub
End If
CurrInvoiceNum = .Cells(a, colNum).Value
End With
Second approach - using .Find
:
Dim rng As Range
With ws2
Set rng = .Range("1:1").Find(What:="Transaction ID", LookAt:=xlWhole)
If rng Is Nothing Then
MsgBox "Column with header 'Transaction ID' not found"
Exit Sub
End If
CurrInvoiceNum = .Cells(a, rng.Column).Value
End With
both apporaches assumes that your headers are in first row: .Range("1:1")