首页 > 解决方案 > 在 Excel 中将 Chr(10) 替换为 Chr(13) + Chr(10) 以导入 Access

问题描述

问题是在 Excel 中使用换行符Char(10),但在 Access 中使用Char(13) & Char(10)。因此,当您从 Excel 导入 Access 时,Excel 中的换行符在 Access 中都位于同一行。

我通过单击按钮自动导入,以便在输入之前对某些数据进行清理。我想将此添加到敏感度中,但我遇到了麻烦:

' Remove blank cells that aren't blank (i.e., pasting values of formulas with no value)
xl.ActiveSheet.UsedRange.Select
xl.Selection.Value = xl.Selection.Value

' Replace line breaks and carriage returns with something Access recognizes
xl.Selection.Value = Replace(xl.Selection.Value, Chr(10), Chr(13) + Chr(10))

' OR this....

xl.ActiveSheet.Selection.Replace _
  What:=Chr(10), Replacement:=Chr(13) + Chr(10), _
  SearchOrder:=xlByColumns, MatchCase:=False

第一种方法生成:

Run-time error '13': Type mismatch

第二种方法生成:

Run-time error '438': Object doesn't support this property or method

我在哪里错了,什么是更好的方法?

标签: excelvbams-access

解决方案


没关系,在发布后几分钟就弄清楚了:

xl.Selection.Replace What:="" & Chr(10) & "", Replacement:="" & Chr(13) & Chr(10) & "", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
    ReplaceFormat:=False

推荐阅读