修改后的代码:现在给出错误“下标超出范围”。
我遇到以下代码的问题。
此代码在同一文件夹中制作文件的多个副本。
除此之外,我在代码中添加了额外的语句,允许将新文件的“文件名”复制到已创建的新工作簿中的新工作表中。
在运行它给出一个错误:application defined or object defined error
谁能告诉我代码中缺少什么?非常感谢您的帮助。
Sub CopyFile()
Dim i As Integer
Dim j As Integer
Dim Subfolder As String
Dim Sourcefile As String
Dim Targetfile As String
Dim targetwb As Workbook
Dim targetsheet As Worksheet
Dim myFSO As Object
Subfolder = "J:\Temp\Data\Report\"
Sourcefile = "Hospital .xls" 'The original file name
Set myFSO = CreateObject("Scripting.FileSystemObject")
'loop from A2 to A53
i = 2
Do While ActiveSheet.Cells(i, 1).Value <> Empty
'determine Targetfilename
Targetfile = Subfolder & ActiveSheet.Cells(i, 1).Value & ".xls"
'copy file
myFSO.CopyFile Subfolder & Sourcefile, Targetfile, True 'true will overwrite existing files
i = i + 1
Loop
Set myFSO = Nothing
targetwb = Workbooks(Targetfile)
targetsheet = targetwb("Sheet12")
j = 2
Do While targetsheet.Cells(j, 3).Value <> Empty
targetsheet.Cells(j, 3).copy
targetsheet.Cells(4).Paste
j = j + 1
Loop
End Sub