简体   繁体   English

Excel宏中的Vlookup

[英]Vlookup in Excel Macro

I have created a form in excel which takes input from users and saves the same in Sheet2. 我已经在excel中创建了一个表单,该表单接受了用户的输入并将其保存在Sheet2中。 I have a combobox named Account from which users will select the name of account. 我有一个名为Account的组合框,用户可以从中选择帐号的名称。 However each account will have a unique Id associated with it. 但是,每个帐户都将具有唯一的ID。 I dont want to expose this Id to the end users but at the same time I want the Id corresponding to the Account selected populated in sheet2. 我不想将此ID公开给最终用户,但与此同时,我希望对应于在sheet2中填充的所选帐户的ID。

I am trying to do a Vlookup to search for Id of the selected Account. 我正在尝试执行Vlookup搜索所选帐户的ID。

I have setup the Account values in Sheet3. 我已经在Sheet3中设置了“帐户”值。

Example: 
Column A  ColumnB
Account1  Id12345
Account2  Id23456
Account3  Id34567

My code in excel macro looks something like below. 我在excel宏中的代码如下所示。

Private Sub Save_Click()

Dim RowCount As Long
Dim myValue as String

RowCount = Worksheets("Sheet2").Range("A1").CurrentRegion.Rows.Count
With Worksheets("Sheet2").Range("A1")
 .Offset(RowCount, 0).Value = Me.Account.Value
myValue = WorksheetFunction.VLookup(Range("A2"), Range("Sheet3!G1:G14"), 2, False)

I am not sure why it is not working. 我不确定为什么它不起作用。 Any suggestion will be greatly appreciated. 任何建议将不胜感激。

Thanks, 谢谢,

Try this: 尝试这个:

Private Sub Save_Click()

    Dim RowCount As Long
    Dim myValue As Variant
    Dim Sh2 As Worksheet, Sh2 As Worksheet
    Dim RefRange As Range

    With ThisWorkbook
        Set Sh2 = .Sheets("Sheet2")
        Set Sh3 = .Sheets("Sheet3")
    End With

    RowCount = Sh2.Range("A1").CurrentRegion.Rows.Count
    Set RefRange = Sh3.Range("G1:H14") '--Change as necessary.

    Sh2.Range("A1").Offset(RowCount, 0).Value = Me.Account.Value
    On Error GoTo myValueErrHandle
    myValue = WorksheetFunction.VLookup(Sh2.Range("A2").Value, RefRange, 2, False)
    '--More code, etc etc.

myValueErrHandle:
    If Err.Number = 1004 Then
        MsgBox "No match using VLOOKUP."
    End If
    Exit Sub

End Sub

As much as possible, qualify everything you can and use succinct but meaningful code. 尽可能地限制一切,并使用简洁但有意义的代码。 Don't use Range("A2") if you want what's inside it. 如果要使用Range("A2") ,请不要使用它。 Use .Value as this is the best practice. 使用.Value因为这是最佳做法。 As far as I can see, there's nothing wrong. 据我所知,没有错。 I also added an Error 1004 handling for no matches. 我还添加了Error 1004处理(没有匹配项)。 Lastly, note that I changed myValue to a Variant , just to cover bases. 最后,请注意,我将myValue更改为Variant ,只是为了覆盖基础。

Let us know if this works. 让我们知道这是否有效。

try this? 尝试这个? : Sorry too long to put in comment: :很抱歉,发表评论太久了:

With Worksheets("Sheet2")
    .Range("A1").Offset(RowCount, 0).Value = Me.Account.Value
    myValue = WorksheetFunction.VLookup(.Range("A2"), Sheeet3.Range("G1:H14"), 2, False)" 
End With

Just maybe, so try it out. 也许吧,所以尝试一下。

Sub Lookup()
Dim Emp As String
Dim Lookup_Range As Range
Dim Age As Single
Emp = "Jitu"
Set Lookup_Range = Range("A1:B8")
Age = Application.WorksheetFunction.VLookup(Emp, Lookup_Range, 2, False)

MsgBox "Age is : $ " & Age
End Sub

Table: Emp Age bibhash 22 rahul 22 abhisek 23 jitu 21 sujit 24 tinku 25 rudra 26 表格: Emp Age bibhash 22 rahul 22 abhisek 23 jitu 21 sujit 24 tinku 25 rudra 26

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

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