Update
Inspired by the Daniel code above and the fact that this is the WAY! more interesting to me now, and then the actual work that I have to do, I created a robust full-featured function to find the first empty line on the sheet. Improvements are welcome! Otherwise, it will go to my library :) I hope others will benefit.
Function firstBlankRow(ws As Worksheet) As Long 'returns the row # of the row after the last used row 'Or the first row with no data in it Dim rngSearch As Range, cel As Range With ws Set rngSearch = .UsedRange.Columns(1).Find("") '-> does blank exist in the first column of usedRange If Not rngSearch Is Nothing Then Set rngSearch = .UsedRange.Columns(1).SpecialCells(xlCellTypeBlanks) For Each cel In rngSearch If Application.WorksheetFunction.CountA(cel.EntireRow) = 0 Then firstBlankRow = cel.Row Exit For End If Next Else '-> no blanks in first column of used range If Application.WorksheetFunction.CountA(Cells(.Rows.Count, 1).EntireRow) = 0 Then '-> is the last row of the sheet blank? '-> yeap!, then no blank rows! MsgBox "Whoa! All rows in sheet are used. No blank rows exist!" Else '-> okay, blank row exists firstBlankRow = .UsedRange.SpecialCells(xlCellTypeBlanks).Row + 1 End If End If End With End Function
Original answer
To find the first space in the sheet, replace this part of your code:
Cells(1, 1).Select For Each Cell In ws.UsedRange.Cells If Cell.Value = "" Then Cell = Num MsgBox "Checking cell " & Cell & " for value." Next
With this code:
With ws Dim rngBlanks As Range, cel As Range Set rngBlanks = Intersect(.UsedRange, .Columns(1)).Find("") If Not rngBlanks Is Nothing Then '-> make sure blank cell exists in first column of usedrange '-> find all blank rows in column A within the used range Set rngBlanks = Intersect(.UsedRange, .Columns(1)).SpecialCells(xlCellTypeBlanks) For Each cel In rngBlanks '-> loop through blanks in column A '-> do a countA on the entire row, if it 0, there is nothing in the row If Application.WorksheetFunction.CountA(cel.EntireRow) = 0 Then num = cel.Row Exit For End If Next Else num = usedRange.SpecialCells(xlCellTypeLastCell).Offset(1).Row End If End With
Scott Holtzman
source share