我的 VBScript 代码出现错误“下标超出范围" [英] I am getting an error in my VBScript code "Subscript out of range"

查看:19
本文介绍了我的 VBScript 代码出现错误“下标超出范围"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能告诉我为什么我收到 Subscript out of range-ArrParent 的错误吗?当我使用 MsgBox(Lbound(ArrParent)) - 它给我 1 而当我使用 MsgBox(Ubound(ArrParent)) - 它给我 960.所以在下面的行中,我收到了这样的 out of range 错误.Dic(ArrParent(Count))=Count+2 在下面找到完整的代码:

Can you tell me why I am getting the error as Subscript out of range- ArrParent? When I used MsgBox(Lbound(ArrParent)) - it is giving me as 1 and when I used MsgBox(Ubound(ArrParent)) - it is giving me as 960. So in the line below I am getting such out of range error. Dic(ArrParent(Count))=Count+2 Find the full code below:

代码

    Sub ParentPIDNumber(ArrParent,ob3,ob2,ob4)

        Dim Dic,DicItems,Dickeys
        Dim Count

        Set Dic = CreateObject("Scripting.Dictionary")

        Count=LBound(ArrParent)
        'MsgBox(ArrParent(Count))
        Do Until Count > UBound(ArrParent) - 1

           Dic(ArrParent(Count))=Count+2 'here Dictionary keys are holding the row numbers as their Items

        Count=Count+1
        Loop

        ParentChildBinding Dic,ob3,ob2,ob4

    End Sub
   Sub FileredOpenProcessToDel(ob3,ob2,ob4)

        Dim ColumnToFilter,TotalRows
        Dim rngFilter,cel,str,rangesToRemove,x 
        Dim strToRemove : strToRemove = ""
        Dim ArrParent

        objExcel1.ScreenUpdating = False
        objExcel1.Calculation = -4135  'xlCalculationManual
        ColumnToFilter=objExcel1.Application.WorksheetFunction.CountA(ob4.Rows(1)) - 1
        ob4.Range(ob4.Cells(1,ColumnToFilter),ob4.Cells(1,ColumnToFilter)).AutoFilter ColumnToFilter, "Open",,,True 

        'Dim rngFilter as Range
        Set rngFilter = objExcel1.Application.Intersect(ob4.UsedRange,ob4.UsedRange.Offset(1),ob4.Columns(1)).SpecialCells(12)'xlCellTypeVisible
           'MsgBox(rngFilter.Rows.Count)
           REM Do While 1=1
            REM 'Msgbox
           REM Loop
        'msgbox "Filtered range has " & rngFilter.Rows.Count & " rows."
            str=""
            For each cel in rngFilter

              str = str & (cel.row) & ":" & (cel.row) & "," 

            Next

                    rangesToRemove = Split(str,",")

                    For x = UBOUND(rangesToRemove)-1 To LBOUND(rangesToRemove) Step -1

                         strToRemove = strToRemove & rangesToRemove(x)

                            If Len(strToRemove) > 200 then

                                ob4.Range(strToRemove).delete'str & rangesToRemove(x) & ":" & rangesToRemove(x) & ","
                                strToRemove = ""

                            Else

                                strToRemove = strToRemove & ","

                            End If

                    Next
                    If len(strToRemove) > 0 then

                        strToRemove = Mid(strToRemove, 1, Len(strToRemove) - 1)
                        'strToRemove = Left(strToRemove, Len(strToRemove) -1)
                        ob4.Range(strToRemove).delete

                    End If

        ob4.AutoFilterMode = False
        objExcel1.ScreenUpdating = True
        objExcel1.Calculation = -4105   'xlCalculationAutomatic

        TotalRows=objExcel1.Application.WorksheetFunction.CountA(ob4.Columns(1))
        'MsgBox(TotalRows)
        ReDim ArrParent(TotalRows - 2)
        ArrParent=ob4.Range("A2:" & "A" & TotalRows).Value
        'Call to the subroutine
        ParentPIDNumber ArrParent,ob3,ob2,ob4

    End Sub

请在这里帮助我!

推荐答案

在这种情况下,您可以做的最好的事情是在单独的脚本中提取错误代码并在那里进行试验,直到找到错误为止.

The best thing you can do in such a situation is extract the error giving code in a separate script and experiment there until you find the error.

在你的情况下,我找不到错误,下面的脚本没有给出错误,但它跳过了数组的最后一个元素

In your case i can't find an error, the following script gives no error but it skips the last element of your array

ArrParent = Array(10, 20, 30)
Count=LBound(ArrParent)
Set Dic = CreateObject("Scripting.Dictionary")
Do Until Count > UBound(ArrParent) - 1
  Dic(ArrParent(Count))=Count+2
  Count=Count+1
Loop
for each key in Dic 
  wscript.echo key & ":" & Dic(key)
next

'10:2
'20:3

我没有你的源数组来试验,但你可以像这样尝试,为了没有索引超出范围错误,你可以使用for each",如果你不需要计数变量它甚至是迭代数组的最佳方式.

I don't have your sourcearray to experiment with but you could try it like this, in order to not have an index out of range error you can use "for each", if you don't need the count variable it would even be the best way to iterate the array.

Count = 1
Set Dic = CreateObject("Scripting.Dictionary")
for each element in ArrParent
  Dic(element)=Count+2
  Count = Count+1
next

for each key in Dic 
  wscript.echo key & ":" & Dic(key)
next
'10:3
'20:4
'30:5

这篇关于我的 VBScript 代码出现错误“下标超出范围"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