Finding range of first empty cell in column and set as variable's range...

MistakesWereMade

Board Regular
Joined
May 22, 2019
Messages
103
I've listed my code below. I want to be able to set the first empty cell's range in a column equal to the range of variable "PasteCell". However, with my code, I get the error "Method 'Range' of object '_Global' failed when trying to msgbox the row location of PasteCell.

Any ideas?

Code:
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
Dim PasteCell As Variant


With Sheets("Inventory")


sourceCol = 7   'column F has a value of 6
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row


    'for every row, find the first blank cell and select it
    For currentRow = 3 To rowCount
        currentRowValue = Cells(currentRow, sourceCol).Value
        If IsEmpty(currentRowValue) Or currentRowValue = "" Then
            PasteCell = Sheets("Inventory").Range(currentRow & sourceCol)
        End If
    Next


MsgBox (Range(PasteCell).Row)


End With
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Try
Code:
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer
Dim currentRowValue As String
Dim PasteCell As Range


With Sheets("Inventory")


sourceCol = 7   'column F has a value of 6
rowCount = .Cells(Rows.Count, sourceCol).End(xlUp).Row


    'for every row, find the first blank cell and select it
    For currentRow = 3 To rowCount
        currentRowValue = .Cells(currentRow, sourceCol).Value
        If IsEmpty(currentRowValue) Or currentRowValue = "" Then
            Set PasteCell = .Cells(currentRow, sourceCol)
        End If
    Next


MsgBox PasteCell.Row


End With
 
Upvote 0
Try this

Code:
Sub test()
    Dim col As Long, lr As Long
    col = 7
    lr = Cells(Rows.Count, col).End(xlUp).Row + 1
    MsgBox "Row : " & Range(Cells(3, col), Cells(lr, col)).Find("").Row

End Sub
 
Upvote 0
Hi Fluffy,

Thanks again for your quick response. It seems to error out at the msgbox this time with a new error saying "object required".

Code:
With Sheets("Inventory")

sourceCol = 7   'column F has a value of 6
rowCount = .Cells(Rows.Count, sourceCol).End(xlUp).Row


    'for every row, find the first blank cell and select it
    For currentRow = 3 To rowCount
        currentRowValue = .Cells(currentRow, sourceCol).Value
        If IsEmpty(currentRowValue) Or currentRowValue = "" Then
            Set PasteCell = .Cells(currentRow & sourceCol)
        End If
    Next


MsgBox PasteCell.Row


End With
 
Upvote 0
Does your data in Col G contain blank cells below row 2, but before the last row with data?
 
Upvote 0
Yes to both, I put something in G3 because my goal is to have code that finds the first empty cell in each column heading from top to bottom. If it finds an empty cell, I would like to store the range so I can fill this empty cell's value. My col G contains its first blank cell in G5. However, sometimes the first blank cell could change to G3 or any cell after G100.
 
Last edited:
Upvote 0
Yes to both, I put something in G3 because my goal is to have code that finds the first empty cell in each column heading from top to bottom. If it finds an empty cell, I would like to store the range so I can fill this empty cell's value. My col G contains its first blank cell in G5. However, sometimes the first blank cell could change to G3 or any cell after G100.

The code works for me.
Try this

The code searches from G3 and up to the last row with data in column G (column 7) and shows the first empty cell.

Code:
Sub test2()
    Dim c As Long, a As String, r As Long
    c = 7
    a = Range(Cells(3, c), Cells(Cells(Rows.Count, c).End(xlUp)(2).Row, 7)).Address
    r = Evaluate("=MIN(IF(" & a & "="""",ROW(" & a & ")))")
    MsgBox "Row : " & r & " Celda : " & Cells(r, c).Address
End Sub
 
Upvote 0
This line
Code:
Set PasteCell = .Cells(currentRow & sourceCol)
should be
Code:
Set PasteCell = .Cells(currentRow, sourceCol)
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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