【发布时间】:2017-10-19 10:23:01
【问题描述】:
我有一个文件夹(每次都是同一个文件夹 - 所以我不需要浪费时间使用 Application.FileDialog 来选择它)并且我需要将所有文件名提取到 Excel 列 C 中。
这是我在 stackoverflow 上使用 Application.FileDialog() 找到的代码,但我想硬编码文件夹的路径 (C:\Users\michal\SkyDrive\csv\bossa\mstcgl_csv) .
另一个问题我有(重要的)xDirectory$、xFname$ 和 InitialFoldr$ 变量末尾的 $ 符号是什么,为什么我不能声明它们 as String ? ?
这些变量不是字符串吗? ?
这是代码:
Sub GetFileNames()
Dim Lista As Worksheet
Dim xRow As Long
Dim xDirectory$
Dim xFname$
Dim InitialFoldr$
Dim start As Double
Dim finish As Double
Dim total_time As Double
start = Timer ' remember time when macro starts.
ThisWorkbook.Sheets("Lista").Range("C1").Select
InitialFoldr$ = "C:\Users\michal\SkyDrive\csv\bossa\"
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$
.Show ' creates list of files ? ? ?
If .SelectedItems.Count <> 0 Then
xDirectory$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirectory$, 7)
Do While xFname$ <> ""
ThisWorkbook.Sheets("Lista").ActiveCell.Offset(xRow, 0) = xFname$
ActiveCell.Offset(xRow) = xFname$
xRow = xRow + 1
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
你们可以帮我做吗? 我只是在学习我的 VBA 基础知识,但我仍然不明白很多东西。 请回答 $ 符号问题:-)
【问题讨论】:
-
Dim xFname$是Dim xFname as String的简写,只是可读性较差,所以通常你不会经常看到它。