如問題:任務是從文件夾中提取所有文件名,但文件夾路徑需要硬編碼到宏中,以防止這些對話框向我提出問題並浪費我的時間。 我不會更改此文件夾。直到時間結束時它將是同一個,我想從第二行開始將文件名提取到Excel列中。 這是我想從中提取所有文件名的文件夾。 「C:\ Users \用戶的Michal \網盤\ CSV \波薩\ mstcgl_mst \」VBA;如何從文件夾中提取所有文件名 - 無需使用Application.FileDialog對象
這是我的代碼部分:
Option Explicit
Sub GetFileNames()
Dim axRow As Long ' inside the Sheet("Lista") row#
Dim xDirectory As String
Dim xFname As String ' name of the file
Dim InitialFoldr$
Dim start As Double
Dim finish As Double
Dim total_time As Double
start = Timer
ThisWorkbook.Sheets("Lista").Range("D2").Activate
InitialFolder = "C:\Users\michal\SkyDrive\csv\bossa\mstcgl_mst"
If Right(InitialFolder, 1) <> "\" Then
InitialFolder = InitialFolder & "\"
End If
Application.InitialFolder.Show
If InitialFolder.SelectedItems.Count <> 0 Then
xDirectory = .SelectedItems(1) & "\"
xFname = Dir(xDirectory, vbArchive)
' Dir's job is to return a string representing
' the name of a file, directory, or an archive that matches a specified pattern.
Do While xFname <> "" ' there is already xFname value (1st file name) assigned.
ActiveCell.Offset(xRow) = xFname
xRow = xRow + 1 ' następny xRow
xFname = Dir()
Loop
End If
End With
finish = Timer ' Set end time.
total_time = Round(finish - start, 3) ' Calculate total time.
MsgBox "This code ran successfully in " & total_time & " seconds", vbInformation
End Sub
這是壓碎行: If InitialFolder.SelectedItems.Count <> 0 Then xDirectory = .SelectedItems(1) & "\"
而.png文件中兩個更重要的問題。 請回復他們 - 這對我來說非常重要。
或者如果你們知道其他方法可以更快地做到這一點,只要不要猶豫,與我分享你的代碼 - 我將非常感激。
請解釋一下這個變量需要什麼? 'Dim flag As Boolean'它在代碼中的作用是什麼? –
它循環文件直到'directory =「」'所有文件都被檢查過。你也可以說'While varDirectory <>「」'。它沒有寫這段代碼。 – UGP
爲什麼'varDirectory'變暗變爲Variant?它不是持有字符串值嗎?請稍微解釋一下。我只是初學者,所以我需要了解這一點。 –