vba vbNewline 与 Chr(10) 作为换行符在 Windows 与 Mac OSX 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26599769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vbNewline vs Chr(10) as linebreak delimiter in Windows vs. Mac OSX
提问by Jamie Marshall
I have an Excel sub that uses the Split()
function to split CSV data from a cell into an array. However, depending on the version of Excel/OS I'm using, the character used as the line break delimiter changes:
我有一个 Excel 子程序,它使用该Split()
函数将单元格中的 CSV 数据拆分为一个数组。但是,根据我使用的 Excel/OS 版本,用作换行符的字符会发生变化:
Excel 2011 / Mac OSX:
Excel 2011 / Mac OSX:
fullArray = Split(CSV, vbNewLine) 'successfully returns array
fullArray = Split(CSV, Chr(10)) 'fails and returns only a single cell
Excel 2007 / Windows 7:
Excel 2007 / Windows 7:
fullArray = Split(CSV, Chr(10)) 'successfully returns array
fullArray = Split(CSV, vbNewLine) 'fails and returns only a single cell
Anyone else noticed this/has an explanation why this is going on?
其他任何人都注意到这一点/有一个解释为什么会这样?
回答by David Zemens
If you need to support multiple OS (or different versions on the same OS) you can look in to conditional compilation statements.
如果您需要支持多个操作系统(或同一操作系统上的不同版本),您可以查看条件编译语句。
You can refer to this list of built-in compiler constants:
你可以参考这个内置编译器常量列表:
http://www.utteraccess.com/wiki/index.php/Conditional_Compilation#Built_In_Compiler_Constants
http://www.utteraccess.com/wiki/index.php/Conditional_Compilation#Built_In_Compiler_Constants
Define your delimiter variable as a string and assign it the result of a function.
将分隔符变量定义为字符串并将函数的结果分配给它。
Dim dlmt as String
dlmt = newLine()
fullArray = Split(CSV, dlmt)
The function then uses the conditional compilation constant to check the OS:
然后该函数使用条件编译常量来检查操作系统:
Function newLine() As String
#If Win32 Or Win64 Then
ret = Chr(10)
#ElseIf Mac Then
ret = vbNewLine
#End If
newLine = ret
End Function
Frankly now that I do this I remember it's not strictly necessary to use conditional compile here unless you have methods/properties that won't compile in some versions. You could use the more simple property of Application.OperatingSystem
:
坦率地说,现在我这样做了,我记得在这里使用条件编译并不是绝对必要的,除非您的方法/属性在某些版本中无法编译。您可以使用更简单的属性Application.OperatingSystem
:
Function newLine() As String
Select Case Application.OperatingSystem
Case Like "Windows*"
ret = Chr(10)
Case Else
ret = vbNewLine
End Select
End Function
回答by Siddharth Rout
As John mentioned in the comments, the two operating systems have different NewLine character.
正如 John 在评论中提到的,这两个操作系统具有不同的 NewLine 字符。
And hence before you split it, check for which character is present and then split it. For example
因此,在拆分之前,请检查存在哪个字符,然后将其拆分。例如
newL = InStr(1, CSV, vbNewLine)
vbChrTen = InStr(1, CSV, Chr(10))
If newL > 0 And vbChrTen > 0 Then
MsgBox "The string contains both. How would you like to handle it?"
'
'~~> Rest of the code
'
ElseIf newL > 0 Then
fullArray = Split(CSV, vbNewLine)
ElseIf vbChrTen > 0 Then
fullArray = Split(CSV, Chr(10))
Else
MsgBox "The string doesn't contain either of the de-limiters. How would you like to handle it?"
'
'~~> Rest of the code
'
End If
回答by Noam Brand
Case Like "Windows*" is not valid. This works for me:
Case Like "Windows*" 无效。这对我有用:
' User has Windows or Mac for NewLine
Dim mynewLine As String
If left(Application.OperatingSystem, 7) = "Windows" Then
mynewLine = Chr(10)
Else
mynewLine = vbNewLine
End If