0
我有一個讀取文件夾中所有文件的代碼,將文件名粘貼到工作表中,並將文件數輸出到消息框中。使用VBA對文件夾中的文件排序
問題:由於某種原因,當我運行代碼時,它可能會按照錯誤的順序獲取文件,這會破壞我的計算(我使用另一個子文件)。
問題:有沒有辦法保證文件總是按正確的順序?
我做了什麼至今:編碼輸出的文件名,我想用一些代碼,在一個給定的順序文件夾中的文件進行排序之前,但我無法找到如何做到這一點的任何引用。
代碼:
Sub Counter()
Dim path As String, count As Integer, i As Long, var As Integer
Dim ws As Worksheet
Dim Filename As String
Dim FileTypeUserForm As UserForm
Dim x As String
Application.Calculation = xlCalculationManual
path = ThisWorkbook.path & "\*.*"
Filename = Dir(path)
ThisWorkbook.Sheets("FILES").Range("A:A").ClearContents
x = GetValue
If x = "EndProcess" Then Exit Sub
Set ws = ThisWorkbook.Sheets("FILES")
i = 0
Do While Filename <> ""
var = InStr(Filename, x)
If var <> 0 Then
i = i + 1
ws.Cells(i + 1, 1) = Filename
Filename = Dir()
Else: Filename = Dir()
End If
Loop
Application.Calculation = xlCalculationAutomatic
ws.Cells(1, 2) = i
MsgBox i & " : files found in folder"
End Sub
Function GetValue()
With FileTypeUserForm
.Show
GetValue = .Tag
End With
Unload FileTypeUserForm
End Function
你可以迪爾()的結果存儲在一個數組,排序此數組到一個新的然後使用這個數組。 – R3uK
@ R3uK我該怎麼做?如果我只是使用迪爾(路徑)不是它只是給我的位置,而不是內容?在這種情況下,如果我有數組,我怎樣才能用我當前的代碼訪問它(這只是使用一個字符串)? – DGMS89