我没有使用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: 结束
答案 0 :(得分:0)
fso.Name
不是FSO的方法或属性。您只需要CreateFolder
,但要确保路径以所需的文件夹名称结尾。因此应显示为:
fso.CreateFolder FFolder_Path
还要在这里指出您说Folder_path
而不是ffolder_path
。我将为您的文件夹路径命名更有意义,以免将来出现此错误。特别是考虑到它以2个重复字符开头。
只需再次查看您的代码,就不能确定为什么会有一个For
循环(从1到1),除非您希望将来有可能循环使用(尽管如果循环了,它只会连续不断地循环)覆盖FL
直到集合中的最后一项):
For lngCount = 1 To 1
'MsgBox .SelectedItems(lngCount)
On Error GoTo PROC_EXIT
If .SelectedItems(lngCount) <> "" Then 'if the file exist, folder_location take the value of the selected file
FL = .SelectedItems(lngCount)
Else
End
End If
'MsgBox "Text :" & previous_version_folder
Next lngCount
我只是说
On Error GoTo PROC_EXIT
If Not .SelectedItems(1) = vbNullString Then FL = .SelectedItems(1)