0

I am trying to do a simple find method. I know "170" exists at Z2. My error reads

Run-time error '91':

Object variable or With block variable not set

Dim cutRef as Range

Worksheets("Sun").Activate
With Rows(2)
Set cutRef = .Find("170")
End With

MsgBox cutRef.Address

I have no clue what i am doing wrong. Thanks for your help.

TylerIlGenio
  • 65
  • 3
  • 11
  • 1
    A lot, start by not relying on `.Activate` and always define by `ThisWorkbook`. What line is this occurring on? The `MsgBox` or the `Set cutRef`? – Aleksandar Misich Jul 05 '17 at 19:55
  • I need activate because this is ran off a button on another sheet. i am getting the error as MsgBox because cutRef is reading as nothing – TylerIlGenio Jul 05 '17 at 20:09
  • 1
    That's not a reason to `Activate`. Qualify `Rows` with the worksheet object instead. You're getting an error because `.Find` doesn't *find* that you're making it look for. `Find` returns `Nothing` when it doesn't find anything, and your code assumes it always does. – Mathieu Guindon Jul 05 '17 at 20:12
  • Gah, wrong dupe, sorry. Try [that one](https://stackoverflow.com/q/26243490/1188513). Or [that one](https://stackoverflow.com/q/11984404/1188513). Or [that one](https://stackoverflow.com/q/29102052/1188513). – Mathieu Guindon Jul 05 '17 at 20:13
  • Always check if your Methods have returned `Nothing`, especially with the shakier application methods like `.Find` – Aleksandar Misich Jul 05 '17 at 20:14
  • As for *why .Find doesn't find the "170"*, there's nothing anyone on this site can do without seeing the actual worksheet. Good luck! – Mathieu Guindon Jul 05 '17 at 20:18

0 Answers0