Function FindLastColumnInRange(rng As Range) As Long
'Description: finds the last column in a range
'Inputs: Range
'Outputs: column number (long); zero if error occured
' check range
If rng Is Nothing Then Exit Function
On Error Resume Next
FindLastColumnInRange = Application.WorksheetFunction.Max( _
rng.Find(What:="*", _
After:=rng.Cells(1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column, _
rng.Find(What:="*", _
After:=rng.Cells(1), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column)
On Error GoTo 0
End Function