I have a list of text. Each line in the list is a string, then ZZZ then a string. What I want is to cut out from the beginning of the line up to and including ZZZ. Then I want to replace the letter K in each string with J. How would I do this?
Printable View
I have a list of text. Each line in the list is a string, then ZZZ then a string. What I want is to cut out from the beginning of the line up to and including ZZZ. Then I want to replace the letter K in each string with J. How would I do this?
Give me an example of the string.
blahkblahXXX bbbbbbbbbbb
This obviously isnt the real string but its like it will be. So I wanna cut from the beinning to XXX including it. Then make the k a j.
*bump*
Try something like thisVB Code:
Private Sub Command1_Click() Dim strString As String strString = "blahkblahXXX bbbbbbbbbbb" strString = Replace(strString, "k", "j") strString = Replace(strString, "X", "") MsgBox strString End Sub
Try this:
VB Code:
Dim sText As String sText = Replace(Split("blahkblahXXX bbbbbbbbbbb", "XXX")(0) & "XXX", "k", "j") Debug.Print (sText)
I need to cut of the rest of the string. Starting with the space to the end.
One modificationQuote:
Originally Posted by Steve_F
VB Code:
Private Sub Command1_Click() Dim strString As String Dim arrString() As String strString = "blahkblahXXX bbbbbbbbbbb" strString = Replace(strString, "k", "j") strString = Replace(strString, "X", "") arrString = Split(strString, " ") MsgBox arrString(0) End Sub
This is all getting confusing. Lemme just explain what I have. I wanna load a list which lines will resemble this :
email(AT)hotmail.com Fargo, Nd Blah
I wanna load the list. Take that excess at the end away. And change (AT) to @. Here is what I have :
VB Code:
Private Sub cmdStart_Click() 'load names Dim strBuff As String 'Clear the ListBox contents List1.Clear intFF = FreeFile Open App.Path & "\load.txt" For Input As #intFF Do Until EOF(1) Line Input #intFF, strBuff List1.AddItem strBuff Loop Close #intFF Label3.BackColor = &HFF00& 'remove excess Label4.BackColor = &HFF00& 'replace text Dim strString As String For intIdx = 0 To intCount strString = List1.Selected(intIdx) strString = Replace(strString, "(at)", "@") List1.Selected(intIdx) = strString Next Label5.BackColor = &HFF00& 'save new file Dim intCount As Integer Dim intIdx As Integer intCount = List2.ListCount - 1 Open App.Path & "\result.txt" For Output As #1 For intIdx = 0 To intCount If List1.Selected(intIdx) Then Print #1, List1.List(intIdx) Next Close #1 Label6.BackColor = &HFF00& End Sub
You can use Split to get rid of the rest (your Replace looks fine)That should work.VB Code:
Dim strString As String Dim arrString As String For intIdx = 0 To intCount strString = List1.Selected(intIdx) strString = Replace(strString, "(at)", "@") arrString = Split(strString, " ") List1.Selected(intIdx) = arrString(0) Next
Did you try the example I posted before?
VB Code:
For intIdx = 0 To intCount strString = List1.Selected(intIdx) strString = Replace(Split(strString, " ")(0), "(at)", "@", , , vbTextCompare) List1.Selected(intIdx) = strString Next
Error "Expected array" on arrString(0)
So I used the second one. But its not working... It says its dont but doesnt write to file :
VB Code:
Private Sub cmdStart_Click() 'load names Dim strBuff As String 'Clear the ListBox contents List1.Clear intFF = FreeFile Open App.Path & "\load.txt" For Input As #intFF Do Until EOF(1) Line Input #intFF, strBuff List1.AddItem strBuff Loop Close #intFF Label3.BackColor = &HFF00& 'remove excess For intIdx = 0 To intCount strString = List1.Selected(intIdx) strString = Replace(Split(strString, " ")(0), "(at)", "@", , , vbTextCompare) List1.Selected(intIdx) = strString Next Label4.BackColor = &HFF00& 'replace text Label5.BackColor = &HFF00& 'save new file intCount = List1.ListCount - 1 Open App.Path & "\emails.txt" For Output As #1 For intIdx = 0 To intCount If List1.Selected(intIdx) Then Print #1, List1.List(intIdx) Next Close #1 Label6.BackColor = &HFF00& End Sub
Thats because I'm moron. :sick:Quote:
Originally Posted by Steve_F
You need to change:blush: Sorry 'bout that.VB Code:
Dim arrString As String 'change to Dim arrString() As String
No prob lol. So it's not saving right now..
VB Code:
Private Sub cmdStart_Click() 'load names Dim strBuff As String 'Clear the ListBox contents List1.Clear intFF = FreeFile Open App.Path & "\load.txt" For Input As #intFF Do Until EOF(1) Line Input #intFF, strBuff List1.AddItem strBuff Loop Close #intFF Label3.BackColor = &HFF00& 'remove excess Dim strString As String Dim arrString() As String For intIdx = 0 To intCount strString = List1.Selected(intIdx) strString = Replace(strString, "(at)", "@") arrString = Split(strString, " ") List1.Selected(intIdx) = arrString(0) Next Label4.BackColor = &HFF00& 'replace text Label5.BackColor = &HFF00& 'save new file intCount = List1.ListCount - 1 Open App.Path & "\emails.txt" For Output As #1 For intIdx = 0 To intCount If List1.Selected(intIdx) Then Print #1, List1.List(intIdx) Next Close #1 Label6.BackColor = &HFF00& End Sub
It doesnt save anything.
As you loop through the listbox are items selected?
If not, then If List1.Selected(intIdx) will always equal False which means your Print code will never execute.
Replace the #1 with your intFF variable, and assign it to Freefile again. It's possible that the filehandle isn't available because you closed it so recently.Quote:
Originally Posted by Steve_F
Say what?
VB Code:
intFF = FreeFile intCount = List1.ListCount - 1 Open App.Path & "\emails.txt" For Output As #intFF For intIdx = 0 To intCount If List1.Selected(intIdx) Then Print #intFF, List1.List(intIdx) Next Close #intFF
Doesnt work :
VB Code:
Private Sub cmdStart_Click() 'load names Dim strBuff As String 'Clear the ListBox contents List1.Clear intFF = FreeFile Open App.Path & "\load.txt" For Input As #intFF Do Until EOF(1) Line Input #intFF, strBuff List1.AddItem strBuff Loop Close #intFF Label3.BackColor = &HFF00& 'remove excess Dim strString As String Dim arrString() As String For intIdx = 0 To intCount strString = List1.Selected(intIdx) strString = Replace(strString, "(at)", "@") arrString = Split(strString, " ") List1.Selected(intIdx) = arrString(0) Next Label4.BackColor = &HFF00& 'replace text Label5.BackColor = &HFF00& 'save new file intFF = FreeFile intCount = List1.ListCount - 1 Open App.Path & "\emails.txt" For Output As #intFF For intIdx = 0 To intCount If List1.Selected(intIdx) Then Print #intFF, List1.List(intIdx) Next Close #intFF Label6.BackColor = &HFF00& End Sub
Hack is right about the selection BTW, if this is all in one sub then there will never be any selected items because you clear and reload the List. I had thought that you might have combined code from a couple different subs in your post (the Dim statements throughout the code block threw me off).
So it now looks like this but still doesnt work :
VB Code:
Private Sub cmdStart_Click() loadlist replace saveit End Sub Private Sub loadlist() Dim strBuff As String 'Clear the ListBox contents List1.Clear intFF = FreeFile Open App.Path & "\load.txt" For Input As #intFF Do Until EOF(1) Line Input #intFF, strBuff List1.AddItem strBuff Loop Close #intFF Label3.BackColor = &HFF00& End Sub Private Sub replace() 'remove excess Dim strString As String Dim arrString() As String For intIdx = 0 To intCount strString = List1.Selected(intIdx) strString = replace(strString, "(at)", "@") arrString = Split(strString, " ") List1.Selected(intIdx) = arrString(0) Next Label4.BackColor = &HFF00& 'replace text Label5.BackColor = &HFF00& End Sub Private Sub saveit() 'save new file intFF = FreeFile intCount = List1.ListCount - 1 Open App.Path & "\emails.txt" For Output As #intFF For intIdx = 0 To intCount If List1.Selected(intIdx) Then Print #intFF, List1.List(intIdx) Next Close #intFF Label6.BackColor = &HFF00& End Sub
After your Replace sub has run, what are the contents of your listbox?
I dont know because there is an error in this line
VB Code:
strString = replace(strString, "(at)", "@")
What is the error?Quote:
Originally Posted by Steve_F
Wrong number of arguments or invalid property assignment on the word replace
Well, this was confusing until I released that you can't call a Sub Replace.
Replace is a VB reserved word, so rename your sub to something like ReplaceString.
2 things. First, you need to either let the user select items from the list before you output it or just output the whole list. This means you need to load the list seperate from where you output it. Second, you should avoid using VB function names for your own functions. The code you just posted probably won't compile because your replace function doesn't accept arguments and you are calling it recursively. Try this:
VB Code:
Private Sub cmdStart_Click() DoReplace SaveIt End Sub Private Sub Form1_Load() 'Or where you want to load it. Dim strBuff As String, intFF As Integer List1.Clear intFF = FreeFile Open App.Path & "\load.txt" For Input As #intFF Do Until EOF(intFF) Line Input #intFF, strBuff List1.AddItem strBuff Loop Close #intFF End Sub Private Sub DoReplace() Dim intIdx As Integer For intIdx = 0 To List1.ListCount - 1 If List1.Selected(intInx) Then List1.List(intIdx) = Replace(Split(strString, " ")(0), "(at)", "@", , , vbTextCompare) End If Next End Sub Private Sub SaveIt() 'save new file Dim intFF As Integer, intIdx As Integer intFF = FreeFile Open App.Path & "\emails.txt" For Output As #intFF For intIdx = 0 To List1.ListCount - 1 If List1.Selected(intIdx) Then Print #intFF, List1.List(intIdx) Next Close #intFF End Sub
That code runs without errors but when I check the file it doesnt save. it does create a file called emails.txt if it doesnt exist but theres nothing in it.
Now that it runs with no errors, lets get back to my question.Quote:
Originally Posted by Steve_F
Before creating emails.txt, do you, in fact, have something in your listbox?
Nope
Thats why your text file is empty. Your routine dumps the contents of your listbox into your text file. So, the problem is not with your emails.txt routine, it is with the routine that populates your listbox in the first place.Quote:
Originally Posted by Steve_F
We are going to step through the process.
I want you to put a command button on your form (it doesn't matter where, it is going to get deleted after we are done anyway).
In the command button, call your LoadIt proceed.
Does your listbox get populated with the contents of load.txt?
If yes, then take LoadIt out of the click event of your command button and call your replace procedure. Does your listbox get repopulated with the stripped down strings.
Yes it loads. When I try and replace it doesnt work. The list is empty
Outstanding. We are making progress.Quote:
Originally Posted by Steve_F
I'm going to play with the replace part of the listbox. I'll be back.
THank you
Isnt the error in this line
VB Code:
List1.List(intIdx) = replace(Split(strString, " ")(0), "(AT)", "@", , , vbTextCompare)
Shouldnt strString be List1.List(intIdx)??
Try this. It took a little dickering around, but I think I have itVB Code:
On Error Resume Next Dim strString As String Dim arrString() As String Dim i As Long For i = 0 To List1.ListCount - 1 strString = List1.List(i) strString = Replace(strString, "(AT)", "@") arrString = Split(strString, " ") List1.RemoveItem i List1.AddItem arrString(0) Next List1.Refresh
I put that in the doreplace and it doesnt work
:confused: It worked for me. What does it do for you?Quote:
Originally Posted by Steve_F
Yep, my bad.Quote:
Originally Posted by Steve_F