Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 行计数器只计数?第一排_Excel_Vba_Rowcount - Fatal编程技术网

Excel 行计数器只计数?第一排

Excel 行计数器只计数?第一排,excel,vba,rowcount,Excel,Vba,Rowcount,我的代码应该选择A-H中从工作表顶部到包含J列中文本的最底行的所有项目。但是,现在它所做的只是选择最上面的一行。这段代码在其他地方工作得很好,但当我在这里运行它时,它只选择最上面的一行 下面是代码和它当前的功能。当在另一个finalrow=语句中运行注释掉的位时,它也会执行相同的操作 Option Explicit Sub FindRow() Dim reportsheet As Worksheet Dim finalrow As Integer Set reportsheet = She

我的代码应该选择A-H中从工作表顶部到包含J列中文本的最底行的所有项目。但是,现在它所做的只是选择最上面的一行。这段代码在其他地方工作得很好,但当我在这里运行它时,它只选择最上面的一行

下面是代码和它当前的功能。当在另一个
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

很抱歉延迟回复。它仍然做同样的事情。然而,在这一点上,我认为这部分代码没有问题。我还把随机文本放在K列中,在J列中有文本的地方旁边,在finalrow=中将“J”切换为“K”,效果很好。不同之处在于,J列自上而下充满了公式。行计数是否也查找公式?如果有,有办法解决吗?@jacob_m第J列用什么公式填充?类似“=If(B2=I2,”,“TON”)”的东西,除了数字对应于它们的行之外。对不起,我不在我的电脑旁,但我相信那是真的it@jacob_m刚刚更新了我的答案并添加了代码,以使用循环查找第J列的最后一行,循环也在顶部启动
finalrow=1