我将数据从Excel导出到Access。 excel中的数据包含公式,如果函数为=IF(SISESTUS!B18="";"";SISESTUS!C18
,则将单元格留空。
问题是:VBA无法使用公式读取空白单元格。这会引发类型不匹配错误。当我只复制值然后就可以了,我的导出宏运行完美。
代码:
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
我认为,在这些行代码之后,宏结束并给出错误消息。为清楚起见。
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
答案 0 :(得分:0)
如果有人遇到类似的问题我用粘贴修复了我的问题:
Sub Copy_Data()
Worksheets("Sheet1").Range("A2:P250").Copy
Worksheets("Sheet1").Range("U2:AJ250").PasteSpecial xlPasteValues
End Sub