Results 1 to 5 of 5

Thread: Find, Find Next, and Replace *resolved* Thanks

  1. #1

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195

    Find, Find Next, and Replace *resolved* Thanks

    ok well this is what im doing. i have a text editor thing that is build into a excel worksheet. and i have written a few functions that Find, FindNext and replace text in side of a text box. just like if you where to go up to the edit menu and find ect. anyway when i run my code to find the text i want to find it, it go and finds it like it is suppose to but what the problem is that after the first few times or few lines it started to find the words or characters but it will be off by a few characters like if i wanted to find "Kyle" in a paragraph it would highlight the 1 or 2 letters before the word then the "Ky" but wont. i dont know what could be my problem here is my code so you can see what im doing

    VB Code:
    1. Private Sub cmdFind1_Click()
    2. On Error GoTo errorhandler:
    3.     Dim intFoundPos As Integer
    4.     strSearchFor = InputBox("Find what?", "Find")
    5.     If Not strSearchFor = "" Then
    6.         intFoundPos = InStr(1, Sheet5.txtBox1.Text, strSearchFor, 1)
    7.         intfoundpos2 = intFoundPos
    8.         strButtonPush = "Find"
    9.         Sheet5.txtBox1.Activate
    10.     End If
    11.     Exit Sub
    12. errorhandler:
    13. End Sub
    14.  
    15. Private Sub cmdFindNext1_Click()
    16.     Dim intFoundPos As Integer, intBegSearch As Integer
    17.     intBegSearch = intfoundpos2 + 2
    18.     intFoundPos = InStr(intBegSearch, Sheet5.txtBox1.Text, strSearchFor, 1)
    19.     If Not Len(strSearchFor) = 0 Then
    20.         intfoundpos2 = intFoundPos
    21.         strButtonPush = "FindNext"
    22.         Sheet5.txtBox1.Activate
    23.     End If
    24. End Sub
    25.  
    26. Private Sub cmdReplace1_Click()
    27. On Error GoTo errorhandler:
    28.     Dim intFoundPos As Integer, strReplace As String
    29.     Dim var As Variant
    30.  
    31.     strSearchFor = InputBox("Find what?", "Find")
    32.     If Not strSearchFor = "" Then
    33.         intFoundPos = InStr(1, Sheet5.txtBox1.Text, strSearchFor, 1)
    34.         intfoundpos2 = intFoundPos
    35.         strButtonPush = "Replace"
    36.         Sheet5.txtBox1.Activate
    37.     End If
    38. errorhandler:
    39. End Sub
    40.  
    41. Private Sub txtBox1_GotFocus()
    42.     Const conBtns As Integer = vbOKOnly + vbInformation + vbDefaultButton1 + vbApplicationModal
    43.     Const conmsg As String = "The Search String was not found."
    44.     Dim intRetVal As Integer
    45.    
    46.     Select Case strButtonPush
    47.         Case "Find"
    48.             'find
    49.             If intfoundpos2 = 0 Then
    50.                 intRetVal = MsgBox(conmsg, conBtns, "Find")
    51.             Else
    52.                 Sheet5.txtBox1.SelStart = intfoundpos2 - 1
    53.                 Sheet5.txtBox1.SelStart = intfoundpos2 - 1 - Sheet5.txtBox1.CurLine
    54.                 Sheet5.txtBox1.SelLength = Len(strSearchFor)
    55.             End If
    56.         Case "FindNext"
    57.             'find next
    58.             If intfoundpos2 = 0 Then
    59.                 intRetVal = MsgBox(conmsg, conBtns, "Find Next")
    60.             Else
    61.                 Sheet5.txtBox1.SelStart = intfoundpos2
    62.                 Sheet5.txtBox1.SelStart = intfoundpos2 - 1 - Sheet5.txtBox1.CurLine
    63.                 Sheet5.txtBox1.SelLength = Len(strSearchFor)
    64.             End If
    65.         Case "Replace"
    66.             'replace
    67.             If intfoundpos2 = 0 Then
    68.                 intRetVal = MsgBox(conmsg, conBtns, "Find")
    69.             Else
    70.                 Sheet5.txtBox1.SelStart = intfoundpos2
    71.                 Sheet5.txtBox1.SelStart = intfoundpos2 - 1 - Sheet5.txtBox1.CurLine
    72.                 Sheet5.txtBox1.SelLength = Len(strSearchFor)
    73.                 strReplace = InputBox("Replace with what?", "Replace")
    74.                 If strSearchFor = "" Then
    75.                 Else
    76.                     Sheet5.txtBox1.SelText = strReplace
    77.                 End If
    78.             End If
    79.         Case Else
    80.     End Select
    81.     strButtonPush = ""
    82. End Sub
    Last edited by big_k105; Oct 8th, 2003 at 03:27 PM.

  2. #2
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    What's with the CurLine stuff? When you subtract it from the SelStart it will offset the selection by the line number that the text is found on. You don't need it. SelStart should alays be intfoundpos2 - 1.

  3. #3

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195
    ok well i have the curline taken out already but i still have a problem and that is that when i search to find something and if it is on the first like of the textbox it will highlite it perfectly. then i hit find next and it finds the next one but it is highted wrong kind of like this underlining here : Hello . and it will move over one every line i go down. the same thing happens when i search for something and it isnt on the top line it will move over by 1 every line it goes down.

    and thanks for the respond. hopefully you understand my problem thanks

  4. #4
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    I see. Carriage returns are separating the lines. You have to subtact out and Cr characters in the string o get the matching SelStart. I made some new code for you below.

    Putting the code in the GotFocus event will probably case yur text box to flicker. It is best to set all the properties of the textbox and THEN activate so it activates nice and clean. I also dump most of the code into one function because it is used by all of your processes.

    Using a stored intPos will only work as long as the text does not change. But what if the user adds or deletes text--the intPos in the string is no longer where the last foud text was. I changed it so that FindNext finds the next instance after the selection (or after the beginning of the selection if the search word is not aleady selected). The first Find r Replace also finds the next instance after the selection, but I included code you can commet out to always find the first instance.

    VB Code:
    1. Option Explicit
    2.  
    3. Dim strSearchFor    As String
    4.  
    5. Private Sub cmdFind1_Click()
    6.     strSearchFor = InputBox("Find what?", "Find")
    7. '    ' Set sel to start to to find first.
    8. '    sheet5.txtBox1.SelStart = 0
    9. '    sheet5.txtBox1.SelLength = 0
    10.     TextBoxFindNext
    11. End Sub
    12.  
    13. Private Sub cmdFindNext1_Click()
    14.     TextBoxFindNext
    15. End Sub
    16.  
    17. Private Sub cmdReplace1_Click()
    18.     Dim strReplace  As String
    19.     strSearchFor = InputBox("Find what?", "Find")
    20. '    ' Set sel to start to to find first.
    21. '    sheet5.txtBox1.SelStart = 0
    22. '    sheet5.txtBox1.SelLength = 0
    23.     If TextBoxFindNext Then
    24.         strReplace = InputBox("Replace with what?", "Replace")
    25.         If Not StrPtr(strReplace) = 0 Then  ' Check for cancel button.
    26.             With sheet5.txtBox1
    27.                 .SelText = strReplace
    28.                 .SelStart = .SelStart - Len(strReplace)
    29.                 .SelLength = Len(strReplace)
    30.             End With
    31.         End If
    32.     End If
    33. End Sub
    34.  
    35. Private Function TextBoxFindNext() As Boolean
    36. On Error GoTo errorhandle
    37.     Const conBtns   As Integer = vbOKOnly + vbInformation + _
    38.                                  vbDefaultButton1 + vbApplicationModal
    39.     Const conMsg    As String = "The Search String was not found."
    40.     Dim intFoundPos As Integer
    41.     Dim i           As Integer
    42.     If Not strSearchFor = "" Then
    43.         If sheet5.txtBox1.SelText = strSearchFor Then
    44.             intFoundPos = sheet5.txtBox1.SelStart + sheet5.txtBox1.SelLength + sheet5.txtBox1.CurLine
    45.             If sheet5.txtBox1.CurLine = 0 Then
    46.                 intFoundPos = intFoundPos + 1
    47.             End If
    48.         Else
    49.             intFoundPos = sheet5.txtBox1.SelStart + sheet5.txtBox1.CurLine
    50.         End If
    51.         If intFoundPos <= 0 Then intFoundPos = 1
    52.         intFoundPos = InStr(intFoundPos, sheet5.txtBox1.Text, strSearchFor, 1)
    53.         If intFoundPos = 0 Then
    54.             MsgBox conMsg, conBtns, "Find"
    55.         Else
    56.             intFoundPos = intFoundPos - 1
    57.             For i = 1 To intFoundPos
    58.                 If Mid$(sheet5.txtBox1.Text, i, 1) = vbCr Then
    59.                     intFoundPos = intFoundPos - 1
    60.                 End If
    61.             Next i
    62.             With sheet5.txtBox1
    63.                 .SelStart = intFoundPos
    64.                 .SelLength = Len(strSearchFor)
    65.                 .Activate
    66.             End With
    67.             TextBoxFindNext = True
    68.         End If
    69.     End If
    70.     Exit Function
    71. errorhandle:
    72.     If Err.Number = 2185 Then
    73.         ' Activate textbox to get CurLine.
    74.         sheet5.txtBox1.Activate
    75.         Resume
    76.     Else
    77.         Err.Raise Err.Number
    78.     End If
    79. End Function

  5. #5

    Thread Starter
    Addicted Member big_k105's Avatar
    Join Date
    May 2003
    Location
    North Dakota
    Posts
    195
    dude, it works perfect ! you dont know how much i needed this. i have been trying to get this to work for almost 2 weeks or more off and on. thanks again for the help.

    p.s. i really like the way you declare varibles and your way of coding is really easy to read. unlike mine anyway thanks again.

Posting Permissions

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



Click Here to Expand Forum to Full Width