运行此代码时,下标在VBA中超出范围

时间:2020-12-13 16:43:03

When i initially run the code below in debug mode, there is no problems. However when i stop and rerun it again, it says Subscript out of range. Why is that so? Not sure why such error is popping out.

当我最初在调试模式下运行以下代码时,没有问题。然而,当我停止并再次重新运行时,它表示下标超出范围。为什么?不知道为什么会出现这样的错误。

Dim date1 As Double
strDate1 = Sheets("Part2").Cells(i, 1).Value
matchEndRow = Application.Match(CDbl(strDate1), Sheets("1.A").Range("A:A"), 1)
MsgBox matchEndRow
matchEndRow = matchEndRow - 1
MsgBox matchEndRow

Need some guidance.

需要一些指导。

1 个解决方案

#1


1  

As follow up from comments, there was another workbook opened when code runs.

作为评论的后续,代码运行时打开了另一个工作簿。

So changing

如此变化

Sheets("Part2") and Sheets("1.A")

表格(“Part2”)和表格(“1.A”)

to

ThisWorkbook.Sheets("Part2") and ThisWorkbook.Sheets("1.A") solves the problem.

ThisWorkbook.Sheets(“Part2”)和ThisWorkbook.Sheets(“1.A”)解决了这个问题。

#1


1  

As follow up from comments, there was another workbook opened when code runs.

作为评论的后续,代码运行时打开了另一个工作簿。

So changing

如此变化

Sheets("Part2") and Sheets("1.A")

表格(“Part2”)和表格(“1.A”)

to

ThisWorkbook.Sheets("Part2") and ThisWorkbook.Sheets("1.A") solves the problem.

ThisWorkbook.Sheets(“Part2”)和ThisWorkbook.Sheets(“1.A”)解决了这个问题。