I have a few dumb questions. Keep in mind, im
not really a VBA expert. So here goes....
"GS" wrote:
Sub TrimNewlineChars2()
Dim sFile$, vData, n&
Const sFilterFlag$ = "~"
'Prompt for the file
sFile = Application.GetOpenFilename
If sFile = Empty Then Exit Sub
vData = Split(ReadTextFile(sFile), vbCrLf)
At this point in the code, the variable "vData"
is a variant type or array, is that right?
ReadTextFile returns a string value. The Split() function returns a
zero-based array. vData is not 'typed' and so by default is type
Variant, meaning it can hold various value type[s]. Assigning the
result of Split() makes vData an array because its type is Variant.
IOW, vData was not declared as an array like so...
Dim vData()
...whereby the parenthesis would def it as an empty (no dims) array.
But....the variable vData(1) is of type string,
is that right?
In this case yes because the entire array contains text from a text
file.
vData(1) is a single element in the vData array. Each element's
position is referred to as its array index.
I'm wondering, at this point in the above code,
is it possible to run the following code?
' Get vData(1) string
myStr = vData(1)
' Put string back in vData, but preserve
' the original variant or array type? Possible?
vData = myStr
Your code sample replaces the array with a string. To preserve the
structure of the array you must ref the element by index...
myStr = vData(1)
'edit myStr as desired
vData(1) = myStr
...exactly as done in the code below to place the FilterFlag in empty
elements so the Filter() function can remove those elements and return
a resized array consisting of UBound(vData) minus (#elements with
FilterFlags).
' Resume rest of code below
'Flag leading/trailing linefeeds
For n = LBound(vData) To UBound(vData)
If vData(n) = Empty Then vData(n) = sFilterFlag _
Else Exit For
Next 'n
For n = UBound(vData) To LBound(vData) Step -1
If vData(n) = Empty Then vData(n) = sFilterFlag _
Else Exit For
Next 'n
The following line should read as...
'Return value with leading/trailing linefeeds *removed*
The following line could read as...
Selection.Value = Join(Filter(vData, sFilterFlag, False), vbCrLf)
...because Excel with auto-magically remove the Cr. Lf on its own is not
desireable for a multi-line output to a text file/control. There are
cases, though, where Excel does not auto-magicically remove Cr. For
example, multi-line contents of a textbox being used in a header must
have the Cr removed beforehand.
End 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