Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Suppose I have the following string.
Dim s as String s = "Always eat lots of fruits and vegetables every day." I would like to be able to truncate this string as quickly as possible, beginning from a word of my choosing, all the way to the end of the string. So, if I choose the word "and" as my target word, the above string should be transformed into: "Always eat lots of fruits " Essentially, all text after the first occurence of "and" was removed from the string. I'm looking for solution that runs quickly. Thanks! ~Rob |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Suppose I have the following string.
Dim s as String s = "Always eat lots of fruits and vegetables every day." I would like to be able to truncate this string as quickly as possible, beginning from a word of my choosing, all the way to the end of the string. So, if I choose the word "and" as my target word, the above string should be transformed into: "Always eat lots of fruits " Essentially, all text after the first occurence of "and" was removed from the string. I'm looking for solution that runs quickly. Thanks! ~Rob Try... s = Mid(s, 1, InStr(s, "and") - 1) OR s = TruncateString(s, "and") Function TruncateString(sText, sFind) TruncateString = Mid(sText, 1, InStr(sText, sFind) - 1) End Function -- Garry Free usenet access at http://www.eternal-september.org Classic VB Users Regroup! comp.lang.basic.visual.misc microsoft.public.vb.general.discussion |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Better function...
Function TruncateString(sText, sFind, Optional lStart& = 1) TruncateString = Mid(sText, lStart, InStr(sText, sFind) - lStart) End Function ...where you can specify the start point as well as the end point... s = TruncateString(s, "and", InStr(s, "eat")) -- Garry Free usenet access at http://www.eternal-september.org Classic VB Users Regroup! comp.lang.basic.visual.misc microsoft.public.vb.general.discussion |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
As Maurizio pointed out.., here's a version optimized for VB[A} use.
Function TruncateString$(sText, sFind, Optional lStart& = 1) TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart) End Function The original version posted was intended for use with VBScript as well as VB[A}. The 2nd version not for VBScript because lStart was 'typed'! -- Garry Free usenet access at http://www.eternal-september.org Classic VB Users Regroup! comp.lang.basic.visual.misc microsoft.public.vb.general.discussion |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
"GS" wrote:
As Maurizio pointed out.., here's a version optimized for VB[A} use. Excellent. I figured the best way would involve using the Instr() function in conjunction with Mid() or Left() or whatever. Thanks again Gary and Maurizio |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
"GS" wrote:
As Maurizio pointed out.., here's a version optimized for VB[A} use. Excellent. I figured the best way would involve using the Instr() function in conjunction with Mid() or Left() or whatever. Thanks again Gary and Maurizio I went with Mid$() so you could start anywhere. Just bear in mind the start position MUST be before the end position! -- Garry Free usenet access at http://www.eternal-september.org Classic VB Users Regroup! comp.lang.basic.visual.misc microsoft.public.vb.general.discussion |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
"GS" wrote:
Function TruncateString$(sText, sFind, Optional lStart& = 1) TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart) End Function Do you know why I'm getting a "Type mismatch" error with this code?: '-------------------- Public Function TruncateString$(sText, sFind, Optional lStart& = 1) TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart) End Function '-------------------- Sub TestTruncate() Dim i As Integer Dim sFilename As String sFilename = "C:\out.txt" sPhrases = Split("Hello world", "Goodbye world", "Not a good world today") i = FreeFile() Open sFilename For Output As #i Print #i, TruncateString$(sPhrases(0), "world") Close #i End Sub |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Robert,
Am Sun, 29 Mar 2015 03:03:03 -0700 schrieb Robert Crandal: sPhrases = Split("Hello world", "Goodbye world", "Not a good world today") try: sphrases = Split("Hello world, Goodbye world, Not a good world today", ", ") or sphrases = Array("Hello world", "Goodbye world", "Not a good world today") Regards Claus B. -- Vista Ultimate / Windows7 Office 2007 Ultimate / 2010 Professional |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Okay, I have a different problem with the TruncateString()
function. I am getting an "Invalid procedure call or argument" message. Not sure why. Here is the code: (The error occurs in the main_test() subroutine) '---------------------------------------------------- Sub main_test() Dim sFile1 As String Dim sFile2 As String Dim n As Integer Dim i As Integer Dim j As Integer Dim vData ' variant gsBlankLine = Chr(13) & Chr(13) & Chr(10) sFile1 = "C:\inp.txt" sFile2 = "C:\out.txt" 'Group directly into chapters divided by "*" borders vData = Split(ReadTextFile(sFile1), String(80, "*")) i = FreeFile() Open sFile2 For Output As #i ' Scan all narratives For n = LBound(vData) To UBound(vData) sClean = Split(vData(n), gsBlankLine) For j = LBound(sClean) To UBound(sClean) ' ' ******** Error occurrs here ********* ' Print #i, TruncateString(sClean(j), "especially so") Next ' j Next ' n Close #i End Sub '------------------------- Public Function ReadTextFile$(Filename$) ' Reads large amounts of data from a text file in one single step. Dim iNum% On Error GoTo ErrHandler iNum = FreeFile(): Open Filename For Input As #iNum ReadTextFile = Space$(LOF(iNum)) ReadTextFile = Input(LOF(iNum), iNum) ErrHandler: Close #iNum: If Err Then Err.Raise Err.Number, , Err.Description End Function 'ReadTextFile() '---------------------------- Public Function TruncateString$(sText, sFind, Optional lStart& = 1) TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart) End Function |
#10
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Il giorno venerdì 27 marzo 2015 13:03:47 UTC+1, Robert Crandal ha scritto:
Suppose I have the following string. Dim s as String s = "Always eat lots of fruits and vegetables every day." I would like to be able to truncate this string as quickly as possible, beginning from a word of my choosing, all the way to the end of the string. So, if I choose the word "and" as my target word, the above string should be transformed into: "Always eat lots of fruits " Essentially, all text after the first occurence of "and" was removed from the string. I'm looking for solution that runs quickly. Thanks! Hi, Try Left$(k, InStr(1, k, f, vbTextCompare) - 1) -- Ciao! :) Maurizio |
#11
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Il giorno venerdì 27 marzo 2015 13:03:47 UTC+1, Robert Crandal ha
scritto: Suppose I have the following string. Dim s as String s = "Always eat lots of fruits and vegetables every day." I would like to be able to truncate this string as quickly as possible, beginning from a word of my choosing, all the way to the end of the string. So, if I choose the word "and" as my target word, the above string should be transformed into: "Always eat lots of fruits " Essentially, all text after the first occurence of "and" was removed from the string. I'm looking for solution that runs quickly. Thanks! Hi, Try Left$(k, InStr(1, k, f, vbTextCompare) - 1) Given the OP's criteria is "..as quickly as possible,...", the Mid() function is considerably faster! -- Garry Free usenet access at http://www.eternal-september.org Classic VB Users Regroup! comp.lang.basic.visual.misc microsoft.public.vb.general.discussion |
#12
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Il giorno venerdì 27 marzo 2015 15:24:55 UTC+1, GS ha scritto:
Il giorno venerdì 27 marzo 2015 13:03:47 UTC+1, Robert Crandal ha scritto: Suppose I have the following string. Dim s as String s = "Always eat lots of fruits and vegetables every day." I would like to be able to truncate this string as quickly as possible, beginning from a word of my choosing, all the way to the end of the string. So, if I choose the word "and" as my target word, the above string should be transformed into: "Always eat lots of fruits " Essentially, all text after the first occurence of "and" was removed from the string. I'm looking for solution that runs quickly. Thanks! Left$(k, InStr(1, k, f, vbTextCompare) - 1) Given the OP's criteria is "..as quickly as possible,...", the Mid() function is considerably faster! ' Windows 7/64 - Excel 2013 ' Option Explicit Private Declare Function GetTickCount Lib "kernel32" () As Long Private Sub Test() Const DebugOn = False Const k = "Always eat lots of fruits and vegetables every day." Const f = "And" Dim i As Long Dim i1 As Long Dim t As Long Dim t1 As Long Dim d As Long Dim s As String If DebugOn Then t = 1 t1 = 1 Else t = 100000 t1 = 5 End If For i1 = 1 To t1 If DebugOn Then Debug.Print "Split:", "'" & Split(k, f, Compa=vbTextCompare)(0) & "'" Else d = GetTickCount For i = 1 To t s = Split(k, f, Compa=vbTextCompare)(0) Next d = GetTickCount - d Debug.Print Format$(d, "0") ' End If If DebugOn Then Debug.Print "Mid$:", "'" & Mid$(k, 1, InStr(1, k, f, vbTextCompare) - 1) & "'" Else d = GetTickCount For i = 1 To t s = Mid$(k, 1, InStr(1, k, f, vbTextCompare) - 1) Next d = GetTickCount - d Debug.Print Format$(d, "0") ' End If If DebugOn Then Debug.Print "Left$:", "'" & Left$(k, InStr(1, k, f, vbTextCompare) - 1) & "'" Else d = GetTickCount For i = 1 To t s = Left$(k, InStr(1, k, f, vbTextCompare) - 1) Next d = GetTickCount - d Debug.Print Format$(d, "0") ' End If If DebugOn Then Debug.Print "Mid:", "'" & Mid(k, 1, InStr(1, k, f, vbTextCompare) - 1) & "'" Else d = GetTickCount For i = 1 To t s = Mid(k, 1, InStr(1, k, f, vbTextCompare) - 1) Next d = GetTickCount - d Debug.Print Format$(d, "0") ' End If If DebugOn Then Debug.Print "Left:", "'" & Left(k, InStr(1, k, f, vbTextCompare) - 1) & "'" Else d = GetTickCount For i = 1 To t s = Left(k, InStr(1, k, f, vbTextCompare) - 1) Next d = GetTickCount - d Debug.Print Format$(d, "0") ' End If Debug.Print Next End Sub Split Mid$ Left$ Mid Left 187 109 109 109 110 218 109 94 109 125 203 93 93 110 109 218 94 109 109 125 187 109 109 109 110 187 94 109 109 109 187 109 109 110 109 187 94 109 109 110 187 109 109 109 110 187 110 93 109 110 187 109 94 125 109 187 109 94 124 110 187 109 94 124 110 172 94 109 109 110 187 94 109 109 109 187 93 110 109 109 187 109 94 124 110 187 109 94 109 125 187 110 93 109 110 187 109 94 109 125 Min : 172 93 93 109 109 Max : 218 110 110 125 125 Average : 190,15 103,75 101,40 112,15 112,70 Rank : 5 2 1 3 4 0,0000% -45,4378% -46,6737% -41,0202% -40,7310% |
#13
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Given the simplicity of your test, this results about 50/50 on my
machine after 5 runs. No surprise! Generally speaking, though, the Mid() function is considered to be faster in the VB community forums. Having tested with more complex uses, I agree with that assessment! -- Garry Free usenet access at http://www.eternal-september.org Classic VB Users Regroup! comp.lang.basic.visual.misc microsoft.public.vb.general.discussion |
#14
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Il giorno venerdì 27 marzo 2015 18:11:08 UTC+1, GS ha scritto:
Given the simplicity of your test, this results about 50/50 on my machine after 5 runs. No surprise! Generally speaking, though, the Mid() function is considered to be faster in the VB community forums. Having tested with more complex uses, I agree with that assessment! Anyway, string functions (Mid$, Left$, ...) are faster then variant. -- Ciao! :) Maurizio |
#15
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Il giorno venerdì 27 marzo 2015 18:11:08 UTC+1, GS ha scritto:
Given the simplicity of your test, this results about 50/50 on my machine after 5 runs. No surprise! Generally speaking, though, the Mid() function is considered to be faster in the VB community forums. Having tested with more complex uses, I agree with that assessment! Anyway, string functions (Mid$, Left$, ...) are faster then variant (Mid, Left, ...). -- Ciao! :) Maurizio |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
TRUNCATE SPACE FROM NUMERICAL STRING eg "33Â 033Â 546" | Excel Worksheet Functions | |||
Truncate string | Excel Programming | |||
Truncate string | Excel Programming | |||
How do you truncate a text string? | Excel Worksheet Functions | |||
Any fast method to parse a string into row & col information | Excel Programming |