HELP! VBA Find won't work when copied!

Blackdeal

New Member
Joined
Apr 8, 2013
Messages
9
I set up a spreadsheet with a find macro, works no problem. Now I am copying the macro to a new spreadsheet that is set up EXACTLY as the one it is coming from and I cannot get the macro to work. What happened? The text in red below is where the debugger keeps on highlighting.

Sub Find()
Dim sFind As String
Dim rSearch As Range
Dim cl As Range
sFind = Sheets("HDM Find").Range("A1")
With Sheets("EMLT Range Planner")
'set range to search, limit it used cells in range
Set rSearch = .Range(.Cells(1, 3), .Cells(.Rows.Count, 1).End(xlUp))
With rSearch
Set cl = .Find(What:=sFind, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not cl Is Nothing Then '< find successful
cl.Offset(0, -1).Copy
Sheets("HDM Find").Range("A2").PasteSpecial
'<- change this to do something with the found cell
Else: MsgBox sFind & " not found"
End If
End With
End With
End Sub
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
I don't think you should have 2 x 'with' statements, otherwise how is Excel supposed to know to which you are referring in your code?


Instead, you could consider using variables.

Dim ws as Worksheet
Dim rng as Range
Dim rng2 as Range

set ws = sheets("something")
set rng = ws.Range("A1:A" & range("A" & rows.count).End(xlUp).Row)
rng2 = rng.Find("Something")



etc. etc.
 
Last edited:
Upvote 0
I would start by removing
Code:
After:=ActiveCell
because if the active cell is not in the search range you'll get an error.
 
Upvote 0
What did you change your code to? Also, what are you searching for? (dates are often problematic)
 
Upvote 0
Sorry, new at this, forgot to let you know the error I am getting. It is "Run-time error '13': Type mismatch"

I removed the
Code:
After:=ActiveCell

I am searching for strings 5 charaters long in both numeric and alpha-numeric.
 
Upvote 0
And don't know if this helps anything, but what I had in red above is what is highlighted in the debugger and the yellow arrow is pointing to the third line.

Code:
MatchCase:=False, SearchFormat:=False)
 
Upvote 0
Can you post the whole code as it currently is? (that part you mention is not a complete line and should never be highlighted on its own.)
 
Upvote 0
Code:
Sub Find()
    Dim sFind  As String
    Dim rSearch As Range
    Dim cl     As Range
    sFind = Sheets("Sheet1").Range("A1")
    With Sheets("EMLT Range Planner").Range("Range")
        'set range to search, limit it used cells in range
            Set cl = .Find(What:=sFind, After:=ActiveCell, LookIn:=xlFormulas, _
                           LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                           MatchCase:=False, SearchFormat:=False)
            If Not cl Is Nothing Then    '< find successful
                cl.Offset(0, -1).Copy
                Sheets("Sheet1").Range("A2").PasteSpecial
                '<- change this to do something with the found cell
            Else: MsgBox sFind & " not found"
            End If
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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