VBA脚本重命名我复制的文件不起作用

时间:2014-11-25 21:43:22

标签: vba

我相信我在使用复制文件和重命名方面处于正确状态,但是我的脚本仍然悬挂在......"运行时错误' 5':无效程序调用或参数。" ......在最后一行" ObjFso1.MoveFile SourceLocation& "" &安培; SourceFileName,DestinationLocation& "" &安培;目的地文件名" 而且,我不确定如何解决它。

代码:

Sub moveTxt()
year_Month = Format(DateAdd("m", 1, Date), "yyyy mmm")
yearMonth = Format(DateAdd("m", 1, Date), "yyyymmm")
Yr = Format(Application.WorksheetFunction.EoMonth(Date, 1), "yyyy")
Mnth = Format(Application.WorksheetFunction.EoMonth(Date, 1), "mmm")
'
Dim ObjFso
Dim ObjFso1
Dim StrSourceLocation
Dim StrDestinationLocation
Dim SourceLocation
Dim DestinationLocation
Dim SourceFileName
Dim DestinationFileName
'
' Copying the *.txt files to the working directory
'
Dim StrSourceFileName
Dim StrDestinationFileName

StrSourceLocation = "\\san\PPC_VENDOR\"
StrDestinationLocation = "\\san\PPC\WORK BASKETS\Forms and Files\" & Yr & "\" & Yr & " " & Mnth & "\Vendor Recon Files\"    'All text files will be copied to destination
StrSourceFileName = "*.txt"                                                    'Creating the file system object
'
Set ObjFso = CreateObject("Scripting.FileSystemObject")              'Copying the file
ObjFso.CopyFile StrSourceLocation & "" & StrSourceFileName, StrDestinationLocation & "", True
'
' Renaming the *.txt files
'
SourceLocation = "\\san\PPC\WORK BASKETS\Forms and Files\" & Yr & "\" & Yr & " " & Mnth & "\Vendor Recon Files\"
DestinationLocation = "\\san\PPC\WORK BASKETS\Forms and Files\" & Yr & "\" & Yr & " " & Mnth & "\Vendor Recon Files\"
SourceFileName = "BC*.txt"
DestinationFileName = "vendor_ins_" & yearMonth & "_v56_*.txt"  'Creating the file system object
SourceFileName = "BS*.txt"
DestinationFileName = "vendor_ins_" & yearMonth & "_v55_*.txt"  'Creating the file system object
SourceFileName = "DD*.txt"
DestinationFileName = "vendor_ins_" & yearMonth & "_v57_*.txt"  'Creating the file system object
SourceFileName = "NC*.txt"
DestinationFileName = "vendor_ins_" & yearMonth & "_v54_*.txt"  'Creating the file system object
'
Set ObjFso1 = CreateObject("Scripting.FileSystemObject")             'Moving the file
ObjFso1.MoveFile SourceLocation & "" & SourceFileName, DestinationLocation & "" & DestinationFileName

End Sub

你有什么想法吗? JD

2 个答案:

答案 0 :(得分:0)

您重新定义了SourceFileName和DestinationFileName 4次。每一个都不是唯一的。您必须使用Select CaseIf Then语句根据给定条件定义每个语句。或者您必须在每个变量定义之间使用MoveFile代码。

编辑:也可以尝试

Dim SourcePath As String
Dim DestPath As String
SourcePath = SourceDestination & SourceFileName
DestPath = DestinationLocation & DestinationFileName
ObjFso1.MoveFile Source:=SourcePath, Destination:=DestPath

答案 1 :(得分:0)

我对代码做了一些更改,因为上面的脚本没有完成我想要的。请告诉我在这个新剧本中我需要改变什么...

Sub moveTxt()
year_Month = Format(DateAdd("m", 1, Date), "yyyy mmm")
yearMonth = Format(DateAdd("m", 1, Date), "yyyymmm")
Yr = Format(Application.WorksheetFunction.EoMonth(Date, 1), "yyyy")
Mnth = Format(Application.WorksheetFunction.EoMonth(Date, 1), "mmm")
'
Dim ObjFso
Dim SourcePath As String
Dim DestPath As String
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'
SourceLocation = "\\san\PPC_VENDOR\"
DestinationLocation = "\\san\PPC\WORK BASKETS\Forms and Files\" & Yr & "\" & Yr & " " & Mnth & "\Vendor Recon Files\"
SourcePath = SourceLocation & SourceFileName
DestPath = DestinationLocation & DestinationFileName
'
SourceFileName = "BC*.txt"
DestinationFileName = "vendor_ins_" & yearMonth & "_v56.txt"
ObjFso.MoveFile Source:=SourcePath, Destination:=DestPath
'
SourceFileName = "BS*.txt"
DestinationFileName = "vendor_ins_" & yearMonth & "_v55.txt"
ObjFso.MoveFile Source:=SourcePath, Destination:=DestPath
'
SourceFileName = "DD*.txt"
DestinationFileName = "vendor_ins_" & yearMonth & "_v57.txt"
ObjFso.MoveFile Source:=SourcePath, Destination:=DestPath
'
SourceFileName = "NC*.txt"
DestinationFileName = "vendor_ins_" & yearMonth & "_v54.txt"
ObjFso.MoveFile Source:=SourcePath, Destination:=DestPath

End Sub

此代码在第19行停止,使用"运行时错误' 53':找不到文件"。

感谢您的任何想法,将不胜感激, JD