Excel将最后一个空行号复制到另一个工作表中的单元格

时间:2014-06-16 17:47:56

标签: excel-vba vba excel

我是VBA的新手。

同一工作簿中的2个工作表。我想从工作表2中获取最后一个空行号,并将该数字超过工作表1中的单元格。

任何人,请帮忙。谢谢!

1 个答案:

答案 0 :(得分:0)

enter code here工作表中的最后空行通常位于工作表的底部或附近。所以我们从最后开始,继续努力:

Sub qwerty()
    Dim N As Long, wf As WorksheetFunction
    Dim s1 As Worksheet, s2 As Worksheet
    Set wf = Application.WorksheetFunction

    Set s1 = Sheets("Sheet1")
    Set s2 = Sheets("Sheet2")

    For N = Rows.Count To 1 Step -1
    If wf.CountA(s2.Cells(N, 1).EntireRow) = 0 Then
        s1.Range("B9").Value = N
        Exit Sub
    End If
    Next N
End Sub

修改#1:

要获取第一个空行而不是最后一个空行,请使用:

Sub qwerty()
        Dim N As Long, wf As WorksheetFunction
        Dim s1 As Worksheet, s2 As Worksheet
        Set wf = Application.WorksheetFunction

        Set s1 = Sheets("Sheet1")
        Set s2 = Sheets("Sheet2")

        For N = 1 to Rows.Count
        If wf.CountA(s2.Cells(N, 1).EntireRow) = 0 Then
            s1.Range("B9").Value = N
            Exit Sub
        End If
        Next N
    End Sub