excelvba-高效循环二维数组

excelvba-高效循环二维数组,excel,vba,scripting,Excel,Vba,Scripting,我正无可救药地试图找到一种更好的方法来填充一系列内容。这种方法产生正确的结果,但速度非常慢。有人能告诉我如何填充2d数组或以其他方式加速算法的正确方向吗?我会喜欢有人成功地使用的代码片段,甚至只是显示更干净方法的链接 here is my OLD code: ---------------- f = 1 maxcol = 'func call to get last non blank col ref .ie could return T, R, H.etc For f

我正无可救药地试图找到一种更好的方法来填充一系列内容。这种方法产生正确的结果,但速度非常慢。有人能告诉我如何填充2d数组或以其他方式加速算法的正确方向吗?我会喜欢有人成功地使用的代码片段,甚至只是显示更干净方法的链接

here is my OLD code:
----------------
    f = 1
    maxcol = 'func call to get last non blank col ref .ie could return T, R, H.etc

    For f = 1 To UBound(filenames)
        Set aDoc = LoadXmlDoc(filenames(f))
        For Each c In Worksheets("Results").Range("A1:" & maxcol & "1")
                                        c.Offset(f, 0).Value = aNode.Text
                    Next c
        Worksheets("Results").Range(maxcol & "1").Offset(f, 0).Value = filenames(f)
    Next f


UPDATED CODE:
----------

Dim aDoc As DOMDocument
Dim aNode As IXMLDOMNode
Dim numOfXpaths As Integer
Dim filenames As Variant
Dim f As Integer
Dim maxcol As String
Dim rngStart As Range
Dim nColIndex As Long
Dim lngCalc As Long
'Dim numOfFiles As Integer
Dim aXpaths As Variant
        numOfFiles = UBound(filenames)
    colToRow aXpaths, numOfXpaths
    maxcol = Number2Char(numOfXpaths)
        ReDim aValues(1 To numOfFiles, 1 To numOfXpaths + 1) As Variant
        For f = 1 To numOfFiles
            Set aDoc = LoadXmlDoc(filenames(f))
            For nColIndex = 1 To numOfXpaths
                    If aDoc.parseError Then
                        aValues(f, nColIndex) = "XML parse error:" 
                    Else
                      Set aNode = aDoc.selectSingleNode(aXpaths(nColIndex))
                      aValues(f, nColIndex) = aNode.Text
                    End If
            Next nColIndex
            aValues(f, numOfXpaths + 1) = filenames(f)
        Next f
        Worksheets("Results").Range("A1").Offset(1, 0).Resize(numOfFiles, numOfXpaths + 1).Value = aValues


    Function colToRow(ByRef aXpaths As Variant, ByRef numOfXpaths As Integer)
    Dim xpathcount As Integer
    Dim c As Integer
    'Dim aXpaths As Variant
    xpathcount = Worksheets("Xpaths").Cells(Rows.Count, "A").End(xlUp).Row - 1
    ReDim aXpaths(1 To xpathcount + 1) As Variant
    For c = 0 To xpathcount
        Worksheets("Results").Range("A1").Offset(0, c) = Worksheets("Xpaths").Range("A1").Offset(c, 0)
        Worksheets("Results").Range("A1").Offset(0, c).Columns.AutoFit
        aXpaths(c + 1) = Worksheets("Xpaths").Range("A1").Offset(c, 0)
    Next c
    Worksheets("Results").Range("A1").Offset(0, xpathcount + 1) = "Filename"
    'colToRow = xpathcount + 1
    numOfXpaths = xpathcount + 1
    End Function

Function Number2Char(ByVal c) As String
Number2Char = Split(Cells(1, c).Address, "$")(1)
End Function

当您从XML获得结果时,您是否考虑过使用XML映射来显示信息?可能不适合您的情况,但值得一试

下面展示了在Excel中使用XML映射的一些内容

将XML字符串加载到定义映射的行的语法类似于:

ActiveWorkbook.XmlMaps("MyMap").ImportXml(MyXMLDoc,True)

当您从XML获得结果时,您是否考虑过使用XML映射来显示信息?可能不适合您的情况,但值得一试

下面展示了在Excel中使用XML映射的一些内容

将XML字符串加载到定义映射的行的语法类似于:

ActiveWorkbook.XmlMaps("MyMap").ImportXml(MyXMLDoc,True)

您可能希望查看我在使用Excel VBA中的变量数组进行大规模数据操作中的代码,详细信息请参见超链接

请注意,由于我没有上面的数据,本文提供了一个示例解决方案,在这种情况下,可以有效地删除前导零,以满足您填充2d数组范围的要求

