简体   繁体   English

VBA错误:运行时错误:9-下标超出范围

[英]VBA Error: Runtime Error: 9 - Subscript out of range

Requirement : I need to copy 1 Column - Col G (need to determine the number of rows dynamically) from 1 Workbook to another. 要求 :我需要将1列-Col G(需要动态确定行数)从1个工作簿复制到另一个。

Problem : Getting VBA Error: 问题 :获取VBA错误:

Runtime Error: 9 - Subscript out of range Please help. 运行时错误:9-下标超出范围,请帮助。

Sub Set_Open_ExistingWorkbook()

Dim wkb As Workbook

Set wkb = Workbooks.Open("C:\Users\me364167\Documents\Practice_OB_Status_Detailed_Report_Mainframe.xls")
'Set wkb = Workbooks.Open("C:\Users\me364167\Documents\Practice level_Opportunity Pipeline_Mainframe.xls")

Dim LastRow As Long
Dim Sheet1Data As Long

With ActiveSheet
    LastRow = .Cells(.Rows.Count, "G").End(xlUp).Row
End With

Workbooks("Practice_OB_Status_Detailed_Report_Mainframe.xls").Worksheets("OB_Status_Detailed_Report").Range(Cells(1, "G"), Cells(LastRow, "G")).Copy
Workbooks("OB Macro.xlsx").Worksheets("OB_Status_Detailed_Report").Range(Cells(1, "A"), Cells(LastRow, "A")).PasteSpecial


' Workbooks("Practice_OB_Status_Detailed_Report_Mainframe.xls").Worksheets("OB_Status_Detailed_Report").Range(Cells(1, "G"), Cells(LastRow, "G")).Copy
' Workbooks("OB Macro.xlsx").Worksheets("OB_Status_Detailed_Report").Range(Cells(1, "A"), Cells(LastRow, "A")).PasteSpecial Paste:=xlValues

wkb.Close

End Sub

This perhaps? 这也许吗? Your ranges are not fully qualified and as Shai Rado says you are missing a PS paremeter. 您的范围不完全合格,正如Shai Rado所说,您缺少PS参数表。

Sub Set_Open_ExistingWorkbook()

Dim wkb As Workbook

Set wkb = Workbooks.Open("C:\Users\me364167\Documents\Practice_OB_Status_Detailed_Report_Mainframe.xls")
'Set wkb = Workbooks.Open("C:\Users\me364167\Documents\Practice level_Opportunity Pipeline_Mainframe.xls")

Dim LastRow As Long
Dim Sheet1Data As Long

With ActiveSheet
    LastRow = .Cells(.Rows.Count, "G").End(xlUp).Row
End With

With wkb.Worksheets("OB_Status_Detailed_Report")
    .Range(.Cells(1, "G"), .Cells(LastRow, "G")).Copy
End With

With Workbooks("OB Macro.xlsx").Worksheets("OB_Status_Detailed_Report")
    .Range(.Cells(1, "A"), .Cells(LastRow, "A")).PasteSpecial xlValues
End With

' Workbooks("Practice_OB_Status_Detailed_Report_Mainframe.xls").Worksheets("OB_Status_Detailed_Report").Range(Cells(1, "G"), Cells(LastRow, "G")).Copy
' Workbooks("OB Macro.xlsx").Worksheets("OB_Status_Detailed_Report").Range(Cells(1, "A"), Cells(LastRow, "A")).PasteSpecial Paste:=xlValues

wkb.Close

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM