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

我有一个与众多链接的工作簿的文件夹。 我想在C:\驱动器中存储它的主版本。 当有人需要使用它时,他们会点击下面的macros来复制该文件夹,询问新名字将被放置在桌面上以供使用。 下面的代码循环但不放在桌面上的文件夹。 它似乎消失了,并没有复制原件

希望有人可以帮忙?

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 

看起来问题在于这一行:

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

您将Destinationvariables设置为ToPath & strName ,所以如果用户input“My name”,那么它将是“C:\ Users \ Owner \ DesktopMy Name”。 你需要在那里放一个斜杠: Destination:=ToPath & "\" & strName