|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
数据库在公网。以下是我更新的代码,数据较少(十几条)时用得还是可以的。但是上五六百条的时候,得更新20s左右了,大家有优化的办法吗?
Private Sub 批量更新()
On Error Resume Next
If Authority("更新") = False Then
MsgBox "未登录或无权限"
Exit Sub
End If
Dim datatable$
datatable = Cells(1, 1)
Dim Lastcolumn As Long
Lastcolumn = Cells(3, Columns.Count).End(xlToLeft).Column
Dim lngLastRow As Long
Dim j, k, m, n As Long
lngLastRow = Cells(Rows.Count, 5).End(xlUp).Row
con.Open strCn '与数据库建立连接,成功返回连接对象con
For m = 4 To lngLastRow
Str1 = "select * from " & datatable & " where st='1' and id='" & Cells(m, 1) & "'"
rst.Open Str1, con, adOpenKeyset, adLockOptimistic
If rst.RecordCount = 0 And Cells(m, 6) <> "" Then
rst.AddNew
For n = 5 To Lastcolumn
rst.Fields(n - 1) = ActiveSheet.Cells(m, n)
Next n
rst.Fields(1) = "1"
rst.Fields(2) = Now()
rst.Update
End If
If Cells(m, 1) <> 0 And Cells(m, 6) <> "" Then
For n = 5 To Lastcolumn
rst.Fields(n - 1) = ActiveSheet.Cells(m, n)
Next n
rst.Fields(1) = "1"
rst.Fields(3) = Now()
rst.Update
End If
rst.Close
Set rst = Nothing
If Cells(m, 6) = "" Then
MsgBox "有f2单元格为空,该行未录入或更新!"
End If
Next m
con.Close
Set con = Nothing
Call 查询
End Sub
|
|