defonic
08-09-2009, 06:25 PM
Hey guys, having a little trouble completing some code. I am trying to loop through a listbox and find/replace text. Thats the easy part, I already got that. I need to do something to the text found then replace it like so, I load a text file into a listbox, here is a partial:
sometext0|sometext1|20 Minutes|sometext2
sometext0|sometext1|12 Minutes|sometext2
sometext0|sometext1|8 Minutes|sometext2
sometext0|sometext1|16 Minutes|sometext2
I need to be able to find the minutes, multiply it by 60, replace the original number and remove the white space and the word 'Minutes' like so:
sometext0|sometext1|1200|sometext2
sometext0|sometext1|720|sometext2
sometext0|sometext1|480|sometext2
sometext0|sometext1|960|sometext2
Any help is appreciated, my brain hurts! I am probably overlooking something simple.
Cerian Knight
08-09-2009, 09:35 PM
Can you show us some code you have tried? Then maybe we can help fix it.
If the example you gave is typical, it might be more efficient to use the Split command to position the items in an array. That presumes the text came from somewhere else before you loaded it into the listbox. Then you can access the elements easily (especially if they are at regular intervals) in order to change them.
defonic
08-10-2009, 11:20 AM
Thanks, I actually figured it out after, well... all day!
I ended up using a GetLeft/GetRight Function to find that string next to the word 'minutes' then did some replacing like so:
Dim i
Dim a, b, c, X, Y, Z, strMins As String
For i = 0 To lstFound.ListCount - 1
X = GetLeftWord(lstFound.List(i), " minutes", False)
Y = GetRightWord(X, "|", True)
Z = Y * 60
strMins = "minutes"
a = lstFound.List(i)
b = Replace(a, Y, Z)
c = Replace(b, strMins, "")
lstDone.AddItem c & "|" & txtCateg
Next i
Everything now works to my standards except trying to load a 10,000 line text file into a ListBox is VERY SLOW! I have tried making it Visible=False then Visible=True when done but it is still slow!
Thanks for the help!
Cerian Knight
08-10-2009, 01:20 PM
Loading 32000 items into a ListBox on a modern computer takes ~0.5 second without hiding and ~0.35 second with hiding. So the overhead must be in your Get or the Replace function usage.
However, first you should correct your variable declarations to be explicit. In your example, the only variable that is not a Variant is 'strMins'. Each Variant used exacts its own toll on performance. Do this instead:
Dim I as Integer 'or Long
Dim a as String, b as String, c as String, X as String, Y as String, Z as String
jerome_gail26
08-10-2009, 07:40 PM
Everything now works to my standards except trying to load a 10,000 line text file into a
ListBox is VERY SLOW! I have tried making it Visible=False then
Visible=True when done but it is still slow!
I try to make a function that convert it to second and with give me the result in just second.
I use split and replace funtion.
Private Function ConvertToSecond(strToSplit As String, _
Delimeter As String) As String
Dim StrSplited() As String, strSecond As Long
StrSplited = Split(strToSplit, Delimeter)
strSecond = Val(Replace(StrSplited(2), " minutes", "")) * 60
ConvertToSecond = StrSplited(0) & "|" & StrSplited(1) & "|" & _
strSecond & "|" & StrSplited(3)
End Function
Good Luck,
Jerome
I you want to view the said project see the attached file.