简体   繁体   English

在带有命名范围的Excel中搜索时,object_global的运行时错误1004方法范围失败

[英]Run time error 1004 method range of object_global failed on a search in Excel with a named range

I am getting error: 我收到错误:

Run time error 1004 method range of object_global failed object_global的运行时错误1004方法范围失败

in the following code line: 在以下代码行中:

Set br = Range("rg").Find(Range("$d$3").Value)

Following is the complete code: 以下是完整的代码:

Private Sub ComboBox1_Change()
Dim y As String
Dim q As Variant
Dim j As Double
Dim i As Variant
Dim k As Variant
Dim l As Double
Dim qst As Variant
Dim br As Range
Dim bra As Variant

qst = MsgBox("Entire data will be removed & archived" & vbNewLine & "Are you sure you want to proceed?", vbYesNo, fnb)
If qst = vbYes Then
Worksheets("Marcopolo").Unprotect myp
Worksheets("Marcopolo").Activate

Dim arch_iv(1 To 5, 1 To 2) As Variant

Dim rg As Variant
Dim fd As Variant

For rg = 1 To 5
    For fd = 1 To 2
        arch_iv(rg, fd) = Worksheets("arrays").Cells(rg, fd).Value
    Next fd
Next rg


For rg = 1 To 5
    For fd = 1 To 2
        Set br = Range("rg").Find(Range("$d$3").Value)
            br.Select
                br.Offset(0, 2).Select
                  bra = ActiveCell.Address
                MsgBox bra
            Range("fd").Copy
        Worksheets("marcopolo").Range(bra).PasteSpecial Paste:=xlPasteValues
    Next fd
Next rg

Else
GoTo Gtout
End If

The error comes, because the named range is not found. 错误来了,因为找不到命名范围。 If the named range rg should be on the "Marcopolo" worksheet, then it should be referred like this: 如果命名范围rg应该在“Marcopolo”工作表上,那么它应该像这样引用:

With Worksheets("Marcopolo")
    Set br = .Range("rg").Find(Range("D3"))
    If br Is Nothing Then
        MsgBox "D3 NOT FOUND!"
        Exit Sub
    End If
end with

The above has another problem, because the parent of the Range("D3") is not explicitly defined. 以上还有另一个问题,因为未明确定义Range("D3")的父级。 Thus, it is either the ActiveSheet or the Worksheet, in which the code is residing. 因此,它是ActiveSheet或Worksheet,代码驻留在其中。 To avoid this problem, consider defining it like this Worksheets("SomeName").Range("D3") . 要避免此问题,请考虑将其定义为此Worksheets("SomeName").Range("D3")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM