|
-
Sep 24th, 2003, 03:16 PM
#1
Thread Starter
Addicted Member
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:
Private Sub cmdFind1_Click()
On Error GoTo errorhandler:
Dim intFoundPos As Integer
strSearchFor = InputBox("Find what?", "Find")
If Not strSearchFor = "" Then
intFoundPos = InStr(1, Sheet5.txtBox1.Text, strSearchFor, 1)
intfoundpos2 = intFoundPos
strButtonPush = "Find"
Sheet5.txtBox1.Activate
End If
Exit Sub
errorhandler:
End Sub
Private Sub cmdFindNext1_Click()
Dim intFoundPos As Integer, intBegSearch As Integer
intBegSearch = intfoundpos2 + 2
intFoundPos = InStr(intBegSearch, Sheet5.txtBox1.Text, strSearchFor, 1)
If Not Len(strSearchFor) = 0 Then
intfoundpos2 = intFoundPos
strButtonPush = "FindNext"
Sheet5.txtBox1.Activate
End If
End Sub
Private Sub cmdReplace1_Click()
On Error GoTo errorhandler:
Dim intFoundPos As Integer, strReplace As String
Dim var As Variant
strSearchFor = InputBox("Find what?", "Find")
If Not strSearchFor = "" Then
intFoundPos = InStr(1, Sheet5.txtBox1.Text, strSearchFor, 1)
intfoundpos2 = intFoundPos
strButtonPush = "Replace"
Sheet5.txtBox1.Activate
End If
errorhandler:
End Sub
Private Sub txtBox1_GotFocus()
Const conBtns As Integer = vbOKOnly + vbInformation + vbDefaultButton1 + vbApplicationModal
Const conmsg As String = "The Search String was not found."
Dim intRetVal As Integer
Select Case strButtonPush
Case "Find"
'find
If intfoundpos2 = 0 Then
intRetVal = MsgBox(conmsg, conBtns, "Find")
Else
Sheet5.txtBox1.SelStart = intfoundpos2 - 1
Sheet5.txtBox1.SelStart = intfoundpos2 - 1 - Sheet5.txtBox1.CurLine
Sheet5.txtBox1.SelLength = Len(strSearchFor)
End If
Case "FindNext"
'find next
If intfoundpos2 = 0 Then
intRetVal = MsgBox(conmsg, conBtns, "Find Next")
Else
Sheet5.txtBox1.SelStart = intfoundpos2
Sheet5.txtBox1.SelStart = intfoundpos2 - 1 - Sheet5.txtBox1.CurLine
Sheet5.txtBox1.SelLength = Len(strSearchFor)
End If
Case "Replace"
'replace
If intfoundpos2 = 0 Then
intRetVal = MsgBox(conmsg, conBtns, "Find")
Else
Sheet5.txtBox1.SelStart = intfoundpos2
Sheet5.txtBox1.SelStart = intfoundpos2 - 1 - Sheet5.txtBox1.CurLine
Sheet5.txtBox1.SelLength = Len(strSearchFor)
strReplace = InputBox("Replace with what?", "Replace")
If strSearchFor = "" Then
Else
Sheet5.txtBox1.SelText = strReplace
End If
End If
Case Else
End Select
strButtonPush = ""
End Sub
Last edited by big_k105; Oct 8th, 2003 at 03:27 PM.
-
Oct 6th, 2003, 08:43 PM
#2
Fanatic Member
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.
-
Oct 7th, 2003, 10:07 AM
#3
Thread Starter
Addicted Member
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
-
Oct 7th, 2003, 08:17 PM
#4
Fanatic Member
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:
Option Explicit
Dim strSearchFor As String
Private Sub cmdFind1_Click()
strSearchFor = InputBox("Find what?", "Find")
' ' Set sel to start to to find first.
' sheet5.txtBox1.SelStart = 0
' sheet5.txtBox1.SelLength = 0
TextBoxFindNext
End Sub
Private Sub cmdFindNext1_Click()
TextBoxFindNext
End Sub
Private Sub cmdReplace1_Click()
Dim strReplace As String
strSearchFor = InputBox("Find what?", "Find")
' ' Set sel to start to to find first.
' sheet5.txtBox1.SelStart = 0
' sheet5.txtBox1.SelLength = 0
If TextBoxFindNext Then
strReplace = InputBox("Replace with what?", "Replace")
If Not StrPtr(strReplace) = 0 Then ' Check for cancel button.
With sheet5.txtBox1
.SelText = strReplace
.SelStart = .SelStart - Len(strReplace)
.SelLength = Len(strReplace)
End With
End If
End If
End Sub
Private Function TextBoxFindNext() As Boolean
On Error GoTo errorhandle
Const conBtns As Integer = vbOKOnly + vbInformation + _
vbDefaultButton1 + vbApplicationModal
Const conMsg As String = "The Search String was not found."
Dim intFoundPos As Integer
Dim i As Integer
If Not strSearchFor = "" Then
If sheet5.txtBox1.SelText = strSearchFor Then
intFoundPos = sheet5.txtBox1.SelStart + sheet5.txtBox1.SelLength + sheet5.txtBox1.CurLine
If sheet5.txtBox1.CurLine = 0 Then
intFoundPos = intFoundPos + 1
End If
Else
intFoundPos = sheet5.txtBox1.SelStart + sheet5.txtBox1.CurLine
End If
If intFoundPos <= 0 Then intFoundPos = 1
intFoundPos = InStr(intFoundPos, sheet5.txtBox1.Text, strSearchFor, 1)
If intFoundPos = 0 Then
MsgBox conMsg, conBtns, "Find"
Else
intFoundPos = intFoundPos - 1
For i = 1 To intFoundPos
If Mid$(sheet5.txtBox1.Text, i, 1) = vbCr Then
intFoundPos = intFoundPos - 1
End If
Next i
With sheet5.txtBox1
.SelStart = intFoundPos
.SelLength = Len(strSearchFor)
.Activate
End With
TextBoxFindNext = True
End If
End If
Exit Function
errorhandle:
If Err.Number = 2185 Then
' Activate textbox to get CurLine.
sheet5.txtBox1.Activate
Resume
Else
Err.Raise Err.Number
End If
End Function
-
Oct 8th, 2003, 03:13 PM
#5
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|