Function declaration error?

P

Pista

Hello.

I'm trying to create a function, this is the code:

Private Function remRet(Dim myText As String)
For i = 0 To Len(myText)
If myText(i) = vbCrLf Or vbCr Then
myText(i) = " "
Next i
remRet = myText
End Function

And I get a compile error: Expected identifier - (by function argument
(array))
I am desperate :) Thank you all for helping me
Pista Holiga
 
P

Pista

Hi Paul

I've already tried it...

I get compile error: Expected array (myText(i) selected)

Do you have another idea?

Thank you,

Pista
 
P

Paul Herber

Hi Paul

I've already tried it...
I get compile error: Expected array (myText(i) selected)
Do you have another idea?

change
For i = 0 To Len(myText)
to
For i = 0 To Len(myText)-1
 
N

Nikolay Belyh

Maybe your question is, "how do I remove line breaks from a string?"
In this case you can just use built-in Replace function:

myText = Replace(myText, vbCr, " ")
myText = Replace(myText, vbLf, " ")

Your code does not compile, because strings cannot be indexed (like
char arrays) in VB.
To work with strings use functions like Mid(), Replace(), etc.


You might find Visio's help on VBA language useful.
It is available in Visio if you click F1 key in VBA editor.

Kind regards, Nikolay.
 
D

David Parker

Surely:
If myText(i) = vbCrLf Or vbCr Then

should read:
If myText(i) = vbCrLf Or myText(i) = vbCr Then
 
N

Nikolay Belyh

Surely:
If myText(i) = vbCrLf Or vbCr Then

should read:
If myText(i) = vbCrLf Or myText(i) = vbCr Then

Ehr..?
I thought one cannot index a string in VB (see the compiler error) ?!

Kind regards, Nikolay.
 
A

Al Edlund

I'd probably try something as unsophisticated as
al

Public Function ReplaceCR(strIn As String) As String

Dim strOut As String

strOut = strIn
strOut = Replace(strOut, Chr(vbCrLf), "")
strOut = Replace(strOut, Chr(vbCr), "")

ReplaceCR = strOut

End Function
 
N

Nikolay Belyh

I'd probably try something as unsophisticated as

Ehr..?
It seems that the question is simple enough to knock everyone down.. :-
D
"Chr(vbCr)" leads to compiler error "Type mismatch" .. :-D

Kind regards, Nikolay.
 
A

Al Edlund

sorry about that.
Since the constant vbCrLf is already chr(10) & chr(13), the correct form
would be

' just get rid of cr's
Public Function ReplaceCR(StrIn As String) As String

Dim strOut As String

strOut = StrIn
strOut = Replace(strOut, vbCrLf, " ")
strOut = Replace(strOut, vbCr, " ")

ReplaceCR = strOut

End Function

' eliminate all control characters in the string
Public Function ReplaceCtrl(StrIn As String) As String

Dim strOut As String
Dim strTemp As String
Dim intX As Integer

For intX = 1 To Len(StrIn)
strTemp = Mid(StrIn, intX, 1)
' since most of your controls are integer 30 or less
If 30 < Asc(strTemp) Then
strOut = strOut & strTemp
End If
Next intX

ReplaceCtrl = strOut


End Function


Public Sub TestReplace()

Dim strOut1 As String
Dim strOut2 As String
Dim StrIn As String

StrIn = "abc def " & vbCrLf & "ghi"

strOut1 = ReplaceCR(StrIn)

MsgBox strOut1

strOut2 = ReplaceCtrl(StrIn)

MsgBox strOut2

End Sub


Al
 
P

Pista

:))

sorry for that simple question, but thanks to you all for your help!
Replace() works fine...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Macro help 0
Seaching an excel sheet 1
Function 0
Word to Excel Table Conversion 1
Convert Word tables to Excel 0
Word to Excel Table conversion 0
Convert Word Tables to Excel 1
Set a range on a variable sheet? 2

Top