Vba 将范围复制到新工作簿-未复制,错误9

Vba 将范围复制到新工作簿-未复制,错误9,vba,excel,Vba,Excel,我收到运行时错误“9”: 下标超出范围 错误发生在结尾处。。我正在尝试打开一个新的电子表格,将编辑的信息复制到其中,然后我将使用下面的脚本根据选择将8-12个文件转储到“FName”中。。。这可能有效,也可能无效 单击“调试”时会突出显示此选项: Workbooks("TFR7").Sheets("Sheet1").Range("A2:V" & LastRow).Copy Destination:=Workbooks(FName).Sheets("Sheet1").Range("A1")

我收到运行时错误“9”:

下标超出范围

错误发生在结尾处。。我正在尝试打开一个新的电子表格,将编辑的信息复制到其中,然后我将使用下面的脚本根据选择将8-12个文件转储到“FName”中。。。这可能有效,也可能无效

单击“调试”时会突出显示此选项:

Workbooks("TFR7").Sheets("Sheet1").Range("A2:V" & LastRow).Copy Destination:=Workbooks(FName).Sheets("Sheet1").Range("A1")
我不明白这里的错误是什么?是我选择的范围吗

旁注:我正在学习如何删除select等的实例。仅供参考

代码如下:

Sub OpenReportThenEdit()

'This will open a designated report and edit it
'File pathway and name must be correct
'Any adjustments to file layout could 'break' macro
'First file will always be TFR7 and from there can go into more


'Currently only works for TFR7

Application.ScreenUpdating = False

Dim wb As Excel.Workbook
Dim LastRow As Long
Dim FName As String

'Open a report, delete header/footer rows

Set wb = Workbooks.Open("C:\Users\USER\Downloads\TFR7", False, False)
wb.Sheets(1).Rows("1:5").EntireRow.Delete
wb.Sheets(1).Range("A" & Rows.Count).End(xlUp).EntireRow.Delete
wb.Sheets(1).Range("A" & Rows.Count).End(xlUp).EntireRow.Delete
wb.Sheets(1).Range("J" & Rows.Count).End(xlUp).EntireRow.Delete

'Edit Sheet Font/Size

With Worksheets("Sheet1").Cells.Font
    .Name = "Arial"
    .Size = 9
End With

'Edit Sheet Alignment, etc.

With Worksheets("Sheet1").Cells
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
    .HorizontalAlignment = xlRight
    .VerticalAlignment = xlBottom
    .WrapText = False
End With

'Replace 'text to columns' and convert dates to Excel Date Value before
'Paste Values' to remove formula

Columns("L:O").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("L2").FormulaR1C1 = "=DATEVALUE(LEFT(RC[4],10))"
Range("L2").Copy Destination:=Range("L2:O2")
LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
Range("L2:O" & LastRow).FillDown
Range("P1:S1").Copy Destination:=Range("L1:O1")

Columns("L:O").Select
Application.CutCopyMode = False
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Application.CutCopyMode = False
Selection.NumberFormat = "m/d/yyyy"

'Delete old date columns, remove duplicate values (by tracking numbers)

Columns("P:S").Select
Selection.Delete Shift:=xlToLeft
ActiveSheet.Range("A1:V" & LastRow).RemoveDuplicates Columns:=19, Header:= _
    xlYes

'Select cells with values, turn them blue (because silly people want them blue)

LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
ActiveSheet.Range("A2:V" & LastRow).Select

With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorAccent1
    .TintAndShade = 0.399975585192419
    .PatternTintAndShade = 0
End With

'Open Workbook, set Workbook as Destination for

FName = "C:\Users\USER\Downloads\Daily_" & _
        Format(Date, "mmdd") & ".xlsm"

Workbooks.Add.SaveAs Filename:=FName, _
                    FileFormat:=xlOpenXMLWorkbookMacroEnabled

Workbooks("TFR7").Sheets("Sheet1").Range("A2:V" & LastRow).Copy Destination:= _
        Workbooks(FName).Sheets("Sheet1").Range("A1")

Application.ScreenUpdating = True

End Sub

改为使用对象:

Dim otherWB As Excel.Workbook

'// other code here

Set otherWB = Workbooks.Add
otherWB.SaveAs Filename:=FName, FileFormat:=xlOpenXMLWorkbookMacroEnabled

'// wb is already set to original workbook, otherWB is set to new workook
wb.Sheets("Sheet1").Range("A2:V" & LastRow).Copy Destination:=otherWB.Sheets("Sheet1").Range("A1")

等等,我是否需要将FName设置为某种形式的变量?7天之后,我开始自学,如果有人能给我指出正确的方向,我将非常感激。错误的原因是
工作簿(“C:\Users\USER\Downloads\Daily\u 0506.xlsm”)
不起作用,只需要
工作簿(“Daily\u 0506.xlsm”)
(即没有路径)。宏观人的回答应该能解决这个问题。