基于更改Combobox值设置单元格查找值并将其写入其他工作表中的另一个单元格

我试图填写Excel工作表中的某个单元格(“iNVD)从数据从工作表”数据库“基于更改combobox。工作表(数据库)中的工作表(数据库)中的comboboxA2我试图读取H2并将其写入工作表( iNVD)。我的代码和buton的错误被按下 在这里输入图像说明

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”中的单元格是数据表中的文本格式单元格。

请帮帮我。 谢谢

你有正确的技术,但不是正确的理解。

调整你的代码到这个:

 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