行计数器只计数?顶行

Row Counter Only Counting? Top Row

我的代码应该 select A-H 中从 sheet 顶部到包含 J 列中文本的最底部行的所有项目。但是,现在它所做的只是 select 第一行。出于其他目的,此代码在其他地方运行良好,但是当我 运行 它在这里时,它仅 select 位于第一行。

这是代码及其当前的作用。当 运行 代替另一个 finalrow = 语句时,注释掉的位的作用相同。

Option Explicit

Sub FindRow()

Dim reportsheet As Worksheet
Dim finalrow As Integer

Set reportsheet = Sheet29

Sheet29.Activate
'finalrow = Cells(Rows.Count, 10).End(xlUp).Row
finalrow = Range("J1048576").End(xlUp).Row


    If Not IsEmpty(Sheet29.Range("B2").Value) Then
         Range(Cells(1, 1), Cells(finalrow, 8)).Select

    End If
End Sub

这是行计数器有效的代码摘录。

datasheet.Select
finalrow = Cells(Rows.Count, 1).End(xlUp).Row

''loop through the rows to find the matching records
For i = 1 To finalrow
    If Cells(i, 1) = item_code Then ''if the name in H1 matches the search name then
        Range(Cells(i, 1), Cells(i, 9)).Copy ''copy columns 1 to 9 (A to I)
        reportsheet.Select ''go to the report sheet
        Range("A200").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False ''find the first blank and paste info there
        datasheet.Select ''go back to the data sheet and continue searching
        End If
Next i

你可以试试这个:

Option Explicit
Sub FindRow()

    ' always use Longs over Integers
    Dim finalrow As Long: finalrow = 1

    ' you might not need this line tbh
    Sheet29.Activate

    With Sheet29

        ' custom find last row
        Do While True
            finalrow = finalrow + 1
            If Len(CStr(.Range("J" & finalrow).Value)) = 0 Then Exit Do
        Loop

        ' Len() is sometimes better then IsEmpty()
        If Len(CStr(.Range("B2").Value)) > 0 Then
            .Range(.Cells(1, 1), .Cells((finalrow - 1), 8)).Select
        End If
    End With

End Sub