Error When Defining Variable - Can't Understand Why

losamfr17

Board Regular
Joined
Jun 10, 2016
Messages
149
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.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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:
Upvote 0
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.
 
Upvote 0
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).
 
Upvote 0

Forum statistics

Threads
1,223,947
Messages
6,175,559
Members
452,652
Latest member
eduedu

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