简体   繁体   中英

Excel VBA Named Range with dynamic created name

i have a problem with my macro. I know its alot of code (sry for that) but i think it could be helpfull. So the basic thing i want to do is, take the value of the combobox and search for it in another worksheet to get the price written in the next column. easy so far, but the name im searching for is not unique in the database. the one im searching for is only defined by beeing part of the correct named range (ie EngineMercedesDiesel)

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

I hop you can help me and (hopefully) you have a easy answer for me

ok i did it finally !

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)

it was immportant to define mname as variant datatype and define also the worksheet of my range. thank for the help

statusupdate: solved !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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