我收到运行时错误“ 438”:对象不支持此属性或方法

时间:2019-03-24 19:24:18

标签: excel vba

我必须在c列的sheet2中搜索一个值,并将其内容显示在用户表单上。

这是我的代码:

Option Explicit

Sub searchdata()
    Dim erow As Long
    Dim WS As Worksheet
    Dim lastrow As Long
    Dim count As Integer
    Dim x As Integer

    lastrow = Sheets("Resolution").Cells(Rows.count, 1).End(xlUp).Row
    count = 0

    For x = 2 To lastrow
        If Sheets("Resolution").Cells(x, 1) = Sheet2.Range("$C2:$c92") Then
            Sheet2.Range("F25") = Sheets("Resolution").Cell(x, 1)
            Sheet2.Range("G25") = Sheets("Resolution").Cell(x, 2)
            Sheet2.Range("H25") = Sheets("Resolution").Cell(x, 3)
            count = count + 1
        End If
    Next x

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

使用:

        Sheet2.Range("F25") = Sheets("Resolution").Cells(x, 1)
        Sheet2.Range("G25") = Sheets("Resolution").Cells(x, 2)
        Sheet2.Range("H25") = Sheets("Resolution").Cells(x, 3)

答案 1 :(得分:1)

您的代码存在一些问题。除了 Cell Cell s 问题之外,还可以直接比较一个单元格与一个范围内的多个单元格以及未使用的单元格偏移量计数。

一个 With ... End With 块可以减少代码和重复的工作表引用调用。

Option Explicit

Sub searchdata()

    Dim lastrow As Long, x As Long, count As Long

    WITH Sheets("Resolution")

        lastrow = .Cells(.Rows.count, 1).End(xlUp).Row
        count = 0

        For x = 2 To lastrow
            'the next line will continue if the value in Resolution
            'is found within Sheet2.Range("$C2:$c92")
            If NOT ISERROR(APPLICATION.MATCH(.Cells(x, 1), Sheet2.Range("$C2:$c92"), 0)) Then
                Sheet2.Range("F25").OFFSET(count, 0) = .CellS(x, 1)
                Sheet2.Range("G25").OFFSET(count, 0) = .CellS(x, 2)
                Sheet2.Range("H25").OFFSET(count, 0) = .CellS(x, 3)
                count = count + 1
            End If
        Next x

    END WITH

end sub