根据更改的Combobox值设置单元格vlookup值,并将其写入其他工作表中的另一个单元格

时间:2016-01-22 15:42:39

标签: excel vba excel-vba combobox

我正在尝试使用工作表“数据库”中基于更改组合框的数据来填充excel工作表(“iNVD)中的某个单元格。工具表(数据库)中的单元格A2由combobox选择我尝试读取H2并将其写入到工作表(iNVD)。按下按钮时我的代码和错误 enter image description here

Dim rngTblArray As Range

Set nvdsheet = Worksheets("iNVD")
With Worksheets("database")
    'Note when using With statement that .Range and .Cells start with a dot
    Set rngTblArray = .Range(.Cells(2, 8), .Cells(500000, 8))   'One column
End With
'MsgBox rngTblArray.Address(External:=True)  'This line for demo purposes only

nvdsheet.Cells(3, 7) = Application.VLookup(CLng(Me.cbmnaslovnvd.Value), rngTblArray, 2, 0)

Me.vbmnaslovnvd.Value,例如返回“Ulica Jakca 1”但是 CLng(Me.cbmnaslovnvd.Value)=我使用buttonclick。

excel“Ulica Jakca 1”中的单元格是数据表中的文本格式单元格。

请帮帮我。谢谢

1 个答案:

答案 0 :(得分:2)

你有正确的技巧,但没有正确理解它。

将代码调整为:

Dim rngTblArray As Range

Set nvdsheet = Worksheets("iNVD")
With Worksheets("database")
    'Note when using With statement that .Range and .Cells start with a dot
    ' ** set the ENTIRE lookup column reference (A:H)
    Set rngTblArray = .Range(.Cells(2, 2), .Cells(500000, 8))   'One column
End With
'MsgBox rngTblArray.Address(External:=True)  'This line for demo purposes only

' ** CLng will not work with text values (that cannot transform to numbers)
' ** lookup the 8th column, since H is 8 columns from A, where the first lookup value is
nvdsheet.Cells(3, 7) = Application.VLookup(Me.cbmnaslovnvd.Value, rngTblArray, 8, 0)

我做了一些评论(与**一致的行),但为了更多地了解我做了两次更改的原因,请阅读VLOOKUP FUNCTION