I have a function to find last row in a specific column in excel.
Below is the code
Private Function FindLastRowInColumn(ByVal XlWorkSheet As Excel.Worksheet, ByVal ColumnName As String) As Long
Dim LastRow As Long
With XlWorkSheet
LastRow = .Cells(.Rows.Count, ColumnName).End(Excel.XlDirection.xlUp).Row
End With
Return LastRow
End Function
I am getting error on the line
LastRow = .Cells(.Rows.Count, ColumnName).End(Excel.XlDirection.xlUp).Row
Error: Option Strict On disallows late binding.
How can I fix this error without turning off Option Strict
?
I managed to fix the issue with the recommendations from jmcilhinney. Below is the code that worked for me.
Public Function FindLastRowInColumn(ByVal XlWorkSheet As Excel.Worksheet, ByVal ColumnName As String) As Long
Dim LastRow As Long
With XlWorkSheet
LastRow = CType(.Cells(.Rows.Count, ColumnName), Excel.Range).End(Excel.XlDirection.xlUp).Row
End With
Return LastRow
End Function