2016-10-19 161 views
0

我正在应的数据传送到名为“皮斯托亚”不同的片的宏并且是这样的码:对象变量或与块变量未设置91 VBA

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)。找到(什么:=”Nome commerciale“,LookAt:= xlWhole,LookIn:= xlValues).Offset(2)。选择”,我认为错误是由于激活新的工作表,因为在前面的宏中,“在起始页上”我在导致错误的行中执行相同的操作。有什么建议么?

+2

在'行(2)'前加'ws.'。将对象分配给对象可确保该调用作用于所需的指定对象。另一件事是确保“Nome commerciale”存在于你正在寻找的地方。 –

+0

我试过用ws。但是没有任何变化......并且是的,我已经检查了第2行 –

+0

如果你在命令前添加'debug.print'并将'.Select'改为'.Value'。错误是否持续?或者它是否在即时窗口中返回一个值? –

回答

2

这表明我的价值没有被发现,因此它试图选择一些不存在的东西。例如。设置范围变量来检查找到的值。通过查找,还可以指定一些其他参数,以防它们不符合您的预期。

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 
+0

请问您可以扩展一下 - 也许是一个如何纠正它的例子(可能是Select语句并检查参考_is不是什么_)... –

+0

是的,请参阅上面的内容。 – SJR

+0

我使用布局策略现在我得到错误448命名参数未找到,但在工作表第2行和第1列“pistoia”只有文本“Nome commerciale”... –

相关问题