PDA

View Full Version : Solved: Find if then statement help please!



MagicMike
11-13-2008, 03:31 PM
Hello all,

I am writing some code to determine if the text in two different fields on a form (work order number and revission number) are present on a consolodated data sheet. I basically want to reject the creation of a new work order number into the system if it already exsists. This may be convoluded but here goes.

1. I have the form code which calls the sub "Check_WO_Use()" when the submit button is pressed.

2. I should note that excel will already be on the correct sheet to search when this is run.

3. I have a public variable declared in a different module as:
'Work order number exsists
Public WO_Exsists As Boolean
so I can set the status to true or false and return to the form code for some If Thens there.

4. The sub "Entry_New()" is called to create a new entry.

module 1:
Sub Check_WO_Use()
'Is the proposed work order number already in use?
Dim Find_Range As Range
Dim Exsisting_WO As String

On Error Resume Next
Exsisting_WO = Format(Left(Home.TextBox_Exsisting_WO.Text, 3) & _
Right(Home.TextBox_Exsisting_WO.Text, 3)) & Format(Right(Home.ComboBox_WO_Rev.Text, 2))

'Check Next available WO to see if its being used
If Home.TextBox_Exsisting_WO.Value = "" Then
Set Find_Range = Cells.Find(What:=Format(Left(Home.TextBox_Next_Avail_WO.Text, 3) & _
Right(Home.TextBox_Next_Avail_WO.Text, 3)), After:=Range("A6"), LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate

On Error GoTo 0
If Not Find_Range Is Nothing Then
MsgBox ("Available WO: This workorder number is already in use!")
WO_Exsists = True
Exit Sub
Else
MsgBox ("Available WO: This workorder number is available")
WO_Exsists = False
Call Entry_New
Exit Sub
End If

'Check Exsisting WO to see if its being used
Else
Set Find_Range = Cells.Find(What:=Exsisting_WO, After:=Range("A6"), LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate

On Error GoTo 0
If Not Find_Range Is Nothing Then
MsgBox ("Exsisting WO: This workorder number is already in use!")
WO_Exsists = True
Exit Sub
Else
MsgBox ("Exsisting WO: This workorder number is available")
WO_Exsists = False
Call Entry_New
Exit Sub
End If

End If
End Sub 'Check_WO_Use()


In the form sub that called the "Check_WO_Use()" sub I have

Call Check_WO_Use

If WO_Exsists = True Then
Exit Sub
Else
End If

Below this is the command to do some emailing stuff which is not relavent to this.

My question is this:

This works ok when there is not an existing WO, however when I try to create a new work order that alreasdy exsists it reacts just like it does not exsist.

Does anyone see anything strange with my find if them logic? I'm hoping that something will stand out to someone on here.

Thank you,

-Mike

Edit: I wanted to add that when I do enter an exsisting wo it does find that cell on the sheet with the exsisting work order. I think there is something flawed in my if then statement and if not is nothing then part. or there is something wrong with how I am using the public WO_Exsists boolean.

MagicMike
11-14-2008, 07:34 AM
ok, maybe all of my junk in here is making this too confusing. Can someone just give me an example of a working find method that could work here?

georgiboy
11-14-2008, 03:00 PM
Could you not do this with a range loop to find the WO number in question?

MagicMike
11-14-2008, 08:01 PM
Could you not do this with a range loop to find the WO number in question?

I'm finding it when it exsists ok, the problem is that my if then statement seems to react as though it found something even when it doesn't exsist. Does that make sense?

rbrhodes
11-14-2008, 11:55 PM
Hi MM,

Two things jump right out at me. Most importantly:

- Change the Lookat:=xlPpart to xlWhole. This is finding a partial string and is probably why your code is failing. eg if you are trying to find 12345 the code will find 12311, 12344, 12399 etc.

-The second is the On Error Resume Next / On Error Goto 0 pair. If you must allow for an Error (as you sometimes have to do) then reset to Goto as soon as possible! Or test for error in your code.

I believe it's number 1 tho.

MagicMike
11-15-2008, 01:20 PM
Hi MM,

Two things jump right out at me. Most importantly:

- Change the Lookat:=xlPpart to xlWhole. This is finding a partial string and is probably why your code is failing. eg if you are trying to find 12345 the code will find 12311, 12344, 12399 etc.

-The second is the On Error Resume Next / On Error Goto 0 pair. If you must allow for an Error (as you sometimes have to do) then reset to Goto as soon as possible! Or test for error in your code.

I believe it's number 1 tho.

ok update, I did change the part to whole as you suggested. But I am still having the same problem. As you said in your second suggestion I think I need some better control when the find method does not find what its looking for. I am trying to control this with an if then but I think I just need a condition when its found and when it doesn't find it I will naturaly get an error which will act as the else thatement would normally act. I'm going to play with the on error goto "err msg" of some sorts. I'm going to create a small file and post here to try and get a better idea of whats going on.

Thanks for the help.

rbrhodes
11-15-2008, 03:01 PM
Hi MM,

I just built an example and ran the code. The Find_Range line is not working (Find_Range is always = 'Nothing'). Change 'Activate' to 'Cells' so a range is found and I think you're built.


Set Find_Range = Cells.Find(What:=Format(Left(Home.TextBox_Next_Avail_WO.Text, 3) & _
Right(Home.TextBox_Next_Avail_WO.Text, 3)), After:=Range("A6"), LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Cells


and


Set Find_Range = Cells.Find(What:=Exsisting_WO, After:=Range("A6"), LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Cells



BTW, these 2 are pearls...

"This works ok when there is not an existing WO, however when I try to create a new work order that alreasdy exsists it reacts just like it does not exsist."

"I'm finding it when it exsists ok, the problem is that my if then statement seems to react as though it found something even when it doesn't exsist."

MagicMike
11-15-2008, 03:20 PM
dr,

lol, let me give that a try. As for my convoluded quotes you can see how disshoveled this problem has me. I dont even make any sense! lol I've been working on this for almost a week and I even brought my computer home from work so I could mess with it over the weekend! Thanks for your help, I'll let you know what happened.

MagicMike
11-16-2008, 01:52 PM
OK! great news, it looks like its working. I was still having problems with it because as I was messing with my file all week I had changed the Dim Find_Range As Range to Find_Range As Varient. That was screwing me up and then I found that.

I need to debug further but thanks alot for the help!