0 votes
in VBA by Beginner (15 points)
edited by
Private Sub CommandButton4_Click()

Static lastInstancePosition As Long
Dim InstancePosition As Long
Dim SearchText As String
Dim LineNumber As String

SearchText = "Find The Text"

    With TextBox2
        InstancePosition = InStr(lastInstancePosition + 1, .Text, SearchText)
        If InstancePosition > 0 Then
            .SetFocus
            .SelStart = InstancePosition - 1
            .SelLength = Len(SearchText)
            
            LineNumber = TextBox2.CurLine
            
            lastInstancePosition = InstancePosition
            
            MsgBox LineNumber + 1
       End If
    End With
    
End Sub

Hello everyone, i need some help

Example error:

If LineCount > Curline where is the text to find

 LineCount = 100 and the text to find is in the position "line 20" 

Thanks

by Beginner (15 points)
edited by
Error is because the break lines in the text

2 Answers

+1 vote
by Beginner (15 points)
selected by
 
Best answer

 

Hello everyone,

SOLVED ... my solution for my problem:

Private Sub CommandButton2_Click()
Static lastInstancePosition As Long
Dim InstancePosition As Long
Dim SearchText As String
Dim LineNumber As String

SearchText = "Find The Text"
'SearchText = Me.TextBox2.Text

    With TextBox1
        InstancePosition = InStr(lastInstancePosition + 1, .Text, SearchText)
        If InstancePosition > 0 Then
            .SetFocus
            .SelStart = InstancePosition - 1
            .SelLength = Len(SearchText)
            
            LineNumber = TextBox1.CurLine
            
            .SetFocus
            .SelStart = (InstancePosition - 1) - LineNumber
            .SelLength = Len(SearchText)
            
            Dim LineNumber2 As String
            LineNumber2 = TextBox1.CurLine
            
            lastInstancePosition = InstancePosition
            
            MsgBox "The text is in line: " & LineNumber2 + 1
            Else
            MsgBox "Text no found!"
       End If
    End With
End Sub

THANKS ;)

0 votes
by Beginner (4 points)

It is difficult to say without any information as to what the error is, or where it occurs, but I note that the LineNumber variable is dimensioned at the outset as a string, and when you present the line number in a MessageBox, you try to add 1 to it - I suggest that you try changing the LineNumber variable to long:

Dim LineNumber As Long

Hopefully that solves it.

by Beginner (15 points)
edited by

Thanks for the answer, 
I apologize for the misunderstanding

the error is because of line breaks VbCrLf

example to work around error for counting characters:

 

Private Sub CommandButton5_Click() ' count only characters in text
'STEP1
' line breaks
Dim xresult As String
Dim TxtLen As Double
Dim ChrTxt As Double

TextBox1.SetFocus

With TextBox1
    TxtLen = Len(.Text)
    ChrTxt = Len(Replace(.Text, Chr(10), ""))
End With

 xresult = TxtLen - ChrTxt + 1
 
'STEP2
' blank spaces
Dim xresult2 As String

Dim TxtLen2 As Double
TxtLen2 = Len(Replace(TextBox1, " ", ""))

xresult2 = TxtLen2

'STEP3

Dim ValueLineBreak As String
Dim ValueBlankSpace As String
Dim FinalCountOnlyCharacter As String

ValueLineBreak = xresult
ValueBlankSpace = xresult2

If xresult = 1 Then
     ValueLineBreak = xresult * 0
     ValueBlankSpace = xresult2
Else
     ValueLineBreak = xresult * 2
     ValueBlankSpace = xresult2 + 2
End If

On Error Resume Next

FinalCountOnlyCharacter = ValueBlankSpace - ValueLineBreak

MsgBox FinalCountOnlyCharacter

End Sub

 

to find string in the text I have this problem, for each line break, SelStart, SelLength and CurLine get in error !
I could work around this problem with the following code by removing the line breaks:

 

Private Sub CommandButton4_Click()
' remove VbCrLf
Dim strText As String
strText = TextBox1.Text

strText = Replace(Replace(strText, Chr(10), ""), Chr(13), "")

TextBox1.Text = strText

End Sub

 

but I don't want to change the text display

THANKS (I need some help)

Welcome to wellsr Q&A
wellsr Q&A is the VBA and Python programming community that rewards you for learning how to code.

Getting Started
Register
VBA Cheat Sheets (On Sale Now)

Earn free prizes for asking VBA and Python questions and for answering questions asked by others in our community.

Looking for something else? Hire our professional VBA Help, instead.

What makes us different?
Our points system rewards you with a chance for free gifts based on the quality of your questions and answers. All you have to do is post and you could get rewarded, like these members:

ParserMonster $25 Amazon Gift Card
Hightree $10 Amazon Gift Card
Thales1 $10 Amazon Gift Card
runfunke $10 Amazon Gift Card
coolag $10 Amazon Gift Card
Siew Hun $10 Amazon Gift Card

So, why don't you join us? It really is an encouraging way to motivate members in our VBA and Python community.

Register

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.

...