简体   繁体   English

从变量引用另一个工作簿中的工作表时,下标超出范围

[英]Subscript out of range when referencing a worksheet in another workbook from a variable

See picture: http://s12.postimg.org/ov8djtuh9/Capture.jpg 查看图片: http : //s12.postimg.org/ov8djtuh9/Capture.jpg

Context: Trying to activate a sheet (variable: cSheet) in another workbook and paste data there from copied data from a different workbook. 上下文:尝试激活另一个工作簿中的工作表(变量:cSheet),然后从另一个工作簿中复制的数据粘贴到该工作表中。 I'm getting a subscript out of range error whenever I try to activate directly using the variable (ie Worksheets(Name).Activate) or try to define a worksheet using the variable and then activate it. 每当我尝试使用变量直接激活(即Worksheets(Name).Activate)或尝试使用变量定义工作表然后激活它时,都会收到下标超出范围的错误。 I've also tried other coding styles, using "With Worksheet" etc. and my code was a lot longer but I started over because every time I fix something, something else goes wrong. 我还尝试了其他编码风格,例如使用“ With Worksheet”等,我的代码虽然更长了,但我还是重新开始了,因为每次我修复某些内容时,都会出问题。 So, sticking to the basics. 因此,坚持基本原则。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Sub GenSumRep()

Dim AutoSR As Workbook
Dim asrSheet As Worksheet
Dim tempWB As Workbook
Dim dataWB As Workbook
Dim SecName As String
Dim oldcell As String
Dim nsName As String
Dim cSheet As Worksheet

Set AutoSR = ActiveWorkbook
Set asrSheet = AutoSR.ActiveSheet

For a = 3 To 10

    SecName = asrSheet.Range("D" & a).Value

    If SecName <> "" Then

    Workbooks.Open Range("B" & a).Value
    Set tempWB = ActiveWorkbook
    'tempWB.Windows(1).Visible = False

    AutoSR.Activate

    Workbooks.Open Range("C" & a).Value
    Set dataWB = ActiveWorkbook
    'dataWB.Windows(1).Visible = False

    AutoSR.Activate

        'Copy paste data
        For b = 24 To 29
        oldcell = Range("C" & b).Value
            If b = 24 Then
            nsName = Trim(SecName) & " Data"
            Set cSheet = tempWB.Sheets(nsName)
            Else
            nsName = asrSheet.Range("B" & b).Value
            Set cSheet = tempWB.Sheets(nsName)
            End If

        'Copy
        dataWB.Activate
        Range(oldcell).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy

        'Paste
        tempWB.Activate
        cSheet.Select
        Range("A1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        Application.CutCopyMode = False

        b = b + 1
        Next b

    End If

a = a + 1

Next a

End Sub

You only get that error for one reason: the name your provided does not exist in the collection! 出现此错误的原因仅一个: 您提供的名称在集合中不存在!

There are a couple of likely reasons for this based on your code: 根据您的代码,可能有两个原因:

  • Your nsName variable contains hidden characters that make it different even though it appears correct. 您的nsName变量包含隐藏的字符,即使它看起来正确,也会使其与众不同。
  • You are looking for the sheet in the wrong workbook. 您在错误的工作簿中查找工作表。

Based on your comments, it seems that you are looking in the wrong workbook . 根据您的评论, 看来您在寻找错误的工作簿 A good way to check out these subscript errors is to iterate the collection and print out the Names included therein. 检查这些下标错误的一个好方法是迭代集合并打印出其中包含的Names

Dim sht as Worksheet    
For Each sht In tempWB.Sheets
    Debug.Print sht.Name
Next sht

In general, it is desirable to get rid of calls to Select and Activate so that you are not relying on the interface in order to get objects. 通常,希望摆脱对“ Select和“ Activate的调用,以便您不依赖接口来获取对象。 See this post about avoiding Select and Activate for more info. 有关更多信息,请参见有关避免SelectActivate的文章。

One idea applied to your code is to assign the Workbooks directly without ActiveWorkbook : 适用于您的代码的一种想法是在不使用ActiveWorkbook情况下直接分配工作簿:

Set tempWB = Workbooks.Open(asrSheet.Range("B" & a).Value)
Set dataWB = Workbooks.Open(asrSheet.Range("C" & a).Value)

instead of: 代替:

    Workbooks.Open Range("B" & a).Value
    Set tempWB = ActiveWorkbook
    'tempWB.Windows(1).Visible = False

    AutoSR.Activate

    Workbooks.Open Range("C" & a).Value
    Set dataWB = ActiveWorkbook
    'dataWB.Windows(1).Visible = False

暂无
暂无

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

相关问题 为什么在引用 VBA 中的另一个工作簿/工作表时出现下标超出范围错误 9? - Why am I getting a Subscript Out of Range Error 9 when referencing another Workbook/Worksheet in VBA? VBA错误:运行时错误:9-从另一个工作簿复制工作表时,下标超出范围 - VBA Error: Runtime Error: 9 - Subscript out of range when copying a worksheet from another workbook 将数据从工作簿复制到另一工作簿时,下标超出范围错误 - Subscript out of range error when copying data from a workbook to another 将范围复制到另一个工作簿时下标超出范围 - Subscript out of range when copying range to another workbook 访问另一个打开的工作簿中的工作表时下标超出范围 - Subscript Out Of Range when accessing Sheet in another opened Workbook 引用另一张纸时出现“下标超出范围”错误 - “Subscript out of range” error when referencing another sheet 如何从另一个工作簿引用源数据 - 下标超出范围 - How to reference source data from another workbook - subscript out of range 从一个工作簿粘贴到另一个工作簿时,Excel VBA:错误9(下标超出范围) - Excel VBA: Error 9 (Subscript out of Range) when pasting from one workbook to another 在另一个 Excel 实例中引用工作簿会产生“运行时错误‘9’:下标超出范围” - Referencing a workbook, in another Excel instance, produces 'Run-time error '9': Subscript out of range' 将工作簿设置为全局变量 - 下标超出范围 - Setting workbook as a global variable - Subscript out of range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM