vbNewline vs Chr(10)作为Windows与Mac OSX中的换行符分隔符 [英] vbNewline vs Chr(10) as linebreak delimiter in Windows vs. Mac OSX
问题描述
我有一个使用 Split()
函数的Excel子句将CSV数据从单元格拆分成数组。然而,根据我使用的Excel / OS版本,用作换行符分隔符的字符更改:
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 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?
推荐答案
如果您需要支持多个操作系统(或同一操作系统上的不同版本),可以查看条件编译语句。
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
将您的分隔符变量定义为字符串,并为其分配一个函数的结果。
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
坦白说,我这样做,我记得在这里使用条件编译并不是绝对必要的ss有某些版本中不能编译的方法/属性。您可以使用更简单的属性 Application.OperatingSystem
:
Function newLine() As String
Select Case Application.OperatingSystem
Case Like "Windows*"
ret = Chr(10)
Case Else
ret = vbNewLine
End Select
End Function
这篇关于vbNewline vs Chr(10)作为Windows与Mac OSX中的换行符分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!