在VBA中input与包含公式的空单元格不匹配错误

我正在将数据从Excel导出到Access。 excel中的数据包含公式,这些公式用if函数留下单元格空白: =IF(SISESTUS!B18="";"";SISESTUS!C18

问题是:VBA不能读取具有公式的空白单元格。 这会引起types不匹配错误。 当我只复制值然后是好的,我的出口macros运行完美。

码:

 Sub Export_Data() Dim cnn As ADODB.Connection Dim rst As ADODB.Recordset Dim dbPath Dim x As Long, i As Long Dim nextrow As Long On Error GoTo errHandler: dbPath = ActiveSheet.Range("S3").Value nextrow = Cells(Rows.Count, 1).End(xlUp).row Set cnn = New ADODB.Connection If Sheet8.Range("A2").Value = "" Then MsgBox " Lisa kirjed tellimusse, midagi pole arhiveerida" Exit Sub End If cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath Set rst = New ADODB.Recordset rst.Open Source:="Tellimused", ActiveConnection:=cnn, _ CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _ Options:=adCmdTable For x = 2 To nextrow rst.AddNew For i = 1 To 16 rst(Cells(1, i).Value) = Cells(x, i).Value Next i rst.Update Next x rst.Close cnn.Close Set rst = Nothing Set cnn = Nothing MsgBox " Tellimus on edukalt arhiiveeritud" Application.ScreenUpdating = True Sheet8.Range("R3").Value = Sheet8.Range("T3").Value + 1 Sheet8.Range("A2:P250").ClearContents On Error GoTo 0 Exit Sub errHandler: Set rst = Nothing Set cnn = Nothing MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Export_Data" End Sub 

我想,在这些行代码之后,macros结束并给出错误消息。 为了清楚。

 For x = 2 To nextrow rst.AddNew For i = 1 To 16 rst(Cells(1, i).Value) = Cells(x, i).Value Next i rst.Update Next x 

我会build议在你的公式中使用一些可识别的值,macros可以读取(如999999)而不是“”,并在macros内处理:

 If Cells(x, i).Value <> 999999 Then rst(Cells(1, i).Value) = Cells(x, i).Value Else rst(Cells(1, i).Value) = "" ' or skip End If 

如果有人遇到类似问题,我修复我的问题:

 Sub Copy_Data() Worksheets("Sheet1").Range("A2:P250").Copy Worksheets("Sheet1").Range("U2:AJ250").PasteSpecial xlPasteValues End Sub