excelmacros将一个文件夹复制到另一个文件夹,用户input的文件夹名称

我试图通过Excelmacros将一个完整的文件夹复制到一个新的文件夹,但我需要用户每次input新的文件夹名称

这是我现在的代码复制到永久/静态文件夹

Sub Copy_Folder() Dim FSO As Object Dim FromPath As String Dim ToPath As String FromPath = "C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\3- FINAL Country Files\1" '<< Change ToPath = "C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\Weekly Back" '<< Change Application.CutCopyMode = False If Right(FromPath, 1) = "\" Then FromPath = Left(FromPath, Len(FromPath) - 1) End If If Right(ToPath, 1) = "\" Then ToPath = Left(ToPath, 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 MsgBox "You can find the files and subfolders from " & FromPath & " in " & ToPath End Sub 

我为用户input一个文件夹名称的方法,但无法将此名称链接到正在创build的新文件夹

 Dim strName As String Dim WeekStr1 As String Dim WeekStr2 As String Reenter: strName = InputBox(Prompt:="Enter the week you would like to update", _ Title:="Week Selection.", Default:="0") If strName = vbNullString Then Exit Sub Else Select Case strName Case Else MsgBox "Incorrect Entry." GoTo Reenter End Select End If 

我需要将“StrName”放置在下面的上下文中,但它似乎无法获得正确的语法

 ToPath = "C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\Week "StrName"" '<< Change 

也许像下面?

 ToPath = "C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\Week" & StrName 

连接文本/string只需使用& (&符号)。 + (加)也可以,但我很满意&

谢谢,我想通过这个问题:)基本上我不得不添加StrName
FSO.CopyFolder Source:= FromPath,Destination:= ToPath&strName

有时最简单的问题是最糟糕的。 谢谢你的帮助

下面是最后的代码供将来参考,以防其他人陷入困境

 Sub Copy_Folder() Dim FSO As Object Dim FromPath As String Dim ToPath As String Dim strName As String Dim WeekStr1 As String Dim WeekStr2 As String FromPath = "C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\3- FINAL Country Files\KSA" '<< Change ToPath = "C:\Users\hayekn\Desktop\AR Reports\0MENACA Working File\AR Working File\Week" Application.CutCopyMode = False Reenter: strName = InputBox(Prompt:="Enter the week you would like to update", _ Title:="Week Selection.", Default:="0") 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