我有一个 Access VBA 脚本
- 循环遍历目录中的 Excel 电子表格列表
- 循环遍历每个电子表格的每个工作表
- 将工作表中的数据导入 Access 表
我想再添加一个步骤:
- 将列中的 Excel 值
Field1
与 Access 表中的值列表进行比较Exclude
,如果找到匹配项,则删除 Excel 中的整行。
这是简化的代码,并附有说明新代码应该放在哪里的注释:
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM FileList")
rs.MoveFirst
Dim appExcel As Excel.Application
Set appExcel = New Excel.Application
Dim objWorkbook As Excel.Workbook
Dim objSheet As Excel.Worksheet
Do Until rs.EOF = True
strFilePath = rs!FilePath
Set objWorkbook = appExcel.Workbooks.Open(strFilePath)
For Each objSheet In objWorkbook.Sheets
objSheet.Select
strQuery =
"INSERT INTO tblImport (Field1)
SELECT [Field1]
FROM [Excel 8.0;HDR=YES;DATABASE=" & strFilePath & "].[" & objSheet.Name & strRange & "]"
CurrentDb.Execute strQuery, dbFailOnError
***Add Code Here to Delete Rows in Excel where value found in Exclusion table***
Next objSheet
Set objSheet = Nothing
objWorkbook.SaveAs FileName:=strFilePath, FileFormat:=xlExcel8
objWorkbook.Close SaveChanges:=False
Set objWorkbook = Nothing
rs.MoveNext
Loop
我确定我需要 Dim 另一个 RecordSet:
Dim rsExclude AS Recordset
Set rsExclude = CurrentDb.OpenRecordset("SELECT * FROM Exclude")
然后呢?或者也许有人可以推荐一种替代方法?