Consulting

Results 1 to 15 of 15

Thread: how to find next but a new word?

  1. #1
    VBAX Regular
    Joined
    Nov 2010
    Posts
    22
    Location

    how to find next but a new word?

    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
    Last edited by Aussiebear; 04-23-2023 at 09:07 PM. Reason: Adjusted the code tags

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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
    Last edited by Aussiebear; 04-23-2023 at 09:08 PM. Reason: Adjusted the code tags
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Nov 2010
    Posts
    22
    Location
    Thanks!
    now I need to write my word every time
    I want a button like findNext in excel

  4. #4
    VBAX Regular
    Joined
    Nov 2010
    Posts
    22
    Location
    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
    Last edited by Aussiebear; 04-23-2023 at 09:09 PM. Reason: Adjusted the code tags

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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
    Last edited by Aussiebear; 04-23-2023 at 09:10 PM. Reason: Adjusted the code tags
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Regular
    Joined
    Nov 2010
    Posts
    22
    Location
    No Xld
    Not working

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

    See this
    Last edited by Aussiebear; 04-23-2023 at 09:12 PM. Reason: Replaced all Upper case

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You mean mine or yours? If mine, that seems right to me, only the user decides to exit.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Contributor
    Joined
    May 2010
    Location
    Sydney, NSW, Australia
    Posts
    170
    Location
    Quote Originally Posted by kemas
    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.
    Last edited by Aussiebear; 04-23-2023 at 09:15 PM. Reason: Adjusted the code tags

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    VBAX Regular
    Joined
    Nov 2010
    Posts
    22
    Location
    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
    Last edited by Aussiebear; 04-23-2023 at 09:17 PM. Reason: Adjusted the code tags

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by kemas
    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?

    Quote Originally Posted by kemas
    your code : end all
    There is no End All in my code.

    Quote Originally Posted by kemas
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  12. #12
    VBAX Regular
    Joined
    Nov 2010
    Posts
    22
    Location
    Quote Originally Posted by xld
    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.

  13. #13
    VBAX Contributor
    Joined
    May 2010
    Location
    Sydney, NSW, Australia
    Posts
    170
    Location
    Quote Originally Posted by xld
    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/show...24&postcount=6 lead me to believe the OP wanted something different.

  14. #14
    VBAX Regular
    Joined
    Nov 2010
    Posts
    22
    Location
    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

  15. #15
    VBAX Contributor
    Joined
    May 2010
    Location
    Sydney, NSW, Australia
    Posts
    170
    Location
    Quote Originally Posted by kemas
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •