引用另一个工作表时"Subscript out of range"错误



我无法克服以下代码引起的"下标超出范围"错误。当我第一次设置找到的变量时,它会抛出错误。sub 用于工作簿中的每个工作表,用于从已知列中搜索员工并标识该行,然后使用该行对变量求和。然后,它会用该总数填充活动工作表中的一个单元格。"col"在主子中指定。

nDate = Range("B3")
Dim startDate As Date
startDate = Range("B2")
Dim emp As String
emp = Range("B8")
Dim rw As String
n = 0
Do While True
    Range("B99") = nDate
    stringDate = Range("B99").Text
    Set found = Worksheets(stringDate).Range("D38:D144").Find(emp, LookIn:=xlValues, _
                        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
            If found = emp Then
                rw = found.row
                n = Worksheets(stringDate).Range(col + rw)
                tot = tot + n
            End If
    If nDate = startDate Then
            Exit Do
        End If
    nDate = nDate - 1
Loop
Range(col + "3") = tot

我的代码中有类似的 Sub,编译得很好。唯一的区别是,在上面的子中,我正在搜索一个范围。下面是另一个没有抛出错误的子。

n = 0
Dim startDate As Date
Dim endDate As Date
startDate = Range("B2")
nDate = Range("B3")
Do While True
    Range("B99") = nDate
    stringDate = Range("B99").Text
    n = Worksheets(stringDate).Range(col + rw)
    tot = tot + n
    If nDate = startDate Then
        Exit Do
    End If
    nDate = nDate - 1
Loop
Range(col + "3") = tot

我知道关于同一错误有类似的问题,但没有一个涉及引用外部工作表。关于如何调试的任何建议?

在子/函数的开头添加以下内容:

Dim targetWs As Excel.Worksheet

然后将循环更改为:

Do While True
    Range("B99") = nDate
    stringDate = Range("B99").Text
    Set targetWs = Nothing
    Set found = Nothing
    On Error Resume Next
    Set targetWs = Worksheets(stringDate)
    On Error GoTo 0
    If targetWs Is Nothing Then
        MsgBox "Worksheet not found: " & stringDate
    Else
        Set found = targetWs.Range("D38:D144").Find(emp, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
    End If
    If Not found Is Nothing Then
        If found = emp Then
            rw = found.row
            n = targetWs.Range(col + rw)
            tot = tot + n
        End If
    End If
    If nDate = startDate Then
        Exit Do
    End If
    nDate = nDate - 1
Loop

如果出现消息框,您将知道您点击的工作表名称不属于 Worksheets 集合。

谢谢大家的帮助。我终于能够调试潜艇了。下面是正确运行的代码。下标错误出现在 while 循环的第二次迭代中,当变量 stringDate 被重新格式化为日期而不是字符串时。我不确定为什么它发生在这个子中,而在其他具有相同逻辑的子中也没有。转换为字符串的目的是在按该格式的日期标记的工作表中移动(例如 2016 年 10 月 25 日)。无论哪种方式,我都通过手动格式化保存当前迭代日期的单元格来解决此问题。我还更改了执行 .find 后识别"找到"的方式。

nDate = Range("B3")
Dim startDate As Date
startDate = Range("B2")
Dim emp As String
emp = Range("B8")
Dim rw As String
n = 0
Do While True
    Range("B99") = nDate
    Range("B99").NumberFormat = "[$-409]mmmm d, yyyy;@"
    stringDate = Range("B99").Text
    Set found = Worksheets(stringDate).Range("D12:D144").Find(emp, LookIn:=xlValues, _
                        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext)
        If Not found Is Nothing Then
            rw = found.Row
            n = Worksheets(stringDate).Range(col + rw)
            Else
                n = 0
        End If
            tot = tot + n
    If nDate = startDate Then
        Exit Do
    End If
    nDate = nDate - 1
Loop
Range(col + "3") = tot

相关内容

最新更新