Results 1 to 5 of 5

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

Threaded View

  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.

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