Yu *_*hen 9 excel vba excel-vba carriage-return
使用Visual Basic,我对Print语句的行为感到困惑,因为有时候下面的语句:会在一行的末尾引起额外的回车"^ M",但有时,它不会.我想知道为什么?
filePath = "d:\tmp\FAE-IMM-Report-2012-Week.org"
If Dir(filePath) <> "" Then
Kill filePath
End If
outFile = FreeFile()
Open filePath For Output As outFile
Print #outFile, "#+TITLE: Weekly Report"
Run Code Online (Sandbox Code Playgroud)
会产生
#+TITLE: Weekly Report^M
Run Code Online (Sandbox Code Playgroud)
虽然我希望没有^ M:
#+TITLE: Weekly Report
Run Code Online (Sandbox Code Playgroud)
在一个测试程序中,几乎相同的代码不会产生"^ M".
经过进一步的实验,我发现以下建议使用vbNewline和";" 在打印内容的最后,仍然没有解决我的问题.
仔细隔离之后,我发现问题的原因是一个看起来像空格的字符,而不是空格,接着是换行符和回车符.在打印包含违规字符串的文本之前,没有回车符,但是一旦打印出违规行,那么包括上一行打印在内的每一行都会有回车符.
我不确定违规字符串是什么,因为我的VBA技能还不太好.
以下是电子表格单元格中违规文本的副本:
"There is something invisible after this visible text
After the invisible text, then there might be a carriage return $Chr(13) and/or newline"
Run Code Online (Sandbox Code Playgroud)
不过,我不确定粘贴到网络浏览器是否会保留内容.通过粘贴到emacs,我没有看到回车,而emacs应该显示它,如果有的话.所以我猜在违规字符串中没有回车符.
以下是程序演示问题:
Sub DemoCarriageReturnWillAppear()
Dim filePath As String
Dim outFile
Dim offendingText
filePath = "d:\tmp\demoCarriageReturn.org"
If Dir(filePath) <> "" Then
Kill filePath
End If
outFile = FreeFile()
Open filePath For Output As outFile
Print #outFile, "#+AUTHOR: Yu Shen" & vbNewLine;
Close #outFile 'At this moment, there is no carriage return
Open filePath For Append As outFile
offendingText = ThisWorkbook.Worksheets("Sheet1").Range("A1")
Print #outFile, offendingText & vbNewLine;
Close #outFile 'Now, every line end has carriage return.
'It must be caused by something offending at the above print out content.
End Sub
Run Code Online (Sandbox Code Playgroud)
以下是上述程序的最终结果:
#+AUTHOR: Yu Shen^M
There is something invisible after this visible text
After the invisible text, then there might be a carriage return $Chr(13) or newline^M
Run Code Online (Sandbox Code Playgroud)
请注意,上面的"^ M"是由我添加的,因为在浏览器中不会显示回车符.
如果您有兴趣,我可以向您发送带有违规内容的excel文件.
我需要你的帮助,如何避免那些违规的字符串或回车.(我甚至尝试将字符串替换为回车符或新行,因为我发现一旦我手动删除导致更改到另一行的任何内容,问题就会消失.但是调用Replace来替换vbNewline,Chr $(13),或者vbCrLf没有任何区别.
感谢您的进一步帮助!
宇
Ton*_*ore 19
使用尾随分号来压制新行:
Print #outFile, "#+TITLE: Weekly Report";
^
^
Run Code Online (Sandbox Code Playgroud)
如果你在语句中犯了错误,VB编辑器通常会添加一个分号,这可以解释为什么有时会输出新行,有时不会.
新的诊断程序
我们需要知道导致问题的单元格A1中的字符.
将以下子例程放在其中一个模块中.
Public Sub DsplInHex(Stg As String)
Dim Pos As Long
For Pos = 1 To Len(Stg)
Debug.Print Hex(AscW(Mid(Stg, Pos, 1))) & " ";
Next
Debug.Print
End Sub
Run Code Online (Sandbox Code Playgroud)
转到VB Editor的立即窗口,然后键入以下文本Return:
DsplInHex(Sheets("Sheet1").range("A1"))
Run Code Online (Sandbox Code Playgroud)
在这一行下面,你应该看到类似的东西54 65 73 74 31.这是单元格中每个字符的代码值列表.我希望我们会在列表末尾看到A换行符代码或D回车代码.
将光标定位在单元格A1中.单击F2以选中编辑,然后Backspace删除不可见的尾随字符,然后Return结束编辑.返回立即窗口,将光标定位到结束位置DsplInHex(Sheets("Sheet1").range("A1"))并单击Return.尾随角色应该已经消失了.
试试并报告回来.祝好运.
为了将来帮助其他人,这里总结了我的问题和解决方案。即使打印语句末尾有分号,每行上额外的回车实际上是由其中一个打印语句中的一串空格后跟换行符(Chr$(A))引起的,一旦打印了这样的字符串,那么所有的前后打印的内容都会有额外的回车!
这似乎是 VBA 6(使用 Excel 2007)上的一个错误,一个令人讨厌的错误!
我的解决方法是用空格替换换行符。
感谢托尼的多次帮助,使我终于确定了原因。
这是演示该问题的代码:
Sub DemoCarriageReturnWillAppearOnAllLines()
Dim filePath As String
Dim outFile
Dim offendingText
filePath = "d:\tmp\demoCarriageReturn.org"
If Dir(filePath) <> "" Then
Kill filePath
End If
outFile = FreeFile()
Open filePath For Output As outFile
Print #outFile, "#+AUTHOR: Yu Shen" & vbNewLine;
Close #outFile 'At this moment, there is no carriage return
Open filePath For Append As outFile
offendingText = " " & Chr$(10)
Print #outFile, offendingText & vbNewLine;
Close #outFile 'Now, every line end has carriage return.
'It must be caused by the offending at the above print out content.
End Sub
Run Code Online (Sandbox Code Playgroud)
在第一个“Close #outFile”之后,以下是 demoCarriageReturn.org 文件的内容:
#+AUTHOR: Yu Shen
Run Code Online (Sandbox Code Playgroud)
注意:如果编辑器能够将回车符显示为可见的 ^M,则不存在回车符。
但是,在第二次“Close #outFile”之后,以下是同一文件的内容以及附加内容:
#+AUTHOR: Yu Shen^M
^M
Run Code Online (Sandbox Code Playgroud)
注意:有两个回车出现。他们不是故意的。特别是,到第一行,print语句已经执行完毕,而在前面的close语句处,发现没有回车。(为了说明回车,我必须在此处的网页中输入^M。但它在打印输出的文件中。)
这就是为什么我认为这是一个错误,因为回车不是有意的。这是不受欢迎的惊喜。
下面的代码显示,如果我过滤掉换行符,问题就会消失。
Sub DemoCarriageReturnWillNotAppearAtAll()
Dim filePath As String
Dim outFile
Dim offendingText
filePath = "d:\tmp\demoCarriageReturn.org"
If Dir(filePath) <> "" Then
Kill filePath
End If
outFile = FreeFile()
Open filePath For Output As outFile
Print #outFile, "#+AUTHOR: Yu Shen" & vbNewLine;
Close #outFile 'At this moment, there is no carriage return
Open filePath For Append As outFile
offendingText = " " & Chr$(10)
Print #outFile, Replace(offendingText, Chr$(10), "") & vbNewLine;
Close #outFile 'Now, no more carriage return.
'The only change is removing the linefeed character in the second print statement
End Sub
Run Code Online (Sandbox Code Playgroud)
完整执行上述程序后,确实没有回车!
#+AUTHOR: Yu Shen
Run Code Online (Sandbox Code Playgroud)
这表明空格后换行的字符串组合导致了该错误,删除换行符可以避免该错误。
下面的代码进一步证明,如果没有违规字符串,即使 print 语句末尾没有换行符和分号,也不会出现不需要的回车符!
Sub DemoCarriageReturnWillNotAppearAtAllEvenWithoutNewLineFollowedBySemiColon()
Dim filePath As String
Dim outFile
Dim offendingText
filePath = "d:\tmp\demoCarriageReturn.org"
If Dir(filePath) <> "" Then
Kill filePath
End If
outFile = FreeFile()
Open filePath For Output As outFile
Print #outFile, "#+AUTHOR: Yu Shen"
Close #outFile 'At this moment, there is no carriage return
Open filePath For Append As outFile
offendingText = " " & Chr$(10)
Print #outFile, Replace(offendingText, Chr$(10), "")
Close #outFile 'Now, no more carriage return.
'The real change is removing the linefeed character in the second print statement
End Sub
Run Code Online (Sandbox Code Playgroud)
同样在输出结果中:
#+AUTHOR: Yu Shen
Run Code Online (Sandbox Code Playgroud)
仍然没有烦人的回车!
由此可见,在print语句末尾使用换行符加分号并不能解决每行回车的问题! 真正的解决方案是在打印输出内容中避免任何空格后跟换行符。
于
| 归档时间: |
|
| 查看次数: |
34173 次 |
| 最近记录: |