数组字符串无法通过'工作表'

时间:2018-06-13 14:39:08

标签: arrays excel vba multidimensional-array

我试图设置一个基本上查看工作表数组的代码,并对每个工作表执行完全相同的检查。不幸的是,当我尝试访问With ...函数时,我似乎得到以下消息(通过它我希望数组运行):

  

运行时错误' 9':订阅超出范围

虽然变量

  

WshtNames(w)的

最终会取得所需的名称,它似乎无法存储它以便它可以应用于相应的工作表?任何帮助非常感谢。代码如下:

    Option Explicit

Public Cell, WshtNames As Variant, Check_Tab, Bench_OAS_Level, Bench_OAS_Change, Bench_Spread_Dur, Bench_Duration, Bench_Convexity, ws As Worksheets, FirstHeaderColumn, LRow, LastRow As Long

Sub Definitions()

Set Bench_OAS_Level = Workbooks("TEST Overview Tool Barclays May 2018").Worksheets("Bench OAS Level")
Set Bench_OAS_Change = Workbooks("TEST Overview Tool Barclays May 2018").Worksheets("Bench OAS Change")
Set Bench_Spread_Dur = Workbooks("TEST Overview Tool Barclays May 2018").Worksheets("Bench Spread Dur")
Set Bench_Duration = Workbooks("TEST Overview Tool Barclays May 2018").Worksheets("Bench Duration")
Set Bench_Convexity = Workbooks("TEST Overview Tool Barclays May 2018").Worksheets("Bench Convexity")
Set Check_Tab = Workbooks("TEST Overview Tool Barclays May 2018").Worksheets("Check")

End Sub

Sub Check_OOT()

Call Definitions

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlManual

Dim CalcRange, FirstHeaderRow, LastColumn As Range
Dim w As Long

WshtNames = Array("Bench_OAS_Level", "Bench_OAS_Change", "Bench_Spread_Dur", "Bench_Duration", "Bench_Convexity")

For w = LBound(WshtNames) To UBound(WshtNames)

  With Worksheets(WshtNames(w))

    Set LastColumn = .UsedRange.Find("Total", , xlValues, xlWhole)
    Set FirstHeaderRow = .UsedRange.Find("Level 1", , xlValues, xlWhole)
    FirstHeaderColumn = FirstHeaderRow.Column + 2
    LastRow = .Cells(Rows.Count, LastColumn.Column).End(xlUp).Row

    Set CalcRange = .Range(.Cells(FirstHeaderRow.Row + 2, FirstHeaderRow.Column + 3), .Cells(LastRow, LastColumn.Column))

       For Each Cell In CalcRange
          If Not IsError(Cell) Then
           If Cell > 500 Or Cell < -100 Then
           Cell.Font.Bold = True
           Cell.Interior.ColorIndex = 36
           Cell.EntireRow.Copy
              Call Copy_into_Check_Tab
           End If
        End If
       Next Cell

  End With

Next w

End Sub

3 个答案:

答案 0 :(得分:2)

似乎工作表的名称是:

  • 替补OAS等级
  • 替补OAS改变
  • Bench Spread Dur
  • 工作时间
  • Bench Convexity

但是你将它们存储在数组中:

  • Bench_OAS_Level
  • Bench_OAS_Change
  • Bench_Spread_Dur
  • Bench_Duration
  • Bench_Convexity

(带下划线)。

这可以解决问题吗?

此外,将字典对象添加到公共范围可能会有所帮助:

public worksheetDictionary as object

将定义代码更改为:

Sub Definitions()
    set worksheetDictionary = CreateObject("Scripting.Dictionary")

    With Workbooks("TEST Overview Tool Barclays May 2018")
        Call worksheetDictionary.Add("Bench_OAS_Level", .Worksheets("Bench OAS Level"))
        Call worksheetDictionary.Add("Bench_OAS_Change",.Worksheets("Bench OAS Change"))
        Call worksheetDictionary.Add("Bench_Spread_Dur", .Worksheets("Bench Spread Dur"))
        Call worksheetDictionary.Add("Bench_Duration", .Worksheets("Bench Duration"))
        Call worksheetDictionary.Add("Bench_Convexity", .Worksheets("Bench Convexity"))

        Set Check_Tab = Workbooks("TEST Overview Tool Barclays May 2018").Worksheets("Check")
    End With
End Sub

然后,您将主块中的for循环更改为:

For each k in worksheetDictionary.Keys
    With worksheetDictionary(k)

希望有所帮助!

答案 1 :(得分:1)

尝试更改......

Dim CalcRange, FirstHeaderRow, LastColumn As Range
Dim w As Long

WshtNames = Array("Bench_OAS_Level", "Bench_OAS_Change", "Bench_Spread_Dur", "Bench_Duration", "Bench_Convexity")

 For w = LBound(WshtNames) To UBound(WshtNames)

     With Worksheets(WshtNames(w))

要...

Dim CalcRange, FirstHeaderRow, LastColumn As Range
Dim w As variant

WshtNames = Array("Bench_OAS_Level", "Bench_OAS_Change", "Bench_Spread_Dur", "Bench_Duration", "Bench_Convexity")

 Foreach w in wshtNames

     With Worksheets(w)

答案 2 :(得分:1)

使用UBound和LBound时,必须使用第二个参数告诉编译器您希望循环的数组的哪个维度。

 example:  For w = LBound(WshtNames,1) To UBound(WshtNames,1)

 example: For w = LBound(WshtNames,2) To UBound(WshtNames,2)

你超出范围b.c编译器不知道你想要告诉它去的地方。

我为了好玩而运行它并按预期工作

enter image description here

以下是解决设置工作簿值的正确方法

WshtNames = Array("Bench_OAS_Level", "Bench_OAS_Change", "Bench_Spread_Dur", "Bench_Duration", "Bench_Convexity")

set wb = Workbooks(“workbookname.filextension”)

For w = LBound(WshtNames) To UBound(WshtNames)

  With wb.Worksheets(WshtNames(w))