VBA:获取对象_Worksheet的方法范围失败

huangapple go评论74阅读模式
英文:

VBA: Getting error method range of object _worksheet failed

问题

以下是代码的中文翻译:

在执行以下代码时,我一直收到标题中提到的错误:

Sub test()
    Dim j As Integer
    j = 2
    If ActiveSheet.Name = "Email" Then
        Sheets("Lists").Range("Z2:Z31").Cells.Value = ""
        For i = 2 To 190
            If Sheets("Lists").Cells(i, 8).Value = Cells(2, 2).Value Then
                Sheets("Lists").Cells(j, 26).Value = Sheets("Lists").Cells(i, 6).Value
                j = j + 1
            End If
        Next
        Range("C12:O12").AutoFilter 1, ">=" & Range("B1").Value, xlAnd, "<=" & Range("C1").Value
    End If
End Sub

此代码在任何更改发生在工作表中时都会自动执行,通过添加此类事件:

Public WithEvents appevent As Application
Private Sub appevent_SheetChange(ByVal sh As Object, ByVal target As Range)
    Call test
End Sub

并且在VBA的ThisWorkbook选项卡中使用Workbook_Open方法自动添加事件,当工作表打开时执行如下操作:

Dim myobject As New Class1
Private Sub Workbook_Open()
    Set myobject.appevent = Application
End Sub

尽管通过调试器正常执行(只需按F5),但我试图做的是在用户从数据验证的单元格中选择选项后,工作表会自动更新在该工作表中查看的内容,并且还会将“Lists”工作表中的一些其他单元格设置为与原始值不同的值。

到目前为止,通过按F5键通过调试器运行代码时,代码可以无缝执行,所以我不知道为什么会出现这样的错误。我猜测VBA不允许以编程方式更改单元格值,除非选定了工作表,那么如果工作表没有选定,我该如何做呢?

如果您可以,请友善地提供帮助。谢谢。
英文:

I keep getting error as mentioned in the title when executing the below code:

Sub test()
    Dim j As Integer
    j = 2
    If ActiveSheet.Name = &quot;Email&quot; Then
        Sheets(&quot;Lists&quot;).Range(&quot;Z2:Z31&quot;).Cells.Value = &quot;&quot;
        For i = 2 To 190
            If Sheets(&quot;Lists&quot;).Cells(i, 8).Value = Cells(2, 2).Value Then
                Sheets(&quot;Lists&quot;).Cells(j, 26).Value = Sheets(&quot;Lists&quot;).Cells(i, 6).Value
                j = j + 1
            End If
        Next
        Range(&quot;C12:O12&quot;).AutoFilter 1, &quot;&gt;=&quot; &amp; Range(&quot;B1&quot;).Value, xlAnd, &quot;&lt;=&quot; &amp; Range(&quot;C1&quot;).Value
    End If
End Sub

This code is automatically executed when any change happens in the worksheet by adding this class event.

Public WithEvents appevent As Application
Private Sub appevent_SheetChange(ByVal sh As Object, ByVal target As Range)
    Call test
End Sub

And using the Workbook_Open method in the ThisWorkbook tab in VBA to add the event automatically when the worksheet open as the below.

Dim myobject As New Class1
Private Sub Workbook_Open()
    Set myobject.appevent = Application
End Sub

Although when executed normally through debugger by just pressing F5 the code executes flawlessly.

What I'm trying to do is after the user selects an option from a data validated cell, the sheet auto updates the content viewed in that sheet and also sets some other cells in the "Lists" sheet to different values from the original ones.

So far, the code executes flawlessly when run through debugger by pressing F5, so I'm out of ideas why such an error occurs. My guess is that VBA doesn't allow changing cell values programmatically unless the sheet is selected, so how can I do that if the sheet isn't selected.

If you can please kindly help. Thanks.

答案1

得分: 1

你正在使用Range(...)和Cells(...)而没有指定工作表。最好的做法是不使用.Select来设置引用对象,而是明确设置对象。

Sub test()
    Dim j As Integer
    j = 2
    If ActiveSheet.Name = "Email" Then
        With Worksheets("Lists")
            .Range("Z2:Z31").Value = ""
            For i = 2 To 190
                '在下一行,Cells(2,2).Value来自于活动工作表...
                '这是你需要的吗,还是一个错误?请检查。
                If .Cells(i, 8).Value = Sheets("Email").Cells(2, 2).Value Then
                    .Cells(j, 26).Value = .Cells(i, 6).Value
                    j = j + 1
                End If
            Next
            '我在Ranges中加了一个点...这是你需要的吗,还是必须引用活动工作表?
            .Range("C12:O12").AutoFilter 1, ">=" & .Range("B1").Value, xlAnd, "<=" & .Range("C1").Value
        End With
    End If
End Sub

无论如何,所有的Range(...)和Cells(...)都需要一个父级对象,如果代码位于工作表中,父级对象就是该工作表,否则你需要明确定义它。你可以使用Sheet("...").Select来定义它。因此,Cells(2, 2).Value和Range("C12:O12").AutoFilter等都来自工作表"Lists"。

'此代码在Email工作表模块中。也将Sub test()移至此处。
'你不需要一个类模块...
Private Sub Worksheet_Change(ByVal Target As Range)
   Call test
End Sub

Private Sub test()
   '.......
   '.......
End Sub

希望这有助于你的代码理解和改进。

英文:

You are using Range(...) and Cells(...) without specifying the Sheet. It is not good practice to use .Select to set a reference object, but prefer to set the object explicitly.

Sub test()
    Dim j As Integer
    j = 2
    If ActiveSheet.Name = &quot;Email&quot; Then
        With Worksheets(&quot;Lists&quot;)
         .Range(&quot;Z2:Z31&quot;).value = &quot;&quot;
         For i = 2 To 190
 &#39;IN NEXT LINE the Cells(2,2).Value comes form ActiveSheet... 
 &#39;is this you need or is a mistake? check it.
             If .Cells(i, 8).value = Cells(2, 2).value Then
                 .Cells(j, 26).value = .Cells(i, 6).value
                 j = j + 1
             End If
         Next
  &#39;I PUT A DOT in Ranges... Is this you need Or have to refer the ActiveSheet?
         .Range(&quot;C12:O12&quot;).AutoFilter 1, &quot;&gt;=&quot; &amp; .Range(&quot;B1&quot;).value, xlAnd, &quot;&lt;=&quot; &amp; .Range(&quot;C1&quot;).value
        End With
    End If
End Sub

In any case all Range(...) and Cells(...) need a parent, and: if the code lives in a sheet, the parent is the sheet ELSE you have to define it somehow. You do it with Sheet("...").Select. So the Cells(2, 2).value and Range("C12:O12").AutoFilter ..Range("B1").value.. Range("C1").value... comes from sheet "Lists".

&#39;This code in Email sheet module. Also move the Sub test() here.
&#39;You don&#39;t need a Class module...
Private Sub Worksheet_Change(ByVal Target As Range)
   Call test
End Sub


Private Sub test()
   &#39;.......
   &#39;.......
End Sub

huangapple
  • 本文由 发表于 2023年5月8日 01:21:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定