Excel VBA-从静态文件夹名称复制文件,然后粘贴到ActiveCell中找到的文件夹名称中

时间:2018-08-21 23:16:27

标签: vba excel-vba

我无法在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

1 个答案:

答案 0 :(得分:0)

您可以使用>>> print "hello '%s'" % ('world') hello 'world' >>> print "hello '%s\%'" % ('world') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: not enough arguments for format string >>> print "hello '%s%'" % ('world') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: not enough arguments for format string >>> 替换Selection。请尝试以下VBA代码:

Sheets("Main").ActiveCell.Value

希望它对您有所帮助。期待您的回音。