Trouble getting an IF THEN statement to work

jimayers

Board Regular
Joined
Nov 14, 2010
Messages
99
Hello, I am having difficulty adding an IF statement withou it causing problems. I need the statement because when the following code searches and finds nothing, it stops. I tried an on error goto, but that made this code not work at all:confused:

Code:
Sub Lookup(Target As Range)
Application.ScreenUpdating = False
Dim pFind As Range
'target.value is selected on sheet(4) and searches sheet(1) returning multiple values on sheet(4)
Set pFind = Sheets(1).Range("A:Z").find(What:=Target.Value, LookIn:=xlValues, LookAt:=xlPart).Offset(, -4).Resize(1, 11)

Target.Offset(, -4).Resize(1, 11).Value = pFind.Value

Application.ScreenUpdating = True
End Sub
I want to add something like
Code:
If pFind = "" then MsgBox("Target value not found")
and have the code wait for next target selection.

Thanks for your time - Jim A
 
Last edited:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Find returns a cell location (object), not a value. Try:

Code:
IF pFind Is Nothing Then MsgBox("Target not found")
 
Last edited:
Upvote 0
Find returns a cell location (object), not a value. Try:

Code:
IF pFind Is Nothing Then MsgBox("Target not found")
It stops working at:
Code:
Set pFind = Sheets(1).Range("E:E").find(What:=Target.Value, LookIn:=xlValues, LookAt:=xlPart).Offset(, -4).Resize(1, 11)
and it looks like before your suggested line "pFind Is Nothing"

Thanks - Jim A
 
Upvote 0
OK...I tried:
Code:
Set pFind = Sheets(1).Range("E:E").find(What:=Target.Value, LookIn:=xlValues, LookAt:=xlPart)
If pFind Is Nothing Then MsgBox ("Target not found")
If pFind Is Nothing Then Exit Sub
Set pFind = pFind.Offset(, -4).Resize(1, 11)
With Worksheets(4).Range("A:P")
Target.Offset(, -4).Resize(1, 11).Value = pFind.Value
End With

This seems to be working so far. Thanks for your help - JA
 
Upvote 0
I got a valid message when I coded the On Error like this:
Code:
Public Sub Lookup(Target As Range)

Application.ScreenUpdating = False
Dim pFind As Range
'target.value is selected on sheet(4) and searches sheet(1) returning multiple values on sheet(4)

On Error GoTo Oops:
Set pFind = Sheets(1).Range("E:Z").Find(What:=Target.Value, LookIn:=xlValues, LookAt:=xlPart).Offset(, -4).Resize(1, 11)

Target.Offset(, -4).Resize(1, 11).Value = pFind.Value

Application.ScreenUpdating = True
Exit Sub

Oops:
    MsgBox ("Not found")
    Application.ScreenUpdating = True
End Sub

Edit: I see you found another way to code it. I suspected the Offset might be the culprit somehow. Glad you got it working.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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