对象variables或块variables未设置91 VBA

我正在研究一个应该将数据传输到另一个名为“pistoia”的工作表,这是代码:

Sub SetCounter(ByVal counter As Double, ByVal product As String) Dim ws As Worksheet On Error Resume Next Sheets("pistoia").Activate Set ws = ActiveWorkbook.Sheets("pistoia") On Error GoTo 0 If ws Is Nothing Then MsgBox "pistoia sheet not found" Else If ws.Name = ActiveWorkbook.ActiveSheet.Name Then Dim index_destRow As Integer, index_destColumn As Integer, search_product As Range Rows(2).Find(What:="Nome commerciale", LookAt:=xlWhole, LookIn:=xlValues).Offset(2).Select Else MsgBox "pistoia sheet found but is inactive" End If End If End Sub 

(2)。find(What:=“Nome commerciale”,LookAt:= xlWhole,LookIn:= xlValues).Offset(2)。select“,我认为错误是由于激活新的工作表,因为在之前的macros中,“在起始页上”我在导致错误的行中执行相同的操作。 有什么build议么?

这表明,价值没有被发现,所以它正试图select一些不存在的东西。 例如设置一个范围variables来检查find的值。 使用查找,也值得指定一些其他的参数,以防他们不是你所期望的。

  Sub SetCounter(ByVal counter As Double, ByVal product As String) Dim ws As Worksheet, index_destRow As Integer, index_destColumn As Integer, search_product As Range Dim rFind As Range On Error Resume Next Sheets("pistoia").Activate Set ws = ActiveWorkbook.Sheets("pistoia") On Error GoTo 0 If ws Is Nothing Then MsgBox "pistoia sheet not found" Else If ws.Name = ActiveWorkbook.ActiveSheet.Name Then Set rFind = ws.Rows(2).Find(What:="Nome commerciale", LookAt:=xlWhole, LookIn:=xlValues, MatchCase:=False, SearchFormat:=False) If Not rFind Is Nothing Then rFind.Offset(2).Select Else msgbox "Value not found" End If Else MsgBox "pistoia sheet found but is inactive" End If End If End Sub