【问题标题】:Loop through folder using array to find lastest version (count) with VBA?使用数组遍历文件夹以使用 VBA 查找最新版本(计数)?
【发布时间】:2015-05-12 10:32:46
【问题描述】:

我附上了一个代码,但是,这只会找到文件夹中存在的文件。
我想要的是文件的增量计数器。问题是有时版本会以 0 或 1 以外的值开始,例如3.
Amesto non AN suppliers TEST W20-3 AN 然后我希望下一个字符串是 4。

我目前正在使用它,但它只有在 1 是第一个时才有效,等等。 我真的被困住了。

' Version check
    Do While Len(Dir(strPath2 & "Amesto non AN suppliers TEST W" & week & "-" & version & "*.cif")) <> 0
        version = version + 1
        strPath = getDirectoryPath & "Amesto non AN suppliers TEST W" & week & "-" & version & " " & UserName & ".cif"
    Loop


Sub loadversion()
Dim MyFile As String
Dim Counter As Long

'Create a dynamic array variable, and then declare its initial size
Dim DirectoryListArray() As String
ReDim DirectoryListArray(1000)

'Loop through all the files in the directory by using Dir$ function
MyFile = Dir$("C:\Users\niclas.madsen\Desktop\AP\WAVE3\CIF\*.*")
Do While MyFile <> ""
    DirectoryListArray(Counter) = MyFile
    MyFile = Dir$
    Counter = Counter + 1
Loop

' do something here?!
If MyFile = vbNullString Then

Else

End If 

'Reset the size of the array without losing its values by using Redim Preserve
ReDim Preserve DirectoryListArray(Counter - 1)

For Counter = 0 To UBound(DirectoryListArray)
    'Debug.Print writes the results to the Immediate window (press Ctrl + G to view it)'
    Debug.Print DirectoryListArray(Counter)
Next Counter 
End Sub

【问题讨论】:

  • 似乎这不是整个代码。第一个循环位于哪个子系统中?另一个问题:如果版本不以1开头,它会给你一个错误吗?
  • 嗨@EngJon,第一个“版本检查”来自我保存文件的子目录。但是,这个 sub 只是一个测试,看看我是否可以让它工作,然后替换那个版本检查。不,现在数组只会列出文件夹中的文件数量,但不会保存或做任何其他事情。版本检查将转到第一个可用值,因此如果存在的文件是 3 和 4,它将以 1 开头 :( 我希望它是 5。希望您理解。
  • 版本检查只是假设没有更高的版本。如果您知道起始版本永远不会高于 4,您不仅可以在循环中检查版本 1,还可以强制它查找版本 4。您可以在实际循环之前的单独 for 循环中执行此操作。
  • 我不知道会有多少,它可以从 1 到 9 不等(我会说)。问题是我们将这个文件导入到文件夹中,所以有时我们会导入版本 4,因此我们需要创建版本 5。所以如果宏可以自动回忆文件夹中找到的最高数字,那就太酷了.
  • 您可以遍历文件夹中的所有文件,剪切第一部分(名称和周),然后逐个检查字符直到它不再是数字值,然后从该字符剪切 -> 你得到版本。然后将这些字符串转换为数字,将它们放入数组(列表)中并对其进行排序。这样你会得到最高的数字

标签: arrays vba excel counter


【解决方案1】:

要获取目录中文件名的最高版本,请插入以下函数:

Function CheckHighestVersion(path As String, cutLettersAtWordBeginning As Integer) As Integer
    Dim file As Variant
    Dim toBeCut As String
    Dim verLength As Integer
    Dim highestVersion As Integer
    highestVersion = 0
    file = Dir(path)

    While (file <> "")
        toBeCut = file
        toBeCut = Mid(toBeCut, cutLettersAtWordBeginning + 1)
        verLength = FindVerLength(toBeCut)
        If verLength = -1 Then
            CheckHighestVersion = 0
            Exit Function
        End If
        toBeCut = Left(toBeCut, verLength)
        If Val(toBeCut) > highestVersion Then
            highestVersion = Val(toBeCut)
        End If
        file = Dir
    Wend
    CheckHighestVersion = highestVersion
End Function

Function FindVerLength(fileName As String) As Integer
    Dim i As Integer
    For i = 1 To Len(fileName)
        If Not IsNumeric(Mid(fileName, i, 1)) Then
            If i = 1 Then
                MsgBox "Couldn't obtain the highest version of the files: " & _
                "The first letter of the version is not numeric. The letter is " & Mid(fileName, i, 1) & _
                ". Please use correct amount of letters to be cut at the beginning of the file name."
                FindVerLength = -1
                Exit Function
            End If
            FindVerLength = i - 1
            Exit Function
        End If
    Next i
    FindVerLength = i
End Function

在您的 Sub 中调用 CheckHighestVersion。路径只是目录(例如 C:\Test\ ),第二个参数是单词开头不需要的字母数。如果我计算正确,那么在您的情况下,该值应该是 30+(周长,第 25 周为 2,第 7 周为 1)。该函数返回该文件夹中包含的最高版本。

【讨论】:

  • 嗨@Engjon!对不起,我的回复晚了,我一直很忙。我还没有检查它,但我只是称之为CheckHighestVersion(getDirectoryPath, 30)吗?它究竟会返回什么?它会返回下一个可用数字还是找到的最高数字?谢谢!
  • @Niclas Madsen 它将返回当前的最高数字
  • 非常感谢,工作几乎完美!我设法改变了它,所以版本不是星期,而是实际版本,所以我可以使用HighestVersion + 1 让它按我的意愿工作。真的很感激。
猜你喜欢
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多