【发布时间】:2020-01-21 19:18:13
【问题描述】:
我没有使用 VBA 的经验,但我试图让用户选择一个文件夹的路径,如果该文件夹不存在则需要创建,然后提取将放入文本文件中的三列 Excel在创建的文件夹中。
在你们的帮助下,我的代码现在可以运行了!!
我应该做什么样的改变?如果你知道一种方法来简化我的代码,那就太好了!
Sub register_formated_data()
'
' register_formated_data Macro
'
Dim order As Object
Dim Folder As Object
Dim Folder_path As String
Dim lastrow As Long
FolderName = "Formated Files"
Filename = "formated" & Right(File_path, InStr(File_path, "\"))
Dim FL As String ' FL is for file location
Sheets(8).Cells(12, 12).Value = ""
With Application.FileDialog(msoFileDialogFolderPicker) '
.Title = "Select the folder" 'Open the file explorer
.InitialFileName = ThisWorkbook.path & "\" 'for you to select
.InitialView = msoFileDialogViewDetails 'the file you want
.AllowMultiSelect = True 'to format
.Show '
On Error GoTo PROC_EXIT
If Not .SelectedItems(1) = vbNullString Then FL = .SelectedItems(1)
End With
Sheets(8).Cells(12, 12).Value = FL
Folder_path = FL + "\" + FolderName
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(Folder_path & FolderName) Then
fso.CreateFolder(Folder_path).Name = FolderName
End If
PROC_EXIT: 结束子
【问题讨论】:
-
您可能需要查看
MkDir以创建目录或文件夹。 -
@alexilas 用户如何选择不存在的文件夹的路径?用户可以选择父文件夹并检查其中的文件夹。例如,用户选择 c:\temp\,如果文件夹 c:\tenp\Formated Files\ 不存在,则创建它。你是这个意思吗?
-
@CDP1802 是的,这就是我想说的。从我在“mkdir”上看到的内容来看,这似乎是我应该考虑的事情! ...我不确定我应该如何实现 mkdir,但我会搜索一下
-
@Cyril 我找到了一种让我的代码工作的方法,但我对
mkdirmethode 很感兴趣!!有关于stackoverflow的例子吗?? -
@alexilas 如果您在帖子中使用 FileSystemObject,您可以在同一对象上使用 CreateTextFile 方法来创建转储文件。