【发布时间】:2014-10-06 10:54:27
【问题描述】:
我正在尝试通过 excel 宏将一个完整的文件夹复制到一个新文件夹中,但我每次都需要用户输入新的文件夹名称
这是我复制到永久/静态文件夹的当前代码
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
我想出了一种方法让用户输入文件夹名称,但无法将此名称链接到正在创建的新文件夹
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
【问题讨论】:
-
有人吗?我确定这是我缺少的一个相当简单的步骤:)
标签: excel copy directory user-input vba