需要注意的要点

该代码通过使用区域来处理非连续范围 使用变体数组时,请测试设置数组大小的范围是否大于1个单元格-如果不是,则不能使用变体 代码从一个范围读取,运行一个操作,然后转储回同一个范围 使用Value2比使用Value2效率稍高 代码如下:

'Press Alt + F11 to open the Visual Basic Editor (VBE)
'From the Menu, choose Insert-Module.
'Paste the code into the right-hand code window.
'Press Alt + F11 to close the VBE
'In Xl2003 Goto Tools … Macro … Macros and double-click KillLeadingZeros

Sub KillLeadingZeros()
    Dim rng1 As Range
    Dim rngArea As Range
    Dim lngRow As Long
    Dim lngCol As Long
    Dim lngCalc As Long
    Dim objReg As Object
    Dim X()


    On Error Resume Next
    Set rng1 = Application.InputBox("Select range for the replacement of leading zeros", "User select", Selection.Address, , , , , 8)
    If rng1 Is Nothing Then Exit Sub
    On Error GoTo 0

    'See Patrick Matthews excellent article on using Regular Expressions with VBA
    Set objReg = CreateObject("vbscript.regexp")
    objReg.Pattern = "^0+"

    'Speed up the code by turning off screenupdating and setting calculation to manual
    'Disable any code events that may occur when writing to cells
    With Application
        lngCalc = .Calculation
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With

    'Test each area in the user selected range

    'Non contiguous range areas are common when using SpecialCells to define specific cell types to work on
    For Each rngArea In rng1.Areas
        'The most common outcome is used for the True outcome to optimise code speed
        If rngArea.Cells.Count > 1 Then
           'If there is more than once cell then set the variant array to the dimensions of the range area
           'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks    
            X = rngArea.Value2
            For lngRow = 1 To rngArea.Rows.Count
                For lngCol = 1 To rngArea.Columns.Count
                    'replace the leading zeroes
                    X(lngRow, lngCol) = objReg.Replace(X(lngRow, lngCol), vbNullString)
                Next lngCol
            Next lngRow
            'Dump the updated array sans leading zeroes back over the initial range
            rngArea.Value2 = X
        Else
            'caters for a single cell range area. No variant array required
            rngArea.Value = objReg.Replace(rngArea.Value, vbNullString)
        End If
    Next rngArea

    'cleanup the Application settings
    With Application
        .ScreenUpdating = True
        .Calculation = lngCalc
        .EnableEvents = True
    End With

    Set objReg = Nothing
    End Sub

您可能希望查看我在使用Excel VBA中的变量数组进行大规模数据操作中的代码,详细信息请参见超链接

请注意,由于我没有上面的数据,本文提供了一个示例解决方案,在这种情况下,可以有效地删除前导零,以满足您填充2d数组范围的要求

需要注意的要点

该代码通过使用区域来处理非连续范围 使用变体数组时,请测试设置数组大小的范围是否大于1个单元格-如果不是,则不能使用变体 代码从一个范围读取,运行一个操作,然后转储回同一个范围 使用Value2比使用Value2效率稍高 代码如下:

'Press Alt + F11 to open the Visual Basic Editor (VBE)
'From the Menu, choose Insert-Module.
'Paste the code into the right-hand code window.
'Press Alt + F11 to close the VBE
'In Xl2003 Goto Tools … Macro … Macros and double-click KillLeadingZeros

Sub KillLeadingZeros()
    Dim rng1 As Range
    Dim rngArea As Range
    Dim lngRow As Long
    Dim lngCol As Long
    Dim lngCalc As Long
    Dim objReg As Object
    Dim X()


    On Error Resume Next
    Set rng1 = Application.InputBox("Select range for the replacement of leading zeros", "User select", Selection.Address, , , , , 8)
    If rng1 Is Nothing Then Exit Sub
    On Error GoTo 0

    'See Patrick Matthews excellent article on using Regular Expressions with VBA
    Set objReg = CreateObject("vbscript.regexp")
    objReg.Pattern = "^0+"

    'Speed up the code by turning off screenupdating and setting calculation to manual
    'Disable any code events that may occur when writing to cells
    With Application
        lngCalc = .Calculation
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .EnableEvents = False
    End With

    'Test each area in the user selected range

    'Non contiguous range areas are common when using SpecialCells to define specific cell types to work on
    For Each rngArea In rng1.Areas
        'The most common outcome is used for the True outcome to optimise code speed
        If rngArea.Cells.Count > 1 Then
           'If there is more than once cell then set the variant array to the dimensions of the range area
           'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks    
            X = rngArea.Value2
            For lngRow = 1 To rngArea.Rows.Count
                For lngCol = 1 To rngArea.Columns.Count
                    'replace the leading zeroes
                    X(lngRow, lngCol) = objReg.Replace(X(lngRow, lngCol), vbNullString)
                Next lngCol
            Next lngRow
            'Dump the updated array sans leading zeroes back over the initial range
            rngArea.Value2 = X
        Else
            'caters for a single cell range area. No variant array required
            rngArea.Value = objReg.Replace(rngArea.Value, vbNullString)
        End If
    Next rngArea

    'cleanup the Application settings
    With Application
        .ScreenUpdating = True
        .Calculation = lngCalc
        .EnableEvents = True
    End With

    Set objReg = Nothing
    End Sub

为了有效地完成这项工作,您应该使用要写入的数据生成二维数据,然后一次性写入所有数据

类似下面的内容。为了与其他语言兼容,我更喜欢基于0的数组,而您似乎在使用基于1的数组1到uboundfilename。因此,在以下未经测试的代码中可能会出现一个接一个的错误:

f = 1
maxcol = 'func call to get last non blank col ref .ie could return T, R, H.etc

' 2D array to hold results    
' 0-based indexing: UBound(filenames) rows and maxcol columns
Dim aValues(0 to UBound(filenames)-1, 0 To maxcol-1) As Variant
Dim rngStart As Range
Dim nColIndex As Long

For f = 1 To UBound(filenames)
    Set aDoc = LoadXmlDoc(filenames(f))

    aValues(f-1, 0) = filenames(f)

    For nColIndex = 1 To maxCol-1
        aValues(f-1, nColIndex) = aNode.Text
    Next nColIndex

Next f

' Copy the 2D array in one go
Worksheets("Results").Offset(1,0).Resize(UBound(filenames),maxCol).Value = aValues

为了有效地完成这项工作,您应该使用要写入的数据生成二维数据,然后一次性写入所有数据

类似下面的内容。为了与其他语言兼容,我更喜欢基于0的数组,而您似乎在使用基于1的数组1到uboundfilename。因此,在以下未经测试的代码中可能会出现一个接一个的错误:

f = 1
maxcol = 'func call to get last non blank col ref .ie could return T, R, H.etc

' 2D array to hold results    
' 0-based indexing: UBound(filenames) rows and maxcol columns
Dim aValues(0 to UBound(filenames)-1, 0 To maxcol-1) As Variant
Dim rngStart As Range
Dim nColIndex As Long

For f = 1 To UBound(filenames)
    Set aDoc = LoadXmlDoc(filenames(f))

    aValues(f-1, 0) = filenames(f)

    For nColIndex = 1 To maxCol-1
        aValues(f-1, nColIndex) = aNode.Text
    Next nColIndex

Next f

' Copy the 2D array in one go
Worksheets("Results").Offset(1,0).Resize(UBound(filenames),maxCol).Value = aValues

+1.新想法。我很想知道在这个场景中是否有任何通用的非xml循环方法。+1表示新想法。我想知道在此场景中是否有任何通用的非xml循环方法。您确定这是在填充问题所在的范围内容,而不是加载xml文件和进行XPath查找吗?您是否关闭了屏幕更新和计算功能?是的,它会生成正确的结果,但只需大约6分钟就可以在5000个文件中通过2个XPath,每个文件的大小约为5-7KB。似乎您如何将结果写入工作表并不重要:您6分钟的大部分时间可能都花在加载和查询文件上。我不知道你怎样才能使那部分更快,除非你把它们存储在一个慢的驱动器上。如果你展示了更多的代码,可能会有一些建议-你似乎跳过了一些行?@TimWilliams-我已经添加了更多的代码并进行了一些更新。我不确定你如何使这明显更快。只需注释掉这些部分并重新运行sub,您就可以轻松检查打开和解析文件所花费的时间。您确定这是在填充问题所在的范围内容,而不是加载XML文件和执行XPath查找吗?您是否关闭了屏幕更新和计算功能?是的,它会生成正确的结果,但只需大约6分钟就可以在5000个文件中通过2个XPath,每个文件的大小约为5-7KB。似乎您如何将结果写入工作表并不重要:您6分钟的大部分时间可能都花在加载和查询文件上。我不知道你怎样才能使那部分更快,除非你把它们存储在一个慢的驱动器上。如果你展示更多的代码,可能会有一些建议-y
你似乎跳过了一些行?@TimWilliams-我在一些更新中添加了更多的代码。我不确定你是如何使这一速度明显加快的。只需注释掉这些部分并重新运行sub,就可以轻松检查打开和解析文件所花费的时间。