Find Value on the other worksheet - 6 lines of code - something is wrong

makiwara

Board Regular
Joined
Mar 8, 2018
Messages
171
Hi! Thank you for reading my quick question!

What I need: I select a cell, then a variable stores its value, and looks for it on worksheet "1" in column ("B:B").

Sub findstring()
Dim CurrValue As String


CurrValue = ActiveCell.Value



Sheets(1).Columns("B:B").Find(What:=CurrValue, After:=ActiveCell, LookIn:=xlFormulas,
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

End Sub

I highlighted the problem. (synctax error according to the debugger) Thank you for your answer!
 
Last edited:

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi,
Your code written that way can produce either a type mismatch error if search value not found or Activate Range Of Class error if the search sheet is not the active sheet.
Also, your published code is missing a line continuation character _ which will cause compile error.

You can resolve these issues by modifying the code as follows:

Code:
 Sub findstring()    
    Dim CurrValue As String
    Dim Foundcell As Range
    
    
    CurrValue = ActiveCell.Value
    
    If Len(CurrValue) = 0 Then Exit Sub
    
    Set Foundcell = Sheets(1).Columns("B:B").Find(What:=CurrValue, After:=ActiveCell, LookIn:=xlFormulas, _
                                                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                                MatchCase:=False, SearchFormat:=False)
                                                
    If Not Foundcell Is Nothing Then
'value found do stuff here
        MsgBox Foundcell.Address
    Else
'not found
        MsgBox CurrValue & Chr(10) & "Record Not Found", 48, "Not Found"
        
    End If


End Sub

Dave
 
Last edited:
Upvote 0
Thank you Dave for the quick response!

I created a button, and I inserted your code to that, so if I was select a cell and click the button, it has to jump to the Sheet("1") and look for the cell's content in column "B".

So I expected that everything will be all right, but unfortunately the code didn't run. Have I done something wrong?
https://imgur.com/a/thqtnYe

Thank you for your answer , it means to me a lot! Have a nice day! :-)

But
Hi,
Your code written that way can produce either a type mismatch error if search value not found or Activate Range Of Class error if the search sheet is not the active sheet.
Also, your published code is missing a line continuation character _ which will cause compile error.

You can resolve these issues by modifying the code as follows:

Code:
 Sub findstring()    
    Dim CurrValue As String
    Dim Foundcell As Range
    
    
    CurrValue = ActiveCell.Value
    
    If Len(CurrValue) = 0 Then Exit Sub
    
    Set Foundcell = Sheets(1).Columns("B:B").Find(What:=CurrValue, After:=ActiveCell, LookIn:=xlFormulas, _
                                                LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                                MatchCase:=False, SearchFormat:=False)
                                                
    If Not Foundcell Is Nothing Then
'value found do stuff here
        MsgBox Foundcell.Address
    Else
'not found
        MsgBox CurrValue & Chr(10) & "Record Not Found", 48, "Not Found"
        
    End If


End Sub

Dave
 
Upvote 0
code ran ok for me.
what error is being reported?

Dave
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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