excel - vbNewline 与 Chr(10) 作为 Windows 与 Mac OSX 中的换行分隔符

标签 excel vba compatibility

我有一个 Excel 子程序,它使用 Split() 函数将 CSV 数据从单元格拆分到数组中。但是,根据我使用的 Excel/OS 版本,用作换行符分隔符的字符会发生变化:

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:

  fullArray = Split(CSV, Chr(10)) 'successfully returns array

  fullArray = Split(CSV, vbNewLine) 'fails and returns only a single cell

还有其他人注意到这一点/有解释为什么会发生这种情况吗?

最佳答案

如果您需要支持多个操作系统(或同一操作系统上的不同版本),您可以查看条件编译语句。

您可以引用以下内置编译器常量列表:

http://www.utteraccess.com/wiki/index.php/Conditional_Compilation#Built_In_Compiler_Constants

将分隔符变量定义为字符串并将其分配给函数的结果。

Dim dlmt as String

dlmt = newLine()

fullArray = Split(CSV, dlmt)

该函数然后使用条件编译常量来检查操作系统:

Function newLine() As String

#If Win32 Or Win64 Then
    ret = Chr(10)
#ElseIf Mac Then
    ret = vbNewLine
#End If

newLine = ret

End Function

坦率地说,现在我这样做了,我记得在这里使用条件编译并不是绝对必要的,除非您有在某些版本中无法编译的方法/属性。您可以使用 Application.OperatingSystem 的更简单属性:

Function newLine() As String

Select Case Application.OperatingSystem
    Case Like "Windows*" 
        ret = Chr(10)
    Case Else
        ret = vbNewLine
End Select

End Function

关于excel - vbNewline 与 Chr(10) 作为 Windows 与 Mac OSX 中的换行分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26599769/

相关文章:

vba - 如何编辑 Access ADP 文件?

c# - Selenium 2.53.1 不适用于 FireFox 48

vba - MS Excel - 如何每 5 秒自动刷新一次单元格?

windows - 我可以在 Linux 上生成带有原生 Excel 图表的 Excel 文件吗?

regex - 替换工作表名称中的多个无效字符

PHP MySQLi 函数在 Linux 上抛出解析错误,但在 Windows 上不会

java - jar 在 Windows 上运行但在 Ubuntu 上不运行

excel - 如何计算一个独特事件在 Excel 中发生的次数?

java - 如何使用 selenium webdriver 将 3 个不同变量中的 3 列值写入 Excel 工作表

excel - 需要 VBA 代码来取消选择所有切片器项目