Cod*_*gry 40 excel vba excel-vba
如果我的字符串以换行符结束,我想从字符串中删除换行符.
Sub linebreak(myString)
If Len(myString) <> 0 Then
If Right$(myString, 1) = vbCrLf Or Right$(myString, 1) = vbNewLine Then myString = Left$(myString, Len(myString) - 1)
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
Moo*_*sli 63
str = Replace(str, vbLf, "")
Run Code Online (Sandbox Code Playgroud)
此代码从代码中取出所有换行符
如果你只想要最后一个:
If Right(str, 1) = vbLf Then str = Left(str, Len(str) - 1)
Run Code Online (Sandbox Code Playgroud)
是你如何尝试OK的方式.
更新:
换行= ASCII 10,换页= ASCII 12和回车= ASCII 13.在这里,我们清楚地看到了我们所知道的:PC来自(电动)打字机
vbLf是Chr(10)并且表示光标向下跳一行(打字机:转动滚轮) vbCr是Chr(13)表示光标跳到开头(打字机:拉回滚动)
在DOS中,无论如何,换行符总是VBCrLf或Chr(13)&Chr(10),但也可以用VB中的文本框.
另一方面, 在Excel单元格中,换行符仅为VBLf,即使没有vbCr,第二行也会从第一个位置开始.使用vbCrLf然后更深入一个单元格.
所以它取决于你从哪里读取并从中获取字符串.如果你想在你的tring中删除所有的vbLf(Char(10))和vbCr(Char(13)),你可以这样做:
strText = Replace(Replace(strText, Chr(10), ""), Chr(13), "")
Run Code Online (Sandbox Code Playgroud)
如果你只想删除最后一个,你可以测试这样做:
If Right(str, 1) = vbLf or Right(str, 1) = vbCr Then str = Left(str, Len(str) - 1)
Run Code Online (Sandbox Code Playgroud)
chr*_*sen 25
vbCrLf并且vbNewLine是actualy两个字符长,所以改为Right$(myString, 2)
Sub linebreak(myString)
If Len(myString) <> 0 Then
If Right$(myString, 2) = vbCrLf Or Right$(myString, 2) = vbNewLine Then
myString = Left$(myString, Len(myString) - 2)
End If
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
小智 7
尝试使用以下行:
CleanString = Application.WorksheetFunction.Clean(MyString)
Run Code Online (Sandbox Code Playgroud)
当您使用Excel时,您不需要VBA来实现这一点,您可以简单地使用内置的"Clean()"函数,这将删除回车符,换行符等,例如:
=Clean(MyString)
Run Code Online (Sandbox Code Playgroud)
Clean function can be called from VBA this way:
Range("A1").Value = Application.WorksheetFunction.Clean(Range("A1"))
However as written here, the CLEAN function was designed to remove the first 32 non-printing characters in the 7 bit ASCII code (values 0 through 31) from text. In the Unicode character set, there are additional nonprinting characters (values 127, 129, 141, 143, 144, and 157). By itself, the CLEAN function does not remove these additional nonprinting characters.
Rick Rothstein have written code to handle even this situation here this way:
Function CleanTrim(ByVal S As String, Optional ConvertNonBreakingSpace As Boolean = True) As String
Dim X As Long, CodesToClean As Variant
CodesToClean = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, _
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 127, 129, 141, 143, 144, 157)
If ConvertNonBreakingSpace Then S = Replace(S, Chr(160), " ")
For X = LBound(CodesToClean) To UBound(CodesToClean)
If InStr(S, Chr(CodesToClean(X))) Then S = Replace(S, Chr(CodesToClean(X)), "")
Next
CleanTrim = WorksheetFunction.Trim(S)
End Function
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
176581 次 |
| 最近记录: |