Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How can I alter "On Error" code so that different code is run depending on when the error is found?

Posted on 2012-04-12
10
Medium Priority
?
208 Views
Last Modified: 2012-04-13
I have a workd=sheet that includes a UserForm that loads when a button is clicked.
The user can enter a code in 10 individual fields.
If the code already exists within the column of another worksheet then I would like a message appear that says that the user cannot enter a duplicate code.
I have employed a NamedRange to accommodate this that is used elsewhere in the workbook for other functions.
I thought I could use the On Error code to allow passage through the code if there were no matches with NamedRange as below, but this of course doesn't work.
How can I ammend please?
Des

On Error GoTo Stage3
If Me.TextBox101.Value = "" Then GoTo Stage3
zzz = 1
x2 = Application.WorksheetFunction.VLookup((Me.TextBox101.Value), Sheets("Tool Database").Range("Tools"), 3, 0)
GoTo Msg1
Stage3:
On Error GoTo Stage4
If Me.TextBox106.Value = "" Then GoTo Stage4
zzz = 2
x3 = Application.WorksheetFunction.VLookup((Me.TextBox106.Value), Sheets("Tool Database").Range("Tools"), 3, 0)
GoTo Msg1
Stage4:

...etc
0
Comment
Question by:spar-kle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37837800
The general format is: =IF(ISERROR(the formula to be checked, what if the result is an error,what if the result is not an error ).

http://www.excel-examples.com/iserror.htm
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 37837845
Perhaps something like

Sub whatever()
' ....
    If Me.TextBox101.Value <> "" Then
        zzz = 1
        x2 = LookupTool(Me.TextBox101.Value)
    ElseIf Me.TextBox106.Value <> "" Then
        zzz = 2
        x3 = LookupTool(Me.TextBox106.Value)
    ' add more ElseIf statements as needed
    End If
'....
End Sub


Function LookupTool(varIn) As String
    Dim varVal
    varVal = Application.VLookup(varIn, Sheets("Tool Database").Range("Tools"), 3, 0)
    If IsError(varVal) Then
        LookupTool = ""
    Else
        LookupTool = varVal
    End If
End Function

Open in new window

0
 

Author Comment

by:spar-kle
ID: 37838656
Thanks for your replies, really appreciated.

rorya: I can see that this will work with my workbook.

The variable zzz refers to the field number so that I can create a message for the user.

I can add code:
    If IsError(varVal) Then
        MsgBox ("A code entered has already been used")

How can I create a message, when the code is found within the "Tool Database" range that says, "The code entered in field " & zzz & " has already been used"
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 85

Expert Comment

by:Rory Archibald
ID: 37839479
If zzz > 0 then msgbox "The code entered in field " & zzz & " has already been used"

Open in new window


if I understood you right.
0
 

Author Comment

by:spar-kle
ID: 37840422
Function LookupTool(varIn) As String
    Dim varVal
    varVal = Application.VLookup(varIn, Sheets("Tool Database").Range("Tools"), 3, 0)
    If IsError(varVal) Then
        LookupTool = ""
MsgBox "The code entered in field " & zzz & " has already been used"
    Else
        LookupTool = varVal
    End If
End Function

Open in new window


Sorry I meant, how do I declare the variable zzz, I presume "Public zzz As Integer" in the function module??

Also with this function I get my message "The code entered in field " & zzz & " has already been used" even when the code entered into theTextBox is not within the worksheet "Tool Database"

ie if the code IS IN the worksheet then I DO NOT get the message, if the code IS NOT within the worksheet I DO get the message ...I really need it the other way around

Actually it seems to revolve around the first TextBox
If the data in this TextBox is not present within the worksheet then I get the message, and all subsequent boxes seem to be ignored.
If the data in this TextBox is present within the worksheet then I do not get the message, and all subsequent boxes seem to be ignored.
0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 37842642
Unfortunately, the original code you posted uses a lot of Goto statements so it was impossible for me to follow the actual intended flow. It sounds to me as though you want to do something like this:

   If Me.TextBox101.Value <> "" Then
        zzz = 1
        If LookupTool(Me.TextBox101.Value) <> "" then msgbox "The code entered in field " & zzz & " has already been used"
End If
    If Me.TextBox106.Value <> "" Then
        zzz = 2
        If LookupTool(Me.TextBox106.Value)  <> "" then msgbox "The code entered in field " & zzz & " has already been used"
End If
    ' add more If  statements as needed
   

Open in new window

0
 

Author Closing Comment

by:spar-kle
ID: 37842915
I'm sorry about my ignorance, but I really appreciate your help.
This works brilliantly
Des
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 37842968
Glad to help.

Note that wasn't a criticism (although I don't like Goto statements except in error handling) - just a statement that without the full code I couldn't see where the code was jumping to. ;)
0
 

Author Comment

by:spar-kle
ID: 37843103
...I didn't think you were criticising ...and I am truly ignorant!! :-)
but with with the help of you guys at EE I have been able to accomplish a great deal.
Really appreciate your support
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 37843121
Fair enough. :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question