Find value not on activesheet

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,404
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
 

Some videos you may like

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,737
Office Version
  1. 365
Platform
  1. Windows
You will need to remove the "ActiveCell" reference.
 
Solution

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,404
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)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,737
Office Version
  1. 365
Platform
  1. Windows
Did you try it?
 

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,404

ADVERTISEMENT

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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,737
Office Version
  1. 365
Platform
  1. Windows
In that case you did not post the entire code.
You cannot activate a cell unless it's on the active sheet.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,737
Office Version
  1. 365
Platform
  1. Windows
My pleasure.
 

Watch MrExcel Video

Forum statistics

Threads
1,128,018
Messages
5,628,170
Members
416,297
Latest member
Kara Payne

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
Top