需要一个函数来返回工作表中填充了空闲单元格的第一个完全空的行(没有值,没有公式,没有空白)。不需要填写任何一列。
我试过了,但我甚至可以编译它:
Public Donations As Worksheet
Set Donations = Sheets("Sheet2")
Function getEmptyRow() As Long
Dim lastCol As Long, lastRow As Long, maxRow As Long
Dim col As Long
Dim r As Variant
lastCol = Donations.Cells(1, Columns.Count).End(xlToLeft).Column
For col = 1 To lastCol Step 1
lastRow = Donations.Cells(Rows.Count, col).End(xlUp).row
maxRow = Application.WorksheetFunction.max(maxRow, lastRow)
Next col
getEmptyRow = maxRow + 1
End Function发布于 2016-06-02 07:01:14
使用EntireRow (让我告诉您,这是非常有用的)并从A1开始逐行计数是一种非常基本的方法。
这将在“即时”窗口中告诉您:
Sub findemptyrow() '''Psuedo Code
Application.ScreenUpdating = False 'turns off annoying blinky
Range("a1").Activate 'start at beginning
While a <> 1 'keep going
If Application.CountA(ActiveCell.EntireRow) = 0 Then 'is it blank?
Debug.Print "Row " & (ActiveCell.Row) & " is blank." 'it is
a = 1 'stop going
End If
ActiveCell.Offset(1).Activate 'next cell
Wend 'do it all over again
Application.ScreenUpdating = True 'back to normal settings
End Sub将ScreenUpdating设为False会使速度更快,即使有10k行。
发布于 2016-06-02 07:59:16
Range.Find method可能是最方便的方法。查找通配符(What:=Chr(42)),从A1 (After:=.Cells(1, 1) )开始,向后搜索(SearchDirection:=xlPrevious),逐行搜索(SearchOrder:=xlByRows).Row),查看公式(LookIn:=xlFormulas),因为这将找到第一个值或公式;如果公式返回空字符串(""),则查看xlValues可能不正确。
Option Explicit
Public Donations As Worksheet
Sub test()
Set Donations = Worksheets("Sheet2")
Debug.Print getNextEmptyRow(Donations)
End Sub
Function getNextEmptyRow(ws As Worksheet)
With ws.Cells
getNextEmptyRow = .Find(What:=Chr(42), After:=.Cells(1, 1), LookIn:=xlFormulas, _
SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row + 1
End With
End Function不能在模块代码表的声明区域中设置Donations。在代码表的声明区域(顶部)声明公共变量,但在sub或函数中设置变量。
如果你想要‘下一个空行’,不要忘了在返回的行上加1。
发布于 2016-06-02 19:48:34
只是使用‘`Range’的SpecialCells方法的另一种选择:
Option Explicit
Sub Test()
Debug.Print "Last row on Sheet1 is: " & FirstCompletelyEmptyRow(Sheet1)
End Sub
Function FirstCompletelyEmptyRow(ByRef wsTarget As Worksheet) As Long
FirstCompletelyEmptyRow = wsTarget.Range("A1").SpecialCells(xlCellTypeLastCell).Row + 1
End Functionhttps://stackoverflow.com/questions/37580130
复制相似问题