Error When Defining Variable - Can't Understand Why

losamfr17

Board Regular
Joined
Jun 10, 2016
Messages
138
Hi,

Why am I getting a run-time error saying, "Object variable or With block variable not set"? The error occurs after the first loop.
Here is my code:

Code:
Dim cell As RangeDim rw As Long


For Each cell In Application.Selection
    cell = cell.Value
    rw = Range("G:G").Find(cell.Value, , xlValues, xlWhole).Row
    Debug.Print rw
Next
Thank you.
 

losamfr17

Board Regular
Joined
Jun 10, 2016
Messages
138
Or this code:

Code:
For Each cell In Sheets("Info").Range("tbl_ClosingDrs[DoorID]")
    cell = cell.Value
    RW = Range("DoorID").Find(cell.Value, , xlValues).Row
    Range("C" & RW).Value = "Closed Stores"
    Range("G" & RW).Value = 995
    Range("H" & RW).Value = "CLOSED ACCOUNTS"
    Range("I" & RW).Value = 680
Next
 
Last edited:

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
22,902
You have cell defined as a range object, but in this line
Code:
cell = cell.Value
1) there is no use of the keyword Set, which is required to set the value of an object variable. Hence your error message

2) the .Value of a range object is either data type String, Double, Boolean or Error, so that would cause a Type Mismatch error also.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
34,624
Office Version
365, 2019, 2016, 2010
Platform
Windows, MacOS
That suggests that whatever you're searching for isn't being found. You should always test for that before trying to use a property of the found cell (like Row).
 

losamfr17

Board Regular
Joined
Jun 10, 2016
Messages
138
That suggests that whatever you're searching for isn't being found. You should always test for that before trying to use a property of the found cell (like Row).
It was that indeed, thank you so much!
 

Forum statistics

Threads
1,086,238
Messages
5,388,641
Members
402,128
Latest member
ghostthing

Some videos you may like

This Week's Hot Topics

Top