Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba Find返回错误方法';范围';对象的'_全球';失败_Vba_Excel_Runtime Error_Excel 2010 - Fatal编程技术网

Vba Find返回错误方法';范围';对象的'_全球';失败

Vba Find返回错误方法';范围';对象的'_全球';失败,vba,excel,runtime-error,excel-2010,Vba,Excel,Runtime Error,Excel 2010,我正在编写一个sub,它允许用户输入ID号(QuickRef)。我正在使用range.find,但找不到克服错误“对象\u Global”的方法“range”失败的方法 数据集是动态的,因此我构建了一种方法来确定要搜索的正确范围,如下所示: Dim qRef As String Dim qCol As Byte Dim lRow As Long Dim subject As Range Dim sRow As Long Dim found As Range 'Finds the QuickRe

我正在编写一个sub,它允许用户输入ID号(QuickRef)。我正在使用range.find,但找不到克服错误“对象\u Global”的方法“range”失败的方法

数据集是动态的,因此我构建了一种方法来确定要搜索的正确范围,如下所示:

Dim qRef As String
Dim qCol As Byte
Dim lRow As Long
Dim subject As Range
Dim sRow As Long
Dim found As Range

'Finds the QuickRef column in the dataset
qCol = Cells.Find(What:="QuickRef", _
            After:=Range("DataDump"), _
            LookAt:=xlWhole, _
            LookIn:=xlValues, _
            SearchOrder:=xlByColumns, _
            SearchDirection:=xlNext).Column

'Finds the last row of the dataset
lRow = Cells.Find(What:="*", _
            After:=Range("A1"), _
            LookAt:=xlPart, _
            LookIn:=xlFormulas, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious, _
            MatchCase:=False).Row

'Sets the range in which you will search for the QuickRef
Set qRef = Range(Range("DataDump").Offset(1, qCol - 2), Cells(lRow, qCol))

qRef.Select 'confirms the correct range is being referred to.

'error occurs with following line
sRow = Range(qRef).Find(What:=InputBox("Enter main subject QuickRef", "Subject Selection"), _
            After:=Range("A1"), _
            LookAt:=xlWhole, _
            LookIn:=xlValues, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False).Row

debug.print sRow

为什么我会犯这个错误?我如何克服它?

这更多是通过一些其他有用的观察来实现的

1) 显式包含正在使用的工作表引用,否则将隐式使用Activesheet

2) 您正在查找的内容可能找不到,因此您将无法访问.Row,因为返回的范围对象将为Nothing。应该对此进行测试

Public Sub GetTotal()
    Dim sRow As Long
    Dim qRef As String
    Dim found As Range

    qRef = "A:A"
    Set found = _
        ThisWorkbook.Worksheets("Sheet1").Range(qRef) & _
        .Find( _
            What:=InputBox("Enter main subject QuickRef", "Subject Selection"), _
            After:=Range("A1"), _
            LookAt:=xlWhole, _
            LookIn:=xlValues, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False)


    If Not found Is Nothing Then
        sRow = found.Row
    End If
    Debug.Print sRow
End Sub

这更多的是通过一些其他有用的观察

1) 显式包含正在使用的工作表引用,否则将隐式使用Activesheet

2) 您正在查找的内容可能找不到,因此您将无法访问.Row,因为返回的范围对象将为Nothing。应该对此进行测试

Public Sub GetTotal()
    Dim sRow As Long
    Dim qRef As String
    Dim found As Range

    qRef = "A:A"
    Set found = _
        ThisWorkbook.Worksheets("Sheet1").Range(qRef) & _
        .Find( _
            What:=InputBox("Enter main subject QuickRef", "Subject Selection"), _
            After:=Range("A1"), _
            LookAt:=xlWhole, _
            LookIn:=xlValues, _
            SearchOrder:=xlByRows, _
            SearchDirection:=xlNext, _
            MatchCase:=False)


    If Not found Is Nothing Then
        sRow = found.Row
    End If
    Debug.Print sRow
End Sub

什么是qRef?qRef是我之前定义的一个范围,它找到了某个列标题及其最后一行。在本例中,它返回R12:R1600。请问qRef是什么?qRef是我之前定义的一个范围,它找到某个列标题及其最后一行。在本例中,它返回R12:R1600。