【问题标题】:object variable or with block variable not set 91 VBA对象变量或块变量未设置 91 VBA
【发布时间】:2016-10-19 14:58:59
【问题描述】:

我正在开发一个宏,它应该将数据传输到名为“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

错误出现在以下行:“Rows(2).Find(What:="Nome Commerciale", LookAt:=xlWhole, LookIn:=xlValues).Offset(2).Select”,我认为错误是由于激活了新工作表,因为在之前的“在起始工作表上”制作的宏中,我在导致错误的行中运行了相同的操作。有什么建议吗?

【问题讨论】:

  • Rows(2)前面添加ws.。将亲子关系分配给对象可确保调用作用于所需的指定对象。另一件事是确保“商业名称”存在于您要查找的位置。
  • 我用 ws 试过了。但没有任何改变......是的,我检查了第 2 行
  • 如果您在命令前添加debug.print 并将.Select 更改为.Value。错误是否仍然存在?还是在即时窗口中返回一个值?
  • 错误仍然存​​在,即时窗口上没有写入任何内容

标签: vba excel macros


【解决方案1】:

这表明我没有找到该值,因此它试图选择的东西不存在。例如。设置范围变量以检查是否找到值。对于 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

【讨论】:

  • 您能否对此进行扩展-也许是如何更正它的示例(可能是 Select 语句和检查引用不是什么)...
  • 是的,见上文。
  • 我使用了制定的策略,但现在我收到错误 448 未找到命名参数,但在工作表“pistoia”的第 2 行和第 1 列中,只有文本“Nome Commerciale”。 .
  • 解决了错误 448...Mac 版本不支持 Searchformat...现在我得到“找不到值”但在 A2 中有我搜索的标签...不知道为什么它没有找到匹配项。
  • 它肯定在正确的纸上吗?在查看整个单元格内容时,请检查没有前导或尾随空格。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-08
  • 2013-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多