Reputation: 323
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:
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
Upvotes: 0
Views: 1638
Reputation: 20302
Ok, I would do it like this. This is very generic...needs to be modified by you to suit your specific needs...
Anything that says 'Sheet2' in ColumnI, starting in Row2 (you may want to start in Row1, if you don't have headers), gets cut/pasted from Sheet1 to Sheet2
Sub MoveSheet2()
Dim i As Variant
Dim endrow As Integer
Dim Sheet1 As Worksheet, Sheet2 As Worksheet
Set Sheet1 = ActiveWorkbook.Sheets("Sheet1")
Set Sheet2 = ActiveWorkbook.Sheets("Sheet2")
endrow = Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row
For i = 2 To endrow
If Sheet1.Cells(i, "I").Value = "Sheet2" Then
Sheet1.Cells(i, "I").EntireRow.Cut Destination:=Sheet2.Range("A" & Sheet2.Rows.Count).End(xlUp).Offset(1)
End If
Next
End Sub
Now, move everything from Sheet2 to your Access DB.
Sub ADOFromExcelToAccess()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\FolderName\DataBaseName.mdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "TableName", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 2 ' the start row in the worksheet
Do While Len(Range("A" & r).Formula) > 0
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("FieldName1") = Range("A" & r).Value
.Fields("FieldName2") = Range("B" & r).Value
.Fields("FieldNameN") = Range("C" & r).Value
' add more fields if necessary...
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
Upvotes: 2
Reputation: 18555
I haven't coded with VBA in years but you certainly can do what you describe. The process would go something like:
Upvotes: 1