我无法将 Excel 中 VBA 中的目标文件夹名称设置为我选择的单元格中的特定路径值。也就是说,我希望始终将在静态位置找到的特定文件复制到目标文件夹中,该文件夹因我用光标选择的单元格而异。
这是对之前发布的 sub 的修改:
Sub sbCopyingAFileReadFromSheet()
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String
'source file
sFile = Sheets("Main").Range("C26")
'source folder
SFolder = Sheets("Main").Range("c27")
'destination folder
sDFolder = Sheets("Main").ActiveCell.Value
''' this is not working here. I would like the destination folder
''' to be where I have my cursor but I get
''' an error : Object doesn't support this property or method '''
'Create Object for File System
Set FSO = CreateObject("Scripting.FileSystemObject")
'Checking If File Is Located in the Source Folder
If Not FSO.FileExists(sSFolder & sFile) Then
MsgBox "Specified File Not Found in Source Folder", vbInformation, "Not Found"
'Copying If the Same File is Not Located in the Destination Folder
ElseIf Not FSO.FileExists(sDFolder & sFile) Then
FSO.CopyFile (sSFolder & sFile), (sSFolder & sDFolder), True
MsgBox "Specified File Copied to Destination Folder Successfully", vbInformation, "Done!"
Else
MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation, "File Already Exists"
End If
End Sub
你可以Selection
用来替换Sheets("Main").ActiveCell.Value
. 请尝试如下 VBA 代码:
Sub sbCopyingAFileReadFromSheet()
Dim FSO
Dim sFile As String
Dim sSFolder As String
Dim sDFolder As String
'source file
sFile = Sheets("Main").Range("C26")
'source folder
SFolder = Sheets("Main").Range("c27")
'destination folder
sDFolder = Selection
'Create Object for File System
Set FSO = CreateObject("Scripting.FileSystemObject")
'Checking If File Is Located in the Source Folder
If Not FSO.FileExists(sSFolder & sFile) Then
MsgBox "Specified File Not Found in Source Folder", vbInformation, "Not Found"
'Copying If the Same File is Not Located in the Destination Folder
ElseIf Not FSO.FileExists(sDFolder & sFile) Then
FSO.CopyFile (sSFolder & sFile), (sSFolder & sDFolder), True
MsgBox "Specified File Copied to Destination Folder Successfully", vbInformation, "Done!"
Else
MsgBox "Specified File Already Exists In The Destination Folder", vbExclamation,"File Already Exists"
End If
End Sub
希望对你有帮助。期待您的回音。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句