将项目添加到集合的最佳方式

时间:2015-08-27 16:49:47

标签: excel excel-vba sorting vba

我在C列中,按数字顺序列出了文件名(已经排序)。我首先循环遍历所述文件所在的文件夹,并将其添加到临时集合中。

Do While file <> ""

l = Left(file, 3)
Length = Len(file)

If (l = "C") And Length = 13 Then
    tempCol.Add file
    Debug.Print file
Else
    GoTo none
End If

none:
'goto next file
file = Dir

Loop

然后我循环遍历C列的长度,并将每一行(保存文件名)与temp集合中的项进行比较。如果在temp集合中找到filename,我将它添加到单独的集合(fCol)。

For i = 17 To cccount
cc = wsSum.Cells(i, 3).value
For Each e In tempCol
If  "C" & cc = e  Then
fCol.Add e
Debug.Print e
End If
Next e
Next i

这很好,除非我觉得可以在一个循环/集合中完成。我应该能够检查文件夹中是否存在C列中的文件名,然后将其添加到集合中。

有一个轻微的曲线球。最终的集合必须进行排序。什么是在循环期间测试项目添加以便对其进行排序的好方法?或者我应该在单独的步骤中这样做?

文件名以“C”或“S”开头,我需要按编号对其进行排序。所以它应该像

那样结束

S60100.XLSM C60100.XLSM S60270.XLSM C60270.XLSM C60275.XLSM S60275.XLSM S60280.XLSM C60280.XLSM S60285.XLSM C60285.XLSM S60290.XLSM C60290.XLSM C60295.XLSM S60295.XLSM C60300.XLSM S60500.XLSM C60500.XLSM C60501.XLSM C60503.XLSM

编辑:我的错。应该提到C列中的文件名只有数字!我知道,很遗憾抱歉:/

编辑2:添加了什么表格的图片 enter image description here

3 个答案:

答案 0 :(得分:2)

请改用阵列。然后你可以轻松地对它进行排序。此外,阵列比连续引用工作表范围快得多。

这种方法的关键方面:

[1]定义列范围,创建该大小的数组,将数据范围放入数组中。

[2]然后嵌套循环处理其余部分。对于每个文件名,循环遍历arrayColumnList(也可以使用vlookup),直到找到匹配项,或者不进行匹配。 MAtches进入最后一个阵列。

Sub ArrayList()

Dim i As Long, j As Long, k As Long

Dim arrList As Variant
    arrList = Array()

Dim lngFinalRow As Long
Dim lngColumnNumber As Long
'/ Used to define the range of your column data

Dim strLeftCharacter As String
Dim lngFileLength As Long

Dim lngFilesFound As Long

Dim arrColumnData As Variant
    arrColumnData = Array()

Dim rngColumnData As Range
    '/ set rngColumnData = Range( etc.)
    ReDim arrColumnData(1 To rngColumnData.Rows.Count, 1 To 1)
    arrColumnData = rngColumnData

'/ At this point you need to convert your numbers to strings with the filename Character. Like this:

'/ for i = Lbound(array,1) to ubound(array,1)
'/ array(i, 1) = "C" & Cstr(array(i,1))
'/ next i

Dim bMatchFound As Boolean

    '/ define dir filepath
    lngFilesFound = 0
    Do Until file = ""

        strLeftCharacter = Left(file, 1) '/ Don't know why you were using 3 when you're only checking for the first character?
        lngFileLength = Len(file)

        If strLeftCharacter = "C" And lngFileLength = 13 _
            Then
                bMatchFound = False
                i = LBound(arrColumnData, 1)
                Do While bMatchFound = False And i <= UBound(arrColumnData, 1)
                    If arrColumnData(i, 1) = file Then bMatchFound = True
                    i = i + 1
                Loop

                If bMatchFound = True _
                    Then
                        lngFilesFound = lngFilesFound + 1
                        ReDim preserve arrList(1 To lngFilesFound)
                        arrList(lngFilesFound) = file
                        Debug.Print file
                End If
        End If

        file = Dir

    Loop

End Sub

这将为您提供带有文件名的一维(I.E. list)数组。你可以通过谷歌搜索找到简单的数组排序。

答案 1 :(得分:2)

请改用ArrayListArrayList对象具有.Contains()方法(如果需要)和.Sort()方法。

Sub MacroMan()

Dim arrayList As Object
Dim WS As Object
Dim tempFile As String
Const searchFolder As String = "C:\Users\MacroMan\Folder\" '// Note trailing '\'

Set arrayList = CreateObject("System.Collections.ArrayList")
Set WS = CreateObject("WScript.Shell")

For Each tempFile In Filter(Split(WS.Exec("CMD /C DIR """ & _
    searchFolder & "*.*"" /B /A:-D").StdOut.ReadAll, vbCrLf), ".")

    If UCase(tempFile) Like "[CS][0-9][0-9][0-9][0-9][0-9][\.]XLSM" Then
        If Evaluate("=NOT(ISERROR(MATCH(" & Mid(tempFile, 2, 5) & ",C:C,0)))") Then
            arrayList.Add tempFile
        End If
    End If
Next

arrayList.Sort

'// Print results -------
    For Each x In arrayList
        Debug.Print CStr(x)
    Next
'// ---------------------

End Sub

答案 2 :(得分:1)

如果您使用初始代码的此修改,则不需要第二个集合:

Sub UsingOnlyOneCollection()

    Do While file <> ""

        l = Left(file, 3)
        Length = Len(file)

        If (l = "C") And Length = 13 Then
            If Evaluate("IFERROR(MATCH(""" & file & """,C:C,),)") Then  '<--- I added this
                tempCol.Add file
                Debug.Print file
            End If
        Else
            GoTo none
        End If

none:
        'goto next file
        file = Dir

    Loop

End Sub