|
本帖最后由 zhaogang1960 于 2015-8-17 10:16 编辑
要求是这样吗?
I2=1时删除Excel工作表中A列对应数据库中的记录
I2=2时删除Excel工作表中B列对应数据库中的记录
如果是请参考:- Sub 删除工作表存在的记录()
- Dim cnn As New ADODB.Connection
- Dim rs As New ADODB.Recordset
- Dim myTable As String
- Dim strWhere As String
- Dim SQL As String
- Dim Col As String
- myTable = "Ruku"
- Col = Cells(1, [i2])
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & ThisWorkbook.Path & "\Mydb.mdb"
- strWhere = " WHERE EXISTS(SELECT * FROM [Excel 8.0;Database=" & ThisWorkbook.FullName & "].[" & ActiveSheet.Name & "$" _
- & Cells(1, [i2]).Resize(Cells(Rows.Count, [i2]).End(xlUp).Row).Address(0, 0) & "] WHERE " & Col & "=" & myTable & "." & Col & ")"
- SQL = "SELECT " & Col & " FROM " & myTable & strWhere
- rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic
- If rs.RecordCount > 0 Then
- SQL = "DELETE FROM " & myTable & strWhere
- cnn.Execute SQL
- MsgBox rs.RecordCount & "条记录被删除。", vbInformation, "提示"
- Else
- MsgBox "没有发现要删除的记录。", vbInformation, "提示"
- End If
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- End Sub
复制代码
|
|