Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Fast way to truncate string

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Fast way to truncate string

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
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

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




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Fast way to truncate string

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%
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

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


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Fast way to truncate string

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
  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Fast way to truncate string

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
  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

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, ...).


I agree! My bad for not using...

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

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


  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Fast way to truncate string

"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



  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

"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


  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

Added 'type' to the args as well...


Function TruncateString$(sText$, sFind$, Optional lStart& = 1)
TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart)
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


  #15   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Fast way to truncate string

"GS" wrote:

I went with Mid$() so you could start anywhere. Just bear in mind the
start position MUST be before the end position!


Yup, that makes sense. Setting "lStart" equal to 1 by default seems
to be the best way. 8)





  #16   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Fast way to truncate string

"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



  #17   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Fast way to truncate string

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
  #18   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Fast way to truncate string

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







  #19   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Fast way to truncate string

Hi Robert,

Am Tue, 31 Mar 2015 01:54:51 -0700 schrieb Robert Crandal:

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


drag vData and sClean into the watch window and step with F8 through
your code.
What about the borders? Are there 20 asterix as in the question before?
Then try:
strSplit = Application.Rept("*", 20)
vData = Split(ReadTextFile(sFile1), String(80,strSplit))


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #20   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

What about the borders? Are there 20 asterix as in the question
before?
Then try:
strSplit = Application.Rept("*", 20)
vData = Split(ReadTextFile(sFile1), String(80,strSplit))


This suggests each paragraph is delimited by 1600 asterisks! If 20
asterisks like before then...

vData = Split(ReadTextFile(sFile1), String(20, "*"))

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




  #21   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

Try using this optimized version I posted earlier...

Function TruncateString$(sText$, sFind$, Optional lStart& = 1)
TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart)
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


  #22   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Fast way to truncate string

"GS" wrote:
Try using this optimized version I posted earlier...

Function TruncateString$(sText$, sFind$, Optional lStart& = 1)
TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart)
End Function


This function causes the following error:

Compile error:
ByRef argument type mismatch

Seems like the problem is related to the data being passed
into the "sText$" parameter, but I'm not sure.



  #23   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Fast way to truncate string

"Claus Busch" wrote:

drag vData and sClean into the watch window and step with F8 through
your code.
What about the borders? Are there 20 asterix as in the question before?
Then try:
strSplit = Application.Rept("*", 20)
vData = Split(ReadTextFile(sFile1), String(80,strSplit))


I have been testing different border sizes, such as 20 and 80. I
didn't think this would affect the code because the Split() function
discards the borders and only stores each inner string into an
array.

The main data types being used in my code are variant, array, and
string. It seems there is a problem with the data types being passed into
the TruncateString() function, but I'm not sure.




  #24   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

"GS" wrote:
Try using this optimized version I posted earlier...

Function TruncateString$(sText$, sFind$, Optional lStart& = 1)
TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart)
End Function


This function causes the following error:

Compile error:
ByRef argument type mismatch

Seems like the problem is related to the data being passed
into the "sText$" parameter, but I'm not sure.


Data type was changed from Variant in the original version to String as
last posted (shown here)...

Sub Truncate()
Const s$ = "Always eat lots of fruits and vegetables every day."
Const f$ = "and"
Dim sz$, lPos&
lPos = InStr(s, "eat")
sz = TruncateString(s, f, lPos): Debug.Print sz
End Sub

Function TruncateString$(sText$, sFind$, Optional lStart& = 1)
TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart)
End Function

...which returns "eat lots of fruits".

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #25   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Fast way to truncate string

"GS" wrote:

Data type was changed from Variant in the original version to String as
last posted (shown here)...


How do I fix this? Most of the data that I'm working with are strings.
I have tried using different combinations of CStr() or CVar(), and I
even tried changing your function declaration to the following:

Function TruncateString$(sText$ as String, sFind$, '.....)
or
Function TruncateString$(ByVal sText$ as String, sFind$, '.....)

The various errors that I'm getting a
"Invalid procedure call or argument"
"Compile error: ByRef argument type mismatch"

Again, here was the code that I was testing:
'--------------------------------------------------------------------------------
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 occurs here *********
'
Print #i, TruncateString(sClean(j), "especially so")

Next ' j

Next ' n
Close #i

End Sub






  #26   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

"GS" wrote:

Data type was changed from Variant in the original version to
String as last posted (shown here)...


How do I fix this? Most of the data that I'm working with are
strings.
I have tried using different combinations of CStr() or CVar(), and I
even tried changing your function declaration to the following:

Function TruncateString$(sText$ as String, sFind$, '.....)
or
Function TruncateString$(ByVal sText$ as String, sFind$, '.....)

The various errors that I'm getting a
"Invalid procedure call or argument"
"Compile error: ByRef argument type mismatch"


I have Lou Gehrig's and so I use data type symbols to save typing...

$ = As String
& = As Long
% = As Integer
# = As Double
! = As Single
@ = As Currency

...and the latest version function works with sample data typed as shown
in the Test_ sub.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #27   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

gsBlankLine = Chr(13) & Chr(13) & Chr(10)

Where & how have you declared gsBlankLine?

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #28   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Fast way to truncate string

"GS" wrote:
gsBlankLine = Chr(13) & Chr(13) & Chr(10)

Where & how have you declared gsBlankLine?


It is declared globally as:

Dim gsBlankLine$


  #29   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

"GS" wrote:
gsBlankLine = Chr(13) & Chr(13) & Chr(10)

Where & how have you declared gsBlankLine?


It is declared globally as:

Dim gsBlankLine$


Better that it be...

Public gsBlankLine$

...else it's scope using Dim makes it module level.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #30   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

"GS" wrote:

Data type was changed from Variant in the original version to
String as last posted (shown here)...


How do I fix this? Most of the data that I'm working with are
strings.
I have tried using different combinations of CStr() or CVar(), and I
even tried changing your function declaration to the following:

Function TruncateString$(sText$ as String, sFind$, '.....)
or
Function TruncateString$(ByVal sText$ as String, sFind$, '.....)

The various errors that I'm getting a
"Invalid procedure call or argument"
"Compile error: ByRef argument type mismatch"

Again, here was the code that I was testing:
'--------------------------------------------------------------------------------
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 occurs here *********
'
Print #i, TruncateString(sClean(j), "especially so")

Next ' j

Next ' n
Close #i

End Sub


Why are you writing one line at a time? Why not edit the array
elements, then 'dump' its contents into a file in 1 shot???

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




  #31   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Fast way to truncate string

I was able to duplicate the exception by using a word that wasn't in
the string being truncated. That precludes that you need to know the
contents of the string being truncated contains the search string...

Function TruncateString$(sText$, sFind$, Optional lStart& = 1)
If InStr(sText, sFind) 0 Then
'Truncate
TruncateString = Mid$(sText, lStart, InStr(sText, sFind) - lStart)
Else
'Return original string
TruncateString = sText
End If
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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
TRUNCATE SPACE FROM NUMERICAL STRING eg "33 033 546" NAEEM Excel Worksheet Functions 8 December 5th 06 07:30 PM
Truncate string bwmaher Excel Programming 3 November 8th 06 07:11 PM
Truncate string bwmaher Excel Programming 0 November 8th 06 01:17 PM
How do you truncate a text string? dailygluttony Excel Worksheet Functions 2 March 28th 06 08:08 PM
Any fast method to parse a string into row & col information Nick Excel Programming 3 August 19th 04 06:47 PM


All times are GMT +1. The time now is 08:15 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"