【问题标题】:Excel VBA 2007 Performance Issue on Windows 7 (64 Bit)Windows 7(64 位)上的 Excel VBA 2007 性能问题
【发布时间】:2013-01-10 10:01:06
【问题描述】:

我是 VBA 新手。我在 Microsoft Excel 2007 中编写了一个 VBA 代码来发送 SOAP 消息并处理soap 响应。 我正在读取 Soap 响应并将值循环写入 Excel 单元格。 示例肥皂响应是:-

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns2:getMyResponse xmlns:ns2="http://my.samplenamesapce/">
        <return>
            <col1>col1Value</col1>
            <col2>col2Value</col2>
            <col3>3.283E7</col3>
        </return>
        <return>
            <col1>col1Value</col1>
            <col2>col2Value</col2>            
        </return>       
      </ns2:getMyResponse>
   </soap:Body>
</soap:Envelope>

在单元格中写入响应的 VBA 代码是:

Private Sub getReportBtn_Click()
Dim WS As Worksheet: Set WS = Worksheets("my_report")
Dim request As New MSXML2.XMLHTTP60
Dim url As String
Dim response As New MSXML2.DOMDocument60
Dim requestData As String
    'Clear the Report Sheet
    WS.Range("A2:C65536").ClearContents
    url = Range("url").Value
    'Construct SOAP REQUEST
    request.Open "POST", url, False
    request.setRequestHeader "Content-Type", "text/xml; charset=""UTF-8"""
    request.setRequestHeader "SOAPAction", ""
    'Create SOAP REQUEST BODY
    requestData = xmlBody
    request.setRequestHeader "Content-Length", Len(requestData)
    On Error GoTo err_handler:

    'Send SOAP REQUEST  
    request.send requestData
    'Read SOAP RESPONSE
    response.LoadXML request.responseText

    Dim MyReport As MSXML2.IXMLDOMNode
    Dim MyReportList As MSXML2.IXMLDOMNodeList
    Set MyReportList = response.getElementsByTagName("return")
    Dim i As Integer
    i = 1
    For Each MyReport In MyReportList
        i = i + 1
        WS.Range("col1Range").Cells(i).Value =  MyReport.selectSingleNode("col1").Text
        WS.Range("col2Range").Cells(i).Value =  MyReport.selectSingleNode("col2").Text
        If MyReport.SelectNodes("col3").Length > 0 Then
        WS.Range("col3Range").Cells(i).Value =  MyReport.selectSingleNode("col3").Text
        End If
    Next MyReport
    If i = 1 Then
        MsgBox "No data found for requested query!"
        Sheets("query").Select
        Exit Sub
    End If   
Sheets("my_report").Select
Exit Sub

err_handler:
    MsgBox "Error occurred during submission, please check your settings."
Exit Sub
End Sub

当我在 Windows 7(64 位)上运行我的 Excel 工作表时,与在 Windows XP(32 位)上运行它相比,它需要双倍的时间来处理相同的数据。 除了操作系统之外,两台机器的配置和物理位置都相同。

【问题讨论】:

  • 它们是 Excel 32 位还是 Windows 7 上的 64 位?它可以解释性能差异(虽然我不知道确切的原因)
  • @nick-perkins 都是 Excel 2007(32 位)。
  • 对不起,我不确定。

标签: excel vba


【解决方案1】:

您可能需要将变量声明为 32 位变量。要查看变量声明是否是延迟区域,请在代码的开头和结尾设置计时器,并在 WS.range clearContents 之前设置第三个计时器。我的预感是分配内存空间的时间是 64 位机器上的两倍。

【讨论】:

    猜你喜欢
    • 2011-04-01
    • 1970-01-01
    • 2013-06-06
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 2011-10-30
    相关资源
    最近更新 更多