Find 1st blank row to fill in when there is an active filter

Shloime

Board Regular
Joined
Oct 25, 2023
Messages
60
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
Platform
  1. Windows
I'm using VBA code to fill data into a sheet that already contains data, so I need to find the first blank row.
these are the 2 methods use, I prefer the first because it checks all columns, The issue with both is, when i have a filter on the target sheet it will find the last visible cell the same as when i click ctrl + arrow up in the worksheet +1 will be a row hidden by the filter and the code would over write the data there.
Does anyone have a solution to handle this, or a different method to find the first blank row?

VBA Code:
 Debug.Print Sheets("Sheet1").Cells.Find(What:="*", After:=Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False).Row + 1
    Debug.Print Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Is the data on the Target Sheet formatted as a Table?
 
Upvote 0
If first blank row in Column A is required.
VBA Code:
LR=Range("A" & Rows.count).end(xlup).row+1
If first blank in the sheet1 is required
VBA Code:
Lastrow = Sheets("Sheet1").UsedRange.Rows.Count + 1
 
Upvote 0
See if this is what you wanted.
Code:
Sub FindFirstBlankCell()
    Dim r As Range, x As Range
    If ActiveSheet.AutoFilterMode Then
        Set r = ActiveSheet.AutoFilter.Range
    End If
    If r Is Nothing Then Set r = ActiveSheet.UsedRange
    With r
        On Error Resume Next
        Set x = Intersect(.SpecialCells(12), .SpecialCells(4)).Cells(1)
        On Error GoTo 0
        If x Is Nothing Then MsgBox "No blank cell" Else MsgBox x.Address
    End With
End Sub
 
Upvote 0
If first blank row in Column A is required.
VBA Code:
LR=Range("A" & Rows.count).end(xlup).row+1
If first blank in the sheet1 is required
VBA Code:
Lastrow = Sheets("Sheet1").UsedRange.Rows.Count + 1
VBA Code:
LR=Range("A" & Rows.count).end(xlup).row+1
is the same as my method 2
VBA Code:
Debug.Print Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1
the issue is when i have filtered that sheet it would be 1 row below the last visible used row

your 2nd suggestion does help with this issue but it would ignore blank rows anywhere on the sheet so if my data begins at row 10 and ends in row 100 it would count 91 (somehow when i tried it's result was 92 I'm not sure why) and write the data in row 92 and the same with a blank row anywhere.
VBA Code:
Sheets("Sheet1").UsedRange.Rows.Count + 1
 
Upvote 0
Used Range has some pitfalls but this might work.

VBA Code:
Sub LastRowFilteredData()

    With Sheets("Sheet1").UsedRange
        Debug.Print .Cells(1).Offset(.Rows.Count).Row
    End With

End Sub

If you can narrow down your excel versions to Excel 2021 or MS 365 and select a column that you can rely on to have all lines populated, then you could use XLookup or XMatch.
 
Upvote 0
Used Range has some pitfalls but this might work.

VBA Code:
Sub LastRowFilteredData()

    With Sheets("Sheet1").UsedRange
        Debug.Print .Cells(1).Offset(.Rows.Count).Row
    End With

End Sub

If you can narrow down your excel versions to Excel 2021 or MS 365 and select a column that you can rely on to have all lines populated, then you could use XLookup or XMatch.
would used range only the used cells or from the very 1st cell in Sheet to the very last?
so if I have data in A11:D20 and then in A51:A60 sould Sheets("Sheet1").UsedRange.Rows.Count be 20 or 50?
 
Upvote 0
.cells(1) will set the top left corner and count from there.
I have logged off for the night. Just try it.
 
Upvote 0
.cells(1) will set the top left corner and count from there.
I have logged off for the night. Just try it.
I've tried your code Used range would cover the range from the first used till the last even if there are many blank rows in middle so your code does it.
I also tried to do it in 1 line this works
VBA Code:
Debug.Print Split(Sheets("Sheet1").UsedRange.Address, "$")(4)
what pitfalls may still be?
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,596
Members
452,657
Latest member
giadungthienduyen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top