I'm having to copy and paste variable amounts (rows) of data into a single column. Thus, I need to know which is the last row that has data in it. I'm using the following function to get me this information:
Lets say that in column A, I have 48 rows of data (percentages) and in column B I have 16 rows of data (numbers). Column C also has 48 rows of data (again numbers). They are all on sheet "NewSheet". Here are the results that I get when I call this function in the immediate window for each row:
Why does this work for column A, but not for columns B or C?
Code:
Public Function LastRow(mySheet As String, myCol As String) As Long
Dim ws As Worksheet
Dim lRow As Long
Set ws = ThisWorkbook.Sheets(mySheet)
With ws
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
LastRow = .Cells.Find(What:="*", _
After:=.Range(myCol & "1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
LastRow = 1
End If
End With
End Function
Code:
?LastRow("NewSheet", "A")
48
?LastRow("NewSheet", "B")
1
?LastRow("NewSheet", "C")
1
Comment