使用VBA复制和重命名文件夹

时间:2021-03-12 10:46:32

I have a folder with numerous linked workbooks. I would like to store a master copy of it within the C:\ drive. When someone needs to use it they would click on the below macro to copy the folder, ask what the new name will be and place it on the desktop for use. The below code cycles through but does not place the folder on the desktop. It just seems to disappear and does not copy the original

我有一个包含大量链接工作簿的文件夹。我想在C:\驱动器中存储它的主副本。当有人需要使用它时,他们会点击下面的宏来复制文件夹,询问新名称是什么,并将其放在桌面上以供使用。下面的代码循环,但不会将文件夹放在桌面上。它似乎消失了,并没有复制原件

Hoping someone can help??

希望有人能帮忙吗?

Sub Copy_Folder()
Dim FSO As Object
Dim FromPath As String
Dim ToPath As String
Dim strName As String


FromPath = "C:\v4 Master Operations Folder"  
ToPath = "C:\Users\Owner\Desktop"
Application.CutCopyMode = False


Reenter:
strName = InputBox(Prompt:="Enter the name of your operation", _
Title:="Operation.", Default:=" ")

If strName = vbNullString Then
MsgBox "Incorrect Entry."
GoTo Reenter

End If

If Right(FromPath, 1) = "\" Then
    FromPath = Left(FromPath, Len(FromPath) - 1)
End If

If Right(ToPath, 1) = "\" Then
    ToPath = Left(ToPath & strName, Len(ToPath) - 1)
End If

Set FSO = CreateObject("scripting.filesystemobject")

If FSO.FolderExists(FromPath) = False Then
    MsgBox FromPath & " doesn't exist"
    Exit Sub
End If

FSO.CopyFolder Source:=FromPath, Destination:=ToPath & strName
MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath & strName

End Sub

1 个解决方案

#1


3  

It looks like the problem is on this line:

看起来问题出在这一行:

FSO.CopyFolder Source:=FromPath, Destination:=ToPath & strName

You are setting your Destination variable equal to ToPath & strName, so if the user enters "My name" then it would be "C:\Users\Owner\DesktopMy Name". You need to put a slash in there: Destination:=ToPath & "\" & strName

您将Destination变量设置为ToPath&strName,因此如果用户输入“我的名字”,那么它将是“C:\ Users \ Owner \ DesktopMy Name”。你需要在那里放一个斜杠:Destination:= ToPath&“\”&strName

#1


3  

It looks like the problem is on this line:

看起来问题出在这一行:

FSO.CopyFolder Source:=FromPath, Destination:=ToPath & strName

You are setting your Destination variable equal to ToPath & strName, so if the user enters "My name" then it would be "C:\Users\Owner\DesktopMy Name". You need to put a slash in there: Destination:=ToPath & "\" & strName

您将Destination变量设置为ToPath&strName,因此如果用户输入“我的名字”,那么它将是“C:\ Users \ Owner \ DesktopMy Name”。你需要在那里放一个斜杠:Destination:= ToPath&“\”&strName