I am importing CSV file with multiple lines where the end of the line is a vbCrLf.
The problem is that sometimes I am getting into the fields with the value vbLf.
I would just like to replace the vbLf.
When i use the code below replaces the vbLf and vbCrLf as indicating the end of the line:
txt = txt.Replace ( vbLf , "")
How do I replace only the vbLf?
For anyone in the future:
If the line ends or starts with vbLf(Chr(10)) you can just TRIM each line, like so:
str = str.TrimStart(Chr(10))
str = str.Trim(Chr(10))
str = str.TrimEnd(Chr(10))
Related
This code specifies the name with which to save Word documents.
ActiveDocument.SaveAs FileName:=MyString & Mystring2 & MyString3 & MyString4
I am trying to remove special characters from a value I've added to the file name.
It works before the final step of removing special characters from MyString4.
MyString4 is Document.Bookmarks("Index").Range.Text
I'm trying to declare the whole line as a separate variable.
Dim Mystring
MyString4 = Document.Bookmarks("Index").Range.Text
and then use the Replace command to get rid of some specific characters.
Unfortunately, it will not let me declare the line as a separate variable. The files get saved without MyString4 at the end.
The simplest solution is to replace
MyString & Mystring2 & MyString3 & MyString4
with a call to a function that does the necessary replacments
SafeFileName(MyString & Mystring2 & MyString3 & MyString4)
Using
Public Function SafeFileName(ByVal ipFileName as String) as String
' Do as many replaces as you need
ipFileName=Replace(ipFileNAme, <your find text 1>, <your replace text>
ipFileName=Replace(ipFileNAme, <your find text 2>, <your replace text>
ipFileName=Replace(ipFileNAme, <your find text 3>, <your replace text>
SafeFileName = ipFilename
End Function
You can be smarter by using a regexp in your function but this might be a job for another day.
For example, the following replaces illegal filename characters with underscores:
Dim i As Long: Const StrExcl As String = """*./\:?|"
StrName = MyString & Mystring2 & MyString3 & MyString4
For i = 1 To Len(StrExcl)
StrName = Replace(StrName, Mid(StrExcl, i, 1), "_")
Next
StrName = Trim(StrName)
ActiveDocument.SaveAs FileName:=StrName & ".docx"
I am using following code to read csv file and finally the data is converted into array.
Function Sample(strPath) As String()
Dim MyData As String, strData() As String
Open strPath For Binary As #1
MyData = Space$(LOF(1))
Get #1, , MyData
Close #1
Sample = Split(MyData, vbCrLf)
End Function
Following code convert each comma separated line into array.
SaleRows = Sample(filepath)
For Each SaleRow In SaleRows
SaleRowArray = Split(SaleRow, ",")
Next SaleRow
Everything works fine but when a cell contains comma, my above function fails. Is there any way to handle comma in a particular cell?
Sample csv
Please observe comma in second row.
When I get array from the comma record, I get below.
It's likely that the strings in your CSV file are escaped with quote marks. If you want to parse the CSV using your own code, then your code needs to be able to ignore commas which occur inside quotes.
A quick way to do this would be to use RegEx. You could try the following:
Private Function changeDelimiter(line as String) as String
Dim regEx As Object
Set regEx = CreateObject("VBScript.regexp")
regEx.IgnoreCase = True
regEx.Global = True
regex.Pattern = ",(?=([^" & Chr(34) & "]*" & Chr(34) & "[^" & Chr(34) & "]*" & Chr(34) & ")*(?![^" & Chr(34) & "]*" & Chr(34) & "))"
changeDelimiter = regex.Replace(line, "#|#")
End Function
Then change the Split() line in your code as follows:
SaleRowArray = Split(changeDelimiter(SaleRow), "#|#")
The function replaces all commas outside quote marks with #|# (which I assume won't ever come up in your input data). You then split the lines into data fields on #|# instead of comma.
I have a query that I execute through VBA in Access 2010. The result of the query should be AFR, but it returns AFR with an extra line below it. I have added the "'" character to make the extra line visible.
TempHold = rs.GetString
Debug.Print "'" & TempHold & "'"
Returns this:
'AFR
'
But should return this:
'AFR'
I have tried using the below code, but none of the If statements evaluate as True. The code should check for a " ", a vbNewLine, or vbCrLf character but none evaluate as true. Does anyone know of any additional characters that would result in a new line?
If Right(TempHold, 1) = " " Then
TempHold = Left(TempHold, Len(TempHold) - 1)
ElseIf Right(TempHold, 2) = vbNewLine Or Right(TempHold, 2) = vbCrLf Then
TempHold = Left(TempHold, Len(TempHold) - 2)
End If
Use:
Asc(Right(TempHold, 1))
to get the Ascii character code.
Once you've found the character code (which, as you wrote in your comment, was 13), you can use your code to remove it:
If Right(TempHold, 1) = Chr(13) Then
TempHold = Left(TempHold, Len(TempHold) - 1)
End If
In this case, you can also use vbCr, which is the same as Chr(13).
The best way to get rid of the carriage return in my opinion is to stop it being created in the first place. This method is a lot tidier than having to remove the last character.
In the .GetString method there is a parameter for RowDelimiter which by default is set to be a carriage return. However you can change this to be whatever you want including a zero length string as follows:
rs.GetString(, , , "")
If you run your debug again with this code:
rs.GetString(, , , "")
Debug.Print "'" & TempHold & "'"
You will get this result:
'AFR'
Remember if you want something different to be placed between rows then just change the zero length string to whatever you need.
I want to remove extra carriage return at end of the text field. I used the following code but it removed it removed carriage return in the middle of the text. I need to remove at the end which is added while pressing return key
Text50.Text = Replace(Text50.Text, vbCrLf, "")
Text50.Text = RTrim(Text50.Text)
Pls suggest me
If you are sure there always is a vbcrlf at the end of the textbox, you can use this:
left(Text50.Text ,len(Text50.Text)-2)
Note that the length of vbcrlf is 2, since it is considered 2 characters: carrige return and line feed.
If you are unsure if the last character is vbcrlf or not, you can use this method:
If Right(Text50.Text, 2) = vbCrLf Then
Text50.Text = Left(Text50.Text, Len(Text50.Text) - 2)
Else
Text50.Text = Text50.Text
End If
This is odd. In the code below the replace() does not replace the : character. Why? How do I fix this? Even if I switch ":" with chr(58) it doesn't work.
Dim dispName as String
dispName = " 110531 Reļ¼Our file 027-10.doc"
dispName = Replace(dispName, ":", " ")
msgbox dispName
I copied your code as it is to notepad++ and found that used both ':' characters are different.
Change both to the same and run the code