简体   繁体   English

Excel宏 - 运行时错误'9':下标超出范围(在For循环中复制)

[英]Excel Macro- Run-time error '9': Subscript out of range (within For Loop to copy)

This is a simple macro code to copy range A2 until last row from one workbook and paste the same in another workbook. 这是一个简单的宏代码,用于将范围A2复制到一个工作簿的最后一行,并将其粘贴到另一个工作簿中。 I am a newbie and googled many pages but failed to help self. 我是一个新手,谷歌搜索了许多页面,但没有帮助自己。 The code is pasted below- 代码粘贴在下面 -

` `

Sub TC_Creation_Sample()
Dim aPath As String, aFile As String, bFile As String
Dim FinalRow As Integer, x As Integer

With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

aPath = "C:\temp\"
aFile = aPath & "Config"
bFile = aPath & "TC_Template"
Workbooks.Open (aFile)
Sheets("Config").Activate
'FinalRow = Cells(Rows.Count, "A").End(xlUp).Row
FinalRow = Worksheets("Config").Range("A2").End(xlDown)

For x = 2 To FinalRow
            Worksheets("Config").Range("A" & x).Select
            Selection.Copy
            Workbooks.Open (bFile)
            Worksheets("TestCases").Range("A" & x).Select
            ActiveSheet.Paste
Next x       
End Sub

` `

Result- Upon debugging, it's found that the code is successfully copy pasting A2 value. 结果 - 在调试时,发现代码成功复制粘贴A2值。 However, it's throwing run-time error 9: Subscript out of range. 但是,它会抛出运行时错误9:下标超出范围。 And the error occurs when Worksheets("Config").Range("A" & x).Select is running for the second time after Next x. 当工作表(“配置”)。范围(“A”和x).Select在Next x之后第二次运行时,会发生错误。 I am not sure what's really causing this error. 我不确定是什么导致了这个错误。 Kindly, advise. 好心提醒。

You haven't assigned row number to FinalRow variable. 您尚未为FinalRow变量分配行号。 Try: 尝试:

FinalRow = Worksheets("Config").Range("A2").End(xlDown).Row

Is this any better? 这更好吗? I assigned your workbooks to workbook variables and took out select and copy methods. 我将workbooks workbook variables分配给workbooks workbook variables并取出了selectcopy方法。 I also added .Row as others have also suggested 我也补充.Row 。其他人也提出过

Sub TC_Creation_Sample()
    Dim aPath As String, aFile As String, bFile As String
    Dim FinalRow As Long, x As Long
    Dim wbA As Workbook, wbB As Workbook

    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With

    aPath = "C:\temp\"
    aFile = aPath & "Config"
    bFile = aPath & "TC_Template"

    Set wbA = Workbook.Open(aFile)
    Set wbB = Workbooks.Open(bFile)

    FinalRow = wbA.Worksheets("Config").Range("A2").End(xlDown).Row
    'FinalRow = wbA.Worksheets("Config").Cells(Rows.Count, "A").End(xlUp).Row

    For x = 2 To FinalRow
        wbB.Worksheets("TestCases").Range("A" & x).Value = wbA.Worksheets("Config").Range("A" & x)
    Next x
End Sub

A number of points to mention. 有几点值得一提。

First - An Excel workbook can contain many millions of rows - but your code is being limited to only 32,768 rows, because you are defining both FinalRow and X as Integers. 首先 - Excel工作簿可以包含数百万行 - 但您的代码仅限于32,768行,因为您将FinalRowX定义为整数。 Use Long Integers instead (Dim X as Long, FinalRow as Long), in case one of the workbooks you open has more than 32,000 rows. 使用长整数(Dim X为Long,FinalRow为Long),以防您打开的工作簿中有一行超过32,000行。

Now onto the cause of the error. 现在到了错误的原因。 When you run the code for the second time through the For-Next Loop, the system hangs, because as far as it's concerned, the active workbook is bFile, and because Config tab doesn't exist on bFile (it's on aFile), it crashes with the unhelpful error message you get (because it can't find the right tab, because it's looking in the wrong place). 当您通过For-Next循环第二次运行代码时,系统会挂起,因为就其而言,活动工作簿是bFile,并且因为bFile上不存在Config选项卡(它位于aFile上),所以崩溃时会收到无用的错误消息(因为它无法找到正确的选项卡,因为它查找的位置错误)。

Incidentally, your code will attempt to open (bFile) EVERY time the For..Next loop triggers. 顺便提一下,每次For..Next循环触发时,您的代码都会尝试打开(bFile)。 Do you need to open the file every time? 你每次都需要打开文件吗? Surely it's just a case of copying the details from one sheet to the next? 当然,这只是将细节从一张纸复制到另一张纸的情况?

You should also have some error handling. 您还应该有一些错误处理。 If either aFile doesn't contain Config tab, or bFile doesn't contain TestCases tab, the code will crash out. 如果aFile不包含Config选项卡,或者bFile不包含TestCases选项卡,则代码将崩溃。 I would strongly recommend that you some error handling in to handles these instances. 我强烈建议您使用一些错误处理来处理这些实例。

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

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