2015-10-14 202 views
0

我有我的宏的问题。我知道它的很多代码(为此),但我认为它可以是有帮助的。所以我想要做的基本事情是,取出组合框的值并在另一个工作表中搜索它,以便将价格写入下一列。 到目前为止,但在im搜索的名称是不唯一的数据库中。一个IM搜索仅由beeing正确的命名范围内的部分定义(即EngineMercedesDiesel)Excel VBA命名范围与动态创建名称

Function getprice(Matrix As Range, name As String) 

Dim i As Integer 
Dim row As Variant 
Dim col As Variant 
Dim price As Variant 
'loop to finde inside the range the name im looking for 
For i = 1 To Matrix.Rows.Count 
    If Matrix.Cells(i, 1).Value = name Then 
     row = Matrix.Cells(i, 1).Address(RowAbsolute:=True) 
     col = Matrix.Cells(i, 1).Address(ColumnAbsolute:=True) 
     price = Tabelle2.Cells(row, col + 1).Value 
     Exit For 
Next 

getprice = price 

End Function 

Private Sub cbschaltung_Change() 

Dim go As Range 
Dim handle As String 

'from here it builds the name i.e. EngineMercedesDiesel an there is a Named range with the same titel outside VBA 

teil = Range("A4").Value 
hersteller = Range("B3").Value 
handle = cbschaltung.Value 

If checkboxel.Value = True Then 

    c = checkboxel.Caption 
    Set go = teil & hersteller & c 'storing to the variable go, here ocures the error 
    Tabelle3.Range("C4").Value = getprice(go, handle) 

ElseIf checkboxmech.Value = True Then 

    c = checkboxmech.Caption 
    Set go = teil & hersteller & c 
    Tabelle3.Range("C4").Value = getprice(go, handle) 

End If 

End Sub 

我希望你们能帮助我,(希望)你对我有一个简单的答案

+1

你可以传递正确的命名范围作为函数的另一个参数吗?如果是这样,我认为在这种情况下代码很容易更新。 –

+0

sry我不明白你的意思是“另一个论点”! 问题是,即使我直接输入名称到函数中也不起作用,因为他有些不知道它是一个范围 – Quantum

+0

我看到你得到了你的答案,但我所建议的是通过命名范围到修复。因此,不是只使用'Matrix'和'name'中的两个参数,而是为命名范围添加第三个参数。无论哪种方式,你有你需要的东西:) –

回答

0

确定我终于做到了!

ElseIf checkboxmech.Value = True Then 

    c = checkboxmech.Caption 
    mname = teil & hersteller & c 
    Set go = Worksheets("Gruppendatenbank").Range(mname) 
    Tabelle3.Range("C4").Value = getprice(go, handle) 

将mname定义为变体数据类型并定义我的范围的工作表是非常重要的。 感谢您的帮助

statusupdate:求解!