修改代码:现在给出错误“下标超出范围”。
我遇到以下代码的问题。
此代码在同一文件夹中制作文件的多个副本。
除此之外,我还在代码中添加了其他语句,这些语句将允许将新文件的“文件名”复制到已创建的新工作簿中的新工作表中。
在运行时给出错误: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
答案 0 :(得分:2)
问题很可能是行和变量columnCount以及对“D”的引用。请参阅下面有关值的评论。细胞需要。更新此项以反映columnCount变量并将“D”更改为4以表示第4列又名“D”:)
dim columnCount as Integer
columnCount = 10
targetsheet.Cells(columnCount, 4).End(xlUp).PasteSpecial Transpose:=True