我是VBA的新手。
同一工作簿中的2个工作表。我想从工作表2中获取最后一个空行号,并将该数字超过工作表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