macros在一个脚本中正常运行,但复制版本花费了100倍的时间

我有一个macros脚本,我已经build立了从数据库中获取条目,并将结果写入工作表,然后再由工作簿中的macros处理。

为了重构我的代码,我试着在前端部分调整一些button,使它更时尚,这不是重要的一部分。

这是仍然有效的原始代码,

Sub Test() Dim xm, dd As Worksheet Set dd = ThisWorkbook.Worksheets("Start Sheet") procName = dd.Cells(1, 1).Value If procName = "" Then MsgBox "There is no value in selected row." & Chr(10) & "Please go to 'Start Sheet' and select a value first.", vbExclamation, "Try again" Exit Sub End If Set xm = ThisWorkbook.Worksheets("The Work Page") xm.Cells.Clear Dim Cn As ADODB.Connection Dim Server_Name As String Dim Database_Name As String Dim User_ID As String Dim Password As String Dim SQLStr As String Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset Dim objectName As String Dim objectTableCol As Collection Set objectTableCol = New Collection Dim y As Long Dim ExistsFlag As Boolean ExistsFlag = False Server_Name = "" ' Enter your server name here Database_Name = "" ' Enter your database name here User_ID = "" ' enter your user ID here Password = "" ' Enter your password here SQLStr = "SELECT columnname FROM [table name] WHERE name = 'some name of a column'" Set Cn = New ADODB.Connection Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _ ";Uid=" & User_ID & ";Pwd=" & Password & ";" rs.Open SQLStr, Cn, adOpenStatic 'Chr(10) is a NewLine character bigarray = Split(rs.Fields("field name to split on"), Chr(10)) rs.Close Set rs = Nothing Cn.Close Set Cn = Nothing '********************************************************* Find Objects!! ******************************************************************************* For i = 0 To UBound(bigarray) xm.Cells(i + 1, 1).Value = bigarray(i) 'this bit of code writes code to the excel sheet. Next i End Sub 

这段代码被各种macros调用为子例程,并且在button的首页上显示。 运行没有困难,平均运行时间约5-6.5秒。

下面是我从上面这个testing模块复制到代码重构目的工作簿中另一个单独模块的代码片段。


 Sub PopulateExcelWithXML() Dim xm, dd As Worksheet Set dd = ThisWorkbook.Worksheets("Start Sheet") procName = dd.Cells(1, 1).Value If procName = "" Then MsgBox "There is no value in selected row." & Chr(10) & "Please go to 'Start Sheet' and select a process first.", vbExclamation, "Try again" Exit Sub End If Set xm = ThisWorkbook.Worksheets("The Work Page") xm.Cells.Clear Dim Cn As ADODB.Connection Dim Server_Name As String Dim Database_Name As String Dim User_ID As String Dim Password As String Dim SQLStr As String Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset Server_Name = "" ' Enter your server name here Database_Name = "" ' Enter your database name here User_ID = "" ' enter your user ID here Password = "s" ' Enter your password here SQLStr = "SELECT column name FROM [table name] WHERE name = 'name of some column'" Set Cn = New ADODB.Connection Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _ ";Uid=" & User_ID & ";Pwd=" & Password & ";" rs.Open SQLStr, Cn, adOpenStatic 'Chr(10) is a NewLine character bigarray = Split(rs.Fields("processxml"), Chr(10)) rs.Close Set rs = Nothing Cn.Close Set Cn = Nothing '********************************************************* Find Objects!! ******************************************************************************* For i = 0 To UBound(bigarray) xm.Cells(i + 1, 1).Value = bigarray(i) 'this bit of code writes code to the excel sheet. used for debugging Next i End Sub 

现在这个代码从头版调用,我右键单击图像,并分配给它的macros。 在每个方面都非常直接和相似,减去他们写入的页面,一个是testingdebugging页面,另一个是工作页面。 第二个macros的运行时间是20-25分钟。 这是非常慢,我不知道为什么。

其他信息可能会帮助人们解决我的问题。 图像在另一个人的机器上工作正常,整个事情包含在一个工作簿,testing方法从来没有停止工作,新的只是它的AC + V版本。 我不重复使用子命名的testing,因为它坐在testing页面充满了我想要保持这种方式,而是我做了另一个子,并复制它,所以我可以调整新的一个需要,而不会丢失旧的工作testing和debugging。 我在XP上运行,并在胜利7进行testing,Excel是2010年。我仍然在我的头撞墙在这里找出是什么原因造成的。 我从数据库拉回来的数据是一行中的单个单元格,它是一个完整的XML,可以是从6000行到25000行的任何地方,因此可以从logging集到数组分开打印出来。

我不是在寻找解决scheme,只是使用testing方法,因为它的工作原理,我想知道为什么其他方法不能正常工作,加深我对系统的理解。 任何帮助赞赏。

空出敏感的领域,他们实际上只是没有离开细节

如果相同的代码在两台不同的机器上工作不同,我怀疑代码是好的。 也许你可以在For循环中添加以下内容?

Application.Calculation = xlManual

Application.Calculation = xlAutomatic