Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Remove newline char from string

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


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
table column name with newline, cannot select with string [email protected] Excel Programming 1 February 19th 14 07:12 PM
Remove the char from right Ranjith Kurian[_2_] Excel Worksheet Functions 2 October 13th 09 01:41 PM
How to substitute a comma with a newline char using Replace. edspyhill01 Excel Discussion (Misc queries) 9 July 30th 06 12:38 AM
last char of a string Buddy Lee Excel Programming 9 November 11th 05 02:02 AM
How do I remove Char(160) from a cell? Dave Excel Worksheet Functions 2 February 3rd 05 02:06 PM


All times are GMT +1. The time now is 04:57 PM.

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

About Us

"It's about Microsoft Excel"