2016-01-22 51 views
0

我想在Excel工作表(“iNVD)中填充工作表”数据库“中基于更改组合框的数据中的某个单元格。 A2在选择通过组合框中我尝试阅读H2并将其写入到工作表(INVD)工作表(数据库)。我的代码和错误,而布顿按下 enter image description here基于改变Combobox值设置单元格查找值,并将其写入另一个工作表中的另一个单元格

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

请帮帮我。谢谢

回答

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

+0

喜读了, 感谢您的帮助。不是它运行错误,但有一个问题。在“iNVD”工作表上,它给我写了#N/A。我尝试与数字的colume一样,仍然无法正常工作。如果您需要更多信息,请给我发短信并再次感谢您的帮助。 –

+0

@JanHlade =我只是重读了你的queston并在'Set rngTblArray'行的代码中进行了编辑。再试一次。 –

+1

谢谢你的帮助。你的帖子非常有用!现在它运作完美。我读了发送的Vlookup Function lint;) –

相关问题