2016-11-07 76 views
0

我从ms访问文件导入表(DKt和DTt),以便excel表(DKe和DTe),每个表对应一张表。现在我想在excel文件中发生一些变化时更新ms文件。我为代码中显示的不同工作表和表格编写了不同的循环,但我有超过8000行,需要很长时间才能运行。是唯一的方式还是有另一种方法来编写所有表和工作表的循环? 我也得到一个错误,当我在旧版本的微软(2013年)运行宏lastrow = Workbooks(1).Sheet("DKe").Cells(Workbooks(1).Sheet("DKe").Rows.Count, "A").End(xlUp).RowSubscript out of range,我怎么能得到不同版本的结果? 这是我从Excel表格中更新ms文件表宏:使用多表excel更新多表MS访问文件

`Sub UpdateMDB() 
Dim accConn As Object, accRST As Object 
Dim accFile As String, accStr As String 
Dim lastrow As Long, i As Long 
Const adOpenKeyset = 1, adLockOptimistic = 3, adCmdTableDirect = 512 
Dim accConn2 As Object, accRST2 As Object, lastrow2 As Long 
lastrow =  Workbooks(1).Sheet("DKe").Cells(Workbooks(1).Sheet("DKe").Rows.Count, "A").End(xlUp).Row 
''lastrow2 = Workbooks(1).Sheets("Dte").Cells(Workbooks(1).Sheets("DTe").Rows.Count, "A").End(xlUp).Row 

accFile = "Z:\Documents\Database\Database1.mdb" 
accStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & accFile & ";" 

Set accConn = CreateObject("ADODB.Connection") 
''Set accConn2 = CreateObject("ADODB.Connection") 
Set accRST = CreateObject("ADODB.Recordset") 
''Set accRST2 = CreateObject("ADODB.Recordset") 

accConn.Open accStr 

'' Update for DK 
accRST.Open "SELECT * FROM DKt", accConn, adOpenKeyset, adLockOptimistic, adCmdTableDirect 
If Not (accRST.BOF And accRST.EOF) Then 
accRST.MoveFirst 
Else 
MsgBox "No records in Access table.", vbInformation 
accRST.Close: accConn.Close: Set accRST = Nothing: Set accConn = Nothing 
Exit Sub 
End If 

Do While Not accRST.EOF 
For i = 1 To lastrow 
    If accRST!ID = Workbooks(1).Sheet("DKe").Range("A" & i) _ 
      And accRST!DK <> Workbooks(1).Sheet("DKe").Range("B" & i) Then 
     accRST!DK.Value = Workbooks(1).Sheet("DKe").Range("B" & i) 
    End If 
Next i 
accRST.Update 
accRST.MoveNext 
Loop 

accRST.Close: accConn.Close 
Set accRST = Nothing: Set accConn = Nothing 

'' Update for DT 
''accRST2.Open "SELECT * FROM DTt", accConn, adOpenKeyset, adLockOptimistic, adCmdTableDirect 
''If Not (accRST2.BOF And accRST2.EOF) Then 
'' accRST2.MoveFirst 
''Else 
'' MsgBox "No records in Access table.", vbInformation 
'' accRST2.Close: accConn.Close: Set accRST2 = Nothing: Set accConn = Nothing 
'' Exit Sub 
''End If 

''Do While Not accRST2.EOF 
'' For i = 1 To lastrow2 
''  If accRST2!ID = Workbooks(1).Sheets("DTe").Range("A" & i) _ 
''    And accRST2!DT <> Workbooks(1).Sheets("DTe").Range("B" & i) Then 
''   accRST2!DT.Value = Workbooks(1).Sheets("DTe").Range("B" & i) 
''  End If 
'' Next i 
'' accRST2.Update 
'' accRST2.MoveNext 
''Loop 

''accRST2.Close: accConn.Close 
''Set accRST2 = Nothing: Set accConn = Nothing 

End Sub 

回答

0

的更新循环变化的部分

`accConn.Open accStr 
accRST.Open "SELECT * FROM "DKt", accConn, adOpenKeyset, adLockOptimistic, adCmdTableDirect 

    If Not (accRST.BOF And accRST.EOF) Then 
    accRST.MoveFirst 
    Else 
MsgBox "No records in Access table.", vbInformation 
accRST.Close: accConn.Close: Set accRST = Nothing: Set accConn = Nothing 
Exit Sub 
End If 

For i = 2 To lastrow 
''(because in excel file the values start from second row and first row is name of the column) 
If accRST!ID = Workbooks(1).Sheets("DKe").Range("A" & i) _ 
      And accRST!DK <> Workbooks(1).Sheets("DKe").Range("B" & i) Then 
    accRST!DK.Value = Workbooks(1).Sheets("DKe").Range("B" & i) 
    accRST.Update 
End If 
accRST.MoveNext 
Next i 
accRST.Close: accConn.Close 
Set accRST = Nothing: Set accConn = Nothing 
MsgBox "DK was updated"` 

DT开始与accConn2.Open accStr下一次更新之后。