VBA – 下标超出范围

我有用于清单清单的这个脚本。 对于每个产品都有一个计数和重新sorting的限制。 如果数量less于重新订购限制,我会发送一封电子邮件询问订单。

我试着运行脚本,它给了我一个编译错误。 Subscript out of range - Run-time error9

这是我的代码。

  Sub sendEmail() ' ' sendEmail Macro ' ' Dim olApp As Object, olMail As Object Dim MyData Dim i As Long Set olApp = GetObject(, "Outlook.Application") ' ~~> I have taken 70 rows MyData = ThisWorkbook.Sheets("HDD").Range("D2:E70") For i = LBound(MyData) To UBound(MyData) - 1 If MyData(i, 5) > MyData(i, 4) Then Set olMail = olApp.CreateItem(0) Debug.Print MyData(i, 2) With olMail .To = "myemail@example.com" .Subject = "Sent from Excel" .Body = MyData(i, 2) .Send End With End If Next i End Sub 

我的工作表是这样的:

 No - Product - Description - Count - Reorder --------------------------------------------- 1 Pen - 4 5 

您的数组的源范围只有两列,但是您引用数组中的硬编码列值4和5。