简体   繁体   English

工作表类的激活方法失败-vlookup-vba

[英]activate method of worksheet class failed - vlookup - vba

I want to use VLOOKUP command and use a range which is in sheet B (not in the activated one A). 我想使用VLOOKUP命令,并使用工作表B中的范围(不在激活的A中)。 Calling the new worksheet gives me an error of " 'runtime error 1004' activate method of worksheet class failed" 调用新工作表会给我一个错误“工作表类的'运行时错误1004'激活方法失败”

Public Sub Creation()

    Worksheets("A").Activate

    Randomize
    Dim code As String
    Dim hamid As Variant
    Dim Lookup_Range As Range
    Code = 100032
    Set Lookup_Range = Worksheets("B").Range("O1:P8")
    On Error Resume Next
    hamid = Application.WorksheetFunction.VLookup(code, Lookup_Range, 2, False)
    On Error GoTo 0

End sub

I have tried using With command to call the new worksheet but I was not successful. 我曾尝试使用With命令来调用新工作表,但未成功。 I am new to VBA so please bear with me. 我是VBA的新手,请多多包涵。

Your lookup range seems to be in worksheet B. Try activating worksheet B before using the lookup function. 您的查找范围似乎在工作表B中。在使用查找功能之前,请尝试激活工作表B。 I've encountered issues trying to define ranges on one worksheet while having another activated: 我在尝试激活一个工作表上定义范围时遇到了问题:

Public Sub Creation()

Worksheets("A").Activate

Randomize
Dim code As String
Dim hamid As Variant
Dim Lookup_Range As Range
code = 100032
'Try here:
Worksheets("B").Activate
Set Lookup_Range = Worksheets("B").Range("O1:P8")
On Error Resume Next
'or here:
Worksheets("B").Activate
hamid = Application.WorksheetFunction.VLookup(code, Lookup_Range, 2, False)
On Error GoTo 0
'also made 'Code' in lookup function 'code'.
End Sub

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

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