PDA

View Full Version : [SOLVED:] how to find next but a new word?



kemas
11-15-2010, 07:28 AM
I have this code to search a word " cat"


Cells.Find(What:="cat", After:=ActiveCell, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
i want to search a new word inputing by user
not the word written in code " cat"

thanks

Bob Phillips
11-15-2010, 07:40 AM
Dim inp As String
inp = InputBox("Search for what?")
If inp = "" Then Exit Sub
Cells.Find(What:=inp, After:=ActiveCell, _
LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate

kemas
11-15-2010, 09:56 AM
Thanks!
now I need to write my word every time
I want a button like findNext in excel

kemas
11-15-2010, 10:56 AM
THANKS

NOW IT WORKS AS I NEED

SEE THIS


Sub Macro1()
Dim inp As String
3 inp = InputBox("Search for what?")
If inp = "" Then Exit Sub
For i = 1 To 10
On Error GoTo 1
Cells.Find(What:=inp, After:=ActiveCell, _
LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
answer = MsgBox("next?", vbYesNo)
If answer = vbNo Then GoTo 3
Next i
1:
MsgBox "NOT fOUND"
End Sub

Bob Phillips
11-15-2010, 11:27 AM
I would do it without the Gotos


Sub Macro1()
Dim nextWord As Boolean
Dim inp As String
Dim answer As String
Dim firstaddress As String
Dim cell As Range
Do
inp = InputBox("Search for what?")
If inp <> "" Then
Set cell = Cells.Find(What:=inp, After:=ActiveCell, _
LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If cell Is Nothing Then
MsgBox "NOT fOUND"
Else
cell.Select
End If
answer = MsgBox("next?", vbYesNo)
End If
Loop Until inp = "" Or answer = vbNo
End Sub

kemas
11-15-2010, 11:36 AM
No Xld
Not working

When we choose "Yes" to find next
it returns to input box

See this

Bob Phillips
11-15-2010, 11:58 AM
You mean mine or yours? If mine, that seems right to me, only the user decides to exit.

Blade Hunter
11-15-2010, 08:09 PM
No Xld
Not working

When we choose "Yes" to find next
it returns to input box

See this


Move this line:


inp = InputBox("Search for what?")

above the Do

and change this:


answer = MsgBox("next?", vbYesNo)

to this


If MsgBox("Next?",vbYesNo) = VBNo then end


xld, the OP wants the input box up once then the option to keep searching the same word.

Bob Phillips
11-16-2010, 01:31 AM
No he doesn't. If you look at his code which he said works (but which I doubt does really) you will see that after he asks the question, he goes back to the inputbox.

kemas
11-16-2010, 03:48 AM
OK!
now it's working as i want or near i want

the last code after changing is


Dim nextWord As Boolean
Dim inp As String
Dim answer As String
Dim firstaddress As String
Dim cell As Range
inp = InputBox("Search for what?")
Do
If inp <> "" Then
Set cell = Cells.Find(What:=inp, After:=ActiveCell, _
LookIn:=xlValues, LookAt:=xlPart, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If cell Is Nothing Then
MsgBox "NOT fOUND"
Else
cell.Select
End If
If MsgBox("Next?", vbYesNo) = vbNo Then End
End If
Loop Until inp = ""
but we still have differences between the two codes

my code : return to inputbox after choosing "no"
your code : end all

thanks

ur code was useful to me in this line

" If MsgBox("Next?", vbYesNo) = vbNo Then End

it's new to me

thanks

Bob Phillips
11-16-2010, 05:45 AM
OK!
now it's working as i want or near i want

<snip>

but we still have differences between the two codes

my code : return to inputbox after choosing "no"

My code can change to that, but how are you going to get out of the loop if you do that?


your code : end all

There is no End All in my code.


ur code was useful to me
in this line " If MsgBox("Next?", vbYesNo) = vbNo Then End

it's new to me

Again, that is not what I had in my code.

kemas
11-16-2010, 02:07 PM
My code can change to that, but how are you going to get out of the loop if you do that?

when we return to inputbox we press cancel
remember : (Loop Until inp = "")

There is no End All in my code.
i said end not end all


Again, that is not what I had in my code.

Blade Hunter
11-16-2010, 03:15 PM
No he doesn't. If you look at his code which he said works (but which I doubt does really) you will see that after he asks the question, he goes back to the inputbox.

I agree completely and the code posted does suggest the need for what you built but the post following that: http://www.vbaexpress.com/forum/showpost.php?p=229924&postcount=6 lead me to believe the OP wanted something different.

kemas
11-16-2010, 03:25 PM
thank u very much

i think we must stop that because the problem has solved

i'm sorry for that

and i think that this post was useful to me

thanks xld and Blade Hunter
and i hope my English is better to speak better

Blade Hunter
11-16-2010, 03:56 PM
thank u very much

i think we must stop that because the problem has solved

i'm sorry for that

and i think that this post was useful to me

thanks xld and Blade Hunter
and i hope my English is better to speak better

No problem, glad you got it working :)