i need to be able to remover certain lines from a text box
e.g. before
line 1
line 2
line 1
line 4
line 2
line 5
line 6
line 2
line 1
so when i type e.g. line 1 in a text box and hit enter i would like it to search the text box and remove what ever is in the text box including any doubles etc.
e.g. if i type line 1 in the text box 2 it remove all "line 1" lines
so after
line 2
line 4
line 2
line 5
line 6
line 2
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Eh! That will leave some blank lines there and it also replaces "Line11" with "1", "xyzLine1abc" with "xyzabc".
How about this:
Code:
'-- vbLf and vbCr are added to front and back to make sure
'-- the first and last lines will be also removed if matched.
sText = vbLf & Text1.Text & vbCr
sItem = vbLf & Text2.Text & vbCr
sText = Replace(sText, sItem, "")
Text1.Text = Mid$(sText, 2, Len(sText) - 2) '-- remove added chars
Don't forget to use [CODE]your code here[/CODE] when posting code
If your question was answered please use Thread Tools to mark your thread [RESOLVED]
'-- vbLf and vbCr are added to front and back to make sure
'-- the first and last lines will be also removed if matched.
sText = vbLf & Text1.Text & vbCr
sItem = vbLf & Text2.Text & vbCr
sText = Replace(sText, sItem, "")
Text1.Text = Mid$(sText, 2, Len(sText) - 2) '-- remove added chars
i just tried that and after clicking a commnd button nothing happened. i had two text boxes with multiline set as well as scroll bars
firstly i opened up vb 6 and click exe
then added 2 text boxes
then set the 2 text boxes to multiline and scroll bars (both ver and hori)
added a command button and added code to command button
Let's try to define the problem better, primarily because AnHn noticed that the problem is not that simple. You have three possibilities that have to be handled. The last line of a multi-line text box could have:
(1) VbCrLf and nothing else
(2) a string without VbCrLf at the end
(3) A string with VbCrLF at the end
The other lines have at least VbCrLF and may or may not have a string preceding it. It appears that you want the code to (A) get rid of all double line spacing first and then (B) examine the last line, which could now be any of (1) through (3) above, and delete any line within the text box that matches it, including the VbCrLf on that line, thus repacking the text box.
Duh! Everything said and done, just use this simple piece of code. This will work just you want it to. Note that you will need two textboxes (one will contain the data and the other will contain the line which you want to remove) and one command button. When you click on the command button, your specified line will be removed.
PLUS, this code has been tested for functionality and I'm including a working form file which you can dnld and use.
Code:
Private Sub Command1_Click()
Dim srch As String, linedata As String
Dim i As Integer
Text1.Tag = ""
For i = 1 To 50 'enough for 50 lines. extend the loop to as much as required
If InStr(Text1.Text,vbNewLine) = 0 Then
linedata = Text1.Text
Text1.Text = ""
Else
linedata = Left(Text1.Text,InStr(Text1.Text,vbNewLine)-1)
Text1.Text = Mid(Text1.Text,Instr(Text1.Text,vbNewLine)+2)
End If
If linedata <> Text2.Text Then
If Text1.Tag <> "" Then Text1.Tag = Text1.Tag & vbNewLine
Text1.Tag = Text1.Tag & linedata
End If
If Text1.Text = "" Then Exit For
Next i
Text1.Text = Text1.Tag
End Sub
If your problem is solved, then drag down the Thread Tools and mark your thread as Resolved.
If I helped you solve your problem, inflate some air into my ego by rating my post and adding a comment too.
For notorious issues (elaborate yourself) contact me via PM. I don't answer them in the forums EVER.
Just use this simple piece of code. This will work. Note that you will need two text boxes. One will contain the data and the other will contain the line that you want to remove. You also need a command button. When you click on the command button, your specified line will be removed. This code has been tested for functionality, and I'm including a working form file.
Code:
Private Sub Command1_Click()
Dim srch As String, linedata As String
Dim i As Integer
Text1.Tag = ""
For i = 1 To 50 'enough for 50 lines. extend the loop to as much as required
If InStr(Text1.Text,vbNewLine) = 0 Then
linedata = Text1.Text
Text1.Text = ""
Else
linedata = Left(Text1.Text,InStr(Text1.Text,vbNewLine)-1)
Text1.Text = Mid(Text1.Text,Instr(Text1.Text,vbNewLine)+2)
End If
If linedata <> Text2.Text Then
If Text1.Tag <> "" Then Text1.Tag = Text1.Tag & vbNewLine
Text1.Tag = Text1.Tag & linedata
End If
If Text1.Text = "" Then Exit For
Next i
Text1.Text = Text1.Tag
End Sub