I am trying to create a VBA program in Excel to delete certain rows and send those deleted rows to an access database. Here is what I want the workflow to be:
- An individual downloads a CSV file called print.xlsx that opens in Excel, and contains something similar to what the table below looks like(obviously the content here is random)
- That individual also has a primary workbook(that again looks similar to this), that is always open on their desktop, lets call it mike.xlsm
- That individual clicks a button in mike.xlsm that executes a macro that should do the following:
- Deletes any rows in mike.xlsm that are not contained in print.xlsx(based on an identifying number (in the flag column). No number will ever duplicate, so that is not an issue.
- Gets any rows from print.xlsx that were not in mike.xlsm, and adds them to mike.xlsm, so essentially, the same rows exists for mike.xlsm as in print.xlsx. The reason for doing this is that print.xlsx does not contain all the columns that mike.xlsm does. However, this shouldn't interfere with the code, as the columns come after the columns that exists in both workbooks. The additional columns in the mike workbook are meant for user input.
- Sends those deleted rows to an Access database, however attaches an additional column to those rows that contains the workbook's name (mike), for easy querying with numerous individuals in the future
The code I am currently using to delete rows is:
Sub Delete_DNE() Dim lRow As Long Dim iCntr As Long lRow = 4000 For iCntr = lRow To 1 Step -1 If Cells(iCntr, 5).Value = "DNE" Then Rows(iCntr).Delete End If Next End Sub
My question is: Does anyone have good code that will execute the above workflow, or suggestions for part of the code, to ultimately get to the full workflow?
I am just using vlookup manually to determine what needs to be labled "DNE" meaning it does not exist in print.xlsx, and then manually copying the rows out of print.xlsx that weren't in mike, into mike. Obviously, this is not an automated or efficient way to do this if we want several employees to be doing this, and a large access database of entries. Thank you!
The following code was in a question flagged as a possible duplicate, and is helpful, but does not address the problem, mostly because it transfers the entire worksheet to access and not just specific rows; I only want the deleted rows sent to access:
Sub AccImport()
Dim acc As New Access.Application
acc.OpenCurrentDatabase "C:\Users\Public\Database1.accdb"
acc.DoCmd.TransferSpreadsheet _
TransferType:=acImport, _
SpreadSheetType:=acSpreadsheetTypeExcel12Xml, _
TableName:="tblExcelImport", _
Filename:=Application.ActiveWorkbook.FullName, _
HasFieldNames:=True, _
Range:="Folio_Data_original$A1:B10"
acc.CloseCurrentDatabase
acc.Quit
Set acc = Nothing
End Sub