I have an Access VBA script that
- Loops through a list of Excel spreadsheets in a directory
- Loops through each Sheet of each spreadsheet
- Imports the data from the sheet to an Access table
I would like to add one more step:
- Compare the Excel values in column
Field1
to a list of values in Access tableExclude
, and delete the entire row in Excel if a match is found.
Here's the simplified code, with a note indicating where the new code should go:
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
I'm sure I'll need to Dim another RecordSet:
Dim rsExclude AS Recordset
Set rsExclude = CurrentDb.OpenRecordset("SELECT * FROM Exclude")
Then what? Or perhaps someone can recommend an alternate approach?