sub和function内容如下,供参考。
效果图:
过程代码:
Sub FirstRow_LastRow()
Dim MaxRow As Long, iRow As Long, vE3 As String
Dim fRow As Long, lRow As Long
MaxRow = Range("B" & Rows.Count).End(xlUp).Row
vE3 = Range("E3").Value
For iRow = 2 To MaxRow
If Cells(iRow, "B") = vE3 Then
fRow = iRow
Exit For
End If
Next iRow
For iRow = MaxRow To 2 Step -1
If Cells(iRow, "B") = vE3 Then
lRow = iRow
Exit For
End If
Next iRow
MsgBox "First Row: " & fRow & Chr(10) & "Last Row: " & lRow
End Sub
自定义函数:
Function FirstRow(rng As Range, ColNum As Integer) As Long
Dim MaxRow As Long, iRow As Long
Dim sValue As String, fRow As Long
MaxRow = Cells(Rows.Count, ColNum).End(xlUp).Row
sValue = rng.Value
For iRow = 2 To MaxRow
If Cells(iRow, "B") = sValue Then
fRow = iRow
Exit For
End If
Next iRow
FirstRow = fRow
End Function
Function LastRow(rng As Range, ColNum As Integer) As Long
Dim MaxRow As Long, iRow As Long
Dim sValue As String, lRow As Long
MaxRow = Cells(Rows.Count, ColNum).End(xlUp).Row
sValue = rng.Value
For iRow = MaxRow To 2 Step -1
If Cells(iRow, "B") = sValue Then
lRow = iRow
Exit For
End If
Next iRow
LastRow = lRow
End Function