Sub MyMacro()
Dim lr As Long, r As Long
Dim uCol As Long, qCol As Long
Application.ScreenUpdating = False
' Find last used row on sheet
lr = Range("A1").SpecialCells(xlLastCell).Row
On Error GoTo err_chk
' Find location of Unit Price
uCol = Rows("2:2").Find(What:="Unit Price", After:=Range("A2"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Column
' Find location of Quantity
qCol = Rows("2:2").Find(What:="Quantity", After:=Range("A2"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Column
On Error GoTo 0
' Loop through all rows starting in row 3
For r = 3 To lr
' If there are numbers in each column, calculate product and put in column K
If (Cells(r, uCol) <> 0) And (Cells(r, qCol) <> 0) Then
Cells(r, "K") = Cells(r, uCol) * Cells(r, qCol)
End If
Next r
Application.ScreenUpdating = True
MsgBox "Process complete", vbOKOnly
Exit Sub
'error handling
err_chk:
If Err.Number = 91 Then
MsgBox "Cannot find Unit Price and Quantity headers in row 2", vbOKOnly, "ERROR!"
Else
MsgBox Err.Number & ": " & Err.Description
End If
Application.ScreenUpdating = True
End Sub