Find value not on activesheet

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Is it possible to find a value that is not on the active sheet?

Code:
Sheet1.Cells.Find(What:="somevalue", _
                       After:=ActiveCell, _
                       LookIn:=xlFormulas, _
                       LookAt:=xlPart, _
                       SearchOrder:=xlByRows, _
                       SearchDirection:=xlNext, _
                       MatchCase:=False, _
                       SearchFormat:=False)

If Sheet1 is the activesheet and contains the value, "somevalue" the above will run.

However if Sheet2 is the activesheet, the above will crash.

How can I amend the code so it will work without having to select Sheet1 first?

Thanks
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You will need to remove the "ActiveCell" reference.
 
Upvote 0
Solution
You will need to remove the "ActiveCell" reference.
You mean like this:

Code:
Sheet1.Cells.Find(What:="somevalue", _
                       LookIn:=xlFormulas, _
                       LookAt:=xlPart, _
                       SearchOrder:=xlByRows, _
                       SearchDirection:=xlNext, _
                       MatchCase:=False, _
                       SearchFormat:=False)
 
Upvote 0
Did you try it?
yes, it didn't work.

error message:

Code:
Run-time error '1004'

Activate method of Range class failed

However I amended it to:

Code:
Dim abc
    
abc = Sheet1.Cells.Find(What:="somevalue", _
                       LookIn:=xlFormulas, _
                       LookAt:=xlPart, _
                       SearchOrder:=xlByRows, _
                       SearchDirection:=xlNext, _
                       MatchCase:=False, _
                       SearchFormat:=False)

and this did return abc as somevalue (if it existed). If it did not, it crashed and I expected that.

Thanks for your help.
 
Upvote 0
In that case you did not post the entire code.
You cannot activate a cell unless it's on the active sheet.
 
Upvote 0

Forum statistics

Threads
1,214,630
Messages
6,120,634
Members
448,973
Latest member
ChristineC

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