将表中每行的特定字段从Excel导出到特定的Access表VBA

时间:2015-12-07 17:16:40

标签: excel vba excel-vba ms-access

好的,所以我从不使用Excel来做VBA,我发现它非常有限,繁琐且令人费解。无论如何,我基本上试图连续获得一组具有"评论"的5个字段。在他们中。然后循环,找到这些行,将它们拉出来,然后将它们附加到特定的Access表。我已经看了很多,并没有找到一致或适用的解决方案,所以我道歉,如果这是不完整或关闭。就像我说的,我不熟悉Excel的VBA。

奇怪的是,我总是得到对象声明错误。我也验证了我的参考文献。

我的错误是"运行时错误' 424':所需对象:

以下是我所拥有的:

Sub ExportNewNotes()
' exports data from the active worksheet to a table in an Access database

Dim r As Long
Dim dbs As Database
Dim iUpdated As Integer

Set dbs = OpenDatabase("J:\CUS Supply Chain\All Division Reporting\Field Retrievals\Field Retrieval - John's Copy.accdb")
iUpdated = 0

r = 6 ' the start row in the worksheet

Do While Len(Range("A" & r).Formula) > 0
' repeat until first empty cell in column A

If Range("AE" & r).Value Is Not Null Then
    dbs.Execute "INSERT INTO tNotes " _
    & "(Retrieval_ID,Location,Batch,Note_Date,Note)" _
    & "SELECT .Fields('Retrieval Nbr') = Range('A' & r).Value," _
    & ".Fields('Location Number') = Range('I' & r).Value," _
    & ".Fields('Lot/Serial Number') = Range('N' & r).Value," _
    & "Date()," _
    & ".Fields('New Comments') = Range('AH' & r).Value,"
    iUpdated = iUpdated + 1
End If

r = r + 1 ' next row
Loop

MsgBox CStr(iUpdated) & " record(s) added to the Notes Table.", vbOKOnly, "Upload Completed"

End Sub

如果有人有任何见解,那将非常感谢!

0 个答案:

没有答案