简体   繁体   English

VBA Windows(Index).Activate导致运行时错误'9':下标超出范围

[英]VBA Windows(Index).Activate causes Run-time error '9': Subscript out of range

I have an almost-complete VBA macro which runs fine until it reaches this line: 我有一个几乎完整的VBA宏,可以正常运行,直到到达此行:

Windows(temp_name).Activate

When I run this I get the following error: 运行此命令时,出现以下错误:

Run-time error '9': 运行时错误“ 9”:

Subscript out of range 下标超出范围

temp_name is the name of the file I need to return to. temp_name是我需要返回的文件的名称。 Here are some lines around this (tell me if more lines are necessary): 以下是一些相关代码(如果需要更多代码,请告诉我):

Next Ind
Application.CutCopyMode = False
Workbooks.OpenText Filename:=CPath _
    , Origin:=437, startRow:=1, DataType:=xlDelimited, TextQualifier:= _
    xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False _
    , Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), _
    Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _
    Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1)), _
    TrailingMinusNumbers:=True
For Ind1 = 1 To iPlateNo
TLCorn = "B" + CStr(iStartrow + (Ind1 - 1) * iStep)
BRCorn = "M" + CStr(iStartrow + 7 + (Ind1 - 1) * iStep)
RangeNOut = TLCorn + ":" + BRCorn
RangeNIn = TLCorn
Windows(FileN).Activate
Range(RangeNOut).Select
Selection.Copy
Windows(temp_name).Activate
Sheets("Data").Select
Range(RangeNIn).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Next Ind1
'   Calculation of background and efflux for individual plates
For Ind1 = 1 To iPlateNo / 2
    iStep2 = (Ind1 - 1) * iStep * 2
    iBgrRow = iStartrow + 8 + iStep2
    BgrValP = Cells(iBgrRow, 2).Value
    For Ind2 = 0 To 7
        iEntryRow = iStartrow + Ind2 + iStep2
        iEntryRow2 = iStartrow + Ind2 + iStep2 + 11
        MediaVal = Cells(iEntryRow, 13).Value
        MonoVal = Cells(iEntryRow2, 13).Value
        Cells(iEntryRow, 15).Value = MEeff * 100 * Volcorr * (MediaVal - BgrValP) / (M40eff * MonoVal + Volcorr * MEeff * (MediaVal - BgrValP))
    Next Ind2
Next Ind1

Try 尝试

Workbooks(temp_name).Activate 

This will reference the specific Excel Workbook instance as I believe it is failing to find the workbook you are intending to reference. 这将引用特定的Excel工作簿实例,因为我认为它找不到您要引用的工作簿。 Assuming Sheets("Data") is a worksheet within whatever workbook name is held in "temp_name" at the time? 假设Sheets(“ Data”)是当时处于“ temp_name”中的工作簿名称中的工作表?

Thanks, 谢谢,

Jamie 杰米

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

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