Verschachtelte Suche funktioniert nicht ?!?!?
23.10.2004 17:03:21
otto-mueller
ich habe folgendes Anliegen:
Ich habe 3 Tabellen
- Alt
- Neu
- Alt-Neu
In der Tabelle Alt sind alte Produktnummern mit zugeordneten MaterialNr. + Menge des Materials
In der Tabelle Neu sind alte Produktnummern, die durch neue ProdNr aus der Tabelle "Alt-Neu" zugeordnet werden.
Dazu habe ich einen
Sub und eine Function.
Die Funktion ist die Suchfunktion.
Die Suchfunktion wird innerhalb einer anderen Suchfunktions aufgerufen. Sobald die innere Suchfunktion ausgeführt ist und die äußere Suchfunktion für die nächste Suche initialisiert wird, bekomme ich Fehlermeldung 91, "Objektvariable oder With-Blockvariable nicht festgelegt"
Wenn ich die innere Suchfunktion auskommentiere, dann gehts ?!?!?
Hat jemand eine Idee, woran es liegen könnte?
Die Datei findet Ihr hier
<a href="https://www.herber.de/bbs/user/12564.zip">https://www.herber.de/bbs/user/12564.zip</a>
Und hier mein Code:
Sub Konvert()
Dim tbl1 As Worksheet
Dim tbl2 As Worksheet
Dim tbl3 As Worksheet
Set tbl1 = Worksheets("Neu")
Set tbl2 = Worksheets("Alt")
Set tbl3 = Worksheets("Alt-Neu")
'Vars für Produkte
Const tbl1ProdCol = 1
Dim Prod As String
Dim ProdNrNeu As String
Dim ProdNrFind As Range
Dim ProdNrFindRow As Integer
Dim ProdNrFirstAddress As String
'Vars für Materialien
Dim MatNr As String
Dim MatNrSuche As String
Dim Menge As Double
'Vars in Tabelle1
Dim tbl1MatNr As Integer
Dim tbl1MengeCol As Integer
tbl1MatNr = 2 'B
tbl1MengeCol = 3 'C
'Const in Tabelle2
Const tbl2ProdCol = 1
Const tbl2MatNrCol = 2
Const tbl2MengeCol = 3
Const tbl3ProdNrAltCol = 1 'A
Const tbl3ProdNrNeuCol = 2 'B
Const fDataRow = 9
lDataRow = tbl1.Cells(65536, tbl1ProdCol).End(xlUp).Row
For r = fDataRow To lDataRow
ProdNr = tbl1.Cells(r, tbl1ProdCol)
With tbl2.Columns(tbl2ProdCol)
.EntireColumn.Hidden = False
Set ProdNrFind = .Find(ProdNr, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)
If Not ProdNrFind Is Nothing Then
ProdNrFirstAddress = ProdNrFind.Address
Do
ProdNrFindRow = ProdNrFind.Row
MatNr = tbl2.Cells(ProdNrFindRow, tbl2MatNrCol)
MatNrSuche = Suche(MatNr, tbl3, tbl3ProdNrAltCol, tbl3ProdNrNeuCol)
If MatNrSuche <> "" Then
MatNr = MatNrSuche
Menge = tbl2.Cells(ProdNrFindRow, tbl2MengeCol)
With tbl1
.Cells(r, tbl1MatNr) = MatNr
.Cells(r, tbl1MengeCol) = Menge
End With
tbl1MatNr = tbl1MatNr + 5
tbl1MengeCol = tbl1MengeCol + 5
End If
'Hier kommt es zum beschriebenen Problem
Set ProdNrFind = tbl2.Columns(tbl2ProdCol).FindNext(ProdNrFind)
Loop While Not ProdNrFind Is Nothing And ProdNrFirstAddress <> ProdNrFind.Address
ProdNrNeu = Suche(Prod, tbl3, tbl3ProdNrAltCol, tbl3ProdNrNeuCol)
If ProdNrNeu <> "" Then
tbl1.Cells(r, tbl1ProdCol + 0) = ProdNrNeu
End If
End If
End With
tbl1MatNr = 2 'B
tbl1MengeCol = 3 'C
Next
End Sub
Function Suche(Key As String, Wks As Worksheet, SearchCol As Integer, OutputCol As Integer)
Dim KeyFind As Range
Dim KeyFindRow As Integer
With Wks.Columns(SearchCol)
.EntireColumn.Hidden = False
Set KeyFind = .Find(Key, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)
If Not KeyFind Is Nothing Then
KeyFindRow = KeyFind.Row
Key = Wks.Cells(KeyFindRow, OutputCol)
Suche = Key
End If
End With
End Function
-------------------------------------------------------------------------
Ich habe es jetzt vorläufig mit einer Do Loop Schleife statt der innere Suchfunktion gelöst, aber würde mich trotzdem intresserien, woran es liegt.
Danke vielmals vorab
Viele Grüße
Otto