在Excel中用Chr(13)+ Chr(10)替换Chr(10)以导入到Access

时间:2019-05-15 19:09:26

标签: excel vba access-vba

问题是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

我在哪里出问题了,还有什么更好的方法?

2 个答案:

答案 0 :(得分:1)

没关系,发布后几分钟就想出来了:

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

答案 1 :(得分:0)

要保留Excel数据,您也可以在导入MS Access后运行以下查询:

update 
    YourTable t 
set 
    t.YourField = Replace(t.YourField, Chr(10), Chr(13) & Chr(10))
where 
    t.YourField like "*" & Chr(10) & "*" and
    t.YourField not like "*" & Chr(13) & Chr(10) & "*"