Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1292to1296
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

VBA Suche

VBA Suche
22.01.2013 14:47:54
Erol
Hallo,
habe ein für Euch bestimmt nur kleines Such-Problem. ,)
Derzeit rufe ich eine UserForm auf, in der mir die Suchergebnisse(Suchwert aus einer textbox)in einer Listbox angezeigt werden.
Nun möchte ich gern eine erweiterte Suche einbauen, in der nach 2 Suchkreterien in Abhängigkeit gesucht wird. (Also nach Werten aus TextBox300 und TextBox301)
Könnt Ihr mir da weiterhelfen?
Hier der Code, den ich derzeit für die einfache Suche verwende:

Private Sub CommandButton1_Click()   '  Suchen
Dim rng As Range
Dim strFirst As String
Dim vtmp() As Long
Dim tntC As Integer
If Len(Trim(TextBox300)) = 0 Then Exit Sub
ListBox1.Clear
For IntC = 1 To 22
Controls("TextBox" & IntC) = ""
Next
ReDim vtmp(0)
With Sheets("Tabelle1")
Set rng = .Range("B:V").Find(What:=TextBox300, LookAt:=xlPart)
If Not rng Is Nothing Then
strFirst = rng.Address
Do
If Not (IsNumeric(Application.Match(rng.Row, vtmp, 0))) Then
ReDim Preserve vtmp(UBound(vtmp) + 1)
vtmp(UBound(vtmp)) = rng.Row
ListBox1.AddItem .Cells(rng.Row, 2)
ListBox1.List(ListBox1.ListCount - 1, 1) = .Cells(rng.Row, 3)
ListBox1.List(ListBox1.ListCount - 1, 2) = .Cells(rng.Row, 4)
ListBox1.List(ListBox1.ListCount - 1, 3) = .Cells(rng.Row, 5)
ListBox1.List(ListBox1.ListCount - 1, 4) = .Cells(rng.Row, 6)
ListBox1.List(ListBox1.ListCount - 1, 5) = .Cells(rng.Row, 7)
ListBox1.List(ListBox1.ListCount - 1, 6) = .Cells(rng.Row, 8)
ListBox1.List(ListBox1.ListCount - 1, 7) = rng.Row
End If
Set rng = .Range("B:V").FindNext(rng)
Loop While Not rng Is Nothing And rng.Address  strFirst
End If
End With
If ListBox1.ListCount > 0 Then
ListBox1.ListIndex = 0
Label14.Caption = "Trefferliste"
Else
Label14.Caption = "Keinen Eintrag gefunden!"
End If
Set rng = Nothing
End Sub

12
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: VBA Suche
22.01.2013 14:55:08
Daniel
Hi
hängt jetzt davon ab, was jetzt die Bedingungen sind und wie gesucht werden soll, das müsstst du mal genauer beschreiben.
muss man wirklich in allen Spalten von B-V suchen oder können die Suchbegriffe nur in bestimmten Spalten auftreten?
falls ja, könntest du auch mit dem Autofilter arbeiten.
Gruß Daniel

AW: VBA Suche
22.01.2013 15:22:33
Erol
Hi,
Danke für die schnelle Antwort!
Es reicht auch, wenn mann die 2 Spalten, in denen gesucht werden soll vorher definiert.
Wichtig wäre nur, dass die Ausgabe des Ergebnisses in der ListBox1 der UserForm1 erfolgt, da ich unseren Mitarbeitern eine "komfortable, übersichtliche Lösung" bieten will ;)
Gruß Erol

Anzeige
AW: VBA Suche
22.01.2013 15:26:06
Daniel
Hi
du könntst den ersten Teil meiner Frage noch genauer beschreiben, für konkreten Code reicht das noch nicht.
Gruß Daniel

AW: VBA Suche
22.01.2013 15:40:08
Erol
Hi,
also den 1. Suchbegriff würde ich über eine TextBox300 eingeben wollen und in Spalte F suchen (hier geht es um Postleitzahlen)
den 2. Suchbegriff würde ich über auch über eine TextBox301 eingeben wollen, der dann den Bereich von Spalte S bis Spalte U durchsucht. (Hintergrund: hier geh es um Warengruppen - einem Lieferanten können 3 Warengruppen zugeordnet werden)
Ziel ist es, mir alle Lieferanten in der ListBox1 anzeigen zu lassen, die meinem Suchkreterium Textbox300 und Textbox301 entsprechen.
gruß erol

Anzeige
AW: VBA Suche
22.01.2013 16:01:08
Daniel
Hi
wenn man die Liste sortieren darf, kannst du dir den Code auch deutlich vereinfachen, indem du alle betreffenden Zeilen per Formel markierst und danach sortierst, so daß sie lückenlos untereinanderstehen.
Dann kannst du den Block mit den Fundstellen direkt in die Listbox schreiben:
With ActiveSheet.UsedRange.Columns(30) ' hier ne leere Spalte angeben
.FormulaR1C1 = "=IF(AND(TEXT(RC6,""00000"")=""" & Textbox300.Text & """,CountIf(RC19:RC21,"" _
" & Textbox301.Text & """)>0),1,"""")"
If WorksheetFunction.Sum(.Cells) > 0 Then
.EntireRow.Sort key1:=.Cells(1, 1), order1:=xlAscending, Header:=xlYes
listbox1.List = Intersect(.SpecialCells(xlCellTypeFormulas, 1).EntireRow, Range("C:H")). _
Value
Label14.Caption = "Trefferliste"
Else
listbox1.Clear
Label14.Caption = "Kein Eintrag gefunden"
End If
.ClearContents
End With
gruß Daniel

Anzeige
AW: VBA Suche
22.01.2013 15:33:40
Peter
Hallo Erol,
da musst Du die Abfrage nach Do einfügen (meine Abfrage anpassen!)
Private Sub CommandButton1_Click()   '  Suchen
Dim rng As Range
Dim strFirst As String
Dim vtmp() As Long
Dim tntC As Integer
If Len(Trim(TextBox300)) = 0 Then Exit Sub
ListBox1.Clear
For IntC = 1 To 22
Controls("TextBox" & IntC) = ""
Next
ReDim vtmp(0)
With Sheets("Tabelle1")
Set rng = .Range("B:V").Find(What:=TextBox300, LookAt:=xlPart)
If Not rng Is Nothing Then
strFirst = rng.Address
Do
If TextBox301.Value = Sheets("Tabelle1").Range("A7").Value Then
If Not (IsNumeric(Application.Match(rng.Row, vtmp, 0))) Then
ReDim Preserve vtmp(UBound(vtmp) + 1)
vtmp(UBound(vtmp)) = rng.Row
ListBox1.AddItem .Cells(rng.Row, 2)
ListBox1.List(ListBox1.ListCount - 1, 1) = .Cells(rng.Row, 3)
ListBox1.List(ListBox1.ListCount - 1, 2) = .Cells(rng.Row, 4)
ListBox1.List(ListBox1.ListCount - 1, 3) = .Cells(rng.Row, 5)
ListBox1.List(ListBox1.ListCount - 1, 4) = .Cells(rng.Row, 6)
ListBox1.List(ListBox1.ListCount - 1, 5) = .Cells(rng.Row, 7)
ListBox1.List(ListBox1.ListCount - 1, 6) = .Cells(rng.Row, 8)
ListBox1.List(ListBox1.ListCount - 1, 7) = rng.Row
End If
End If
Set rng = .Range("B:V").FindNext(rng)
Loop While Not rng Is Nothing And rng.Address  strFirst
End If
End With
If ListBox1.ListCount > 0 Then
ListBox1.ListIndex = 0
Label14.Caption = "Trefferliste"
Else
Label14.Caption = "Keinen Eintrag gefunden!"
End If
Set rng = Nothing
End Sub
Gruß Peter

Anzeige
AW: VBA Suche
22.01.2013 15:48:37
Erol
Hallo Peter,
danke für die Hilfe..
Bekomme aber ein Fehler: Typen unverträglich
Laufzeitfehler:33
Gruß
Erol

AW: VBA Suche
22.01.2013 15:40:55
Peter
Hallo Erol,
vielleicht besser noch so:
Private Sub CommandButton1_Click()   '  Suchen
Dim rng As Range
Dim strFirst As String
Dim vtmp() As Long
Dim tntC As Integer
If Len(Trim(TextBox300)) = 0 Then Exit Sub
ListBox1.Clear
For IntC = 1 To 22
Controls("TextBox" & IntC) = ""
Next
ReDim vtmp(0)
With Sheets("Tabelle1")
Set rng = .Range("B:V").Find(What:=TextBox300, LookAt:=xlPart)
If Not rng Is Nothing Then
strFirst = rng.Address
Do
If TextBox301.Value = Sheets("Tabelle1").Range("A" & rng.Row).Value Then
If Not (IsNumeric(Application.Match(rng.Row, vtmp, 0))) Then
ReDim Preserve vtmp(UBound(vtmp) + 1)
vtmp(UBound(vtmp)) = rng.Row
ListBox1.AddItem .Cells(rng.Row, 2)
ListBox1.List(ListBox1.ListCount - 1, 1) = .Cells(rng.Row, 3)
ListBox1.List(ListBox1.ListCount - 1, 2) = .Cells(rng.Row, 4)
ListBox1.List(ListBox1.ListCount - 1, 3) = .Cells(rng.Row, 5)
ListBox1.List(ListBox1.ListCount - 1, 4) = .Cells(rng.Row, 6)
ListBox1.List(ListBox1.ListCount - 1, 5) = .Cells(rng.Row, 7)
ListBox1.List(ListBox1.ListCount - 1, 6) = .Cells(rng.Row, 8)
ListBox1.List(ListBox1.ListCount - 1, 7) = rng.Row
End If
End If
Set rng = .Range("B:V").FindNext(rng)
If rng.Address = strFirst Then
MsgBox "Der gesuchte Begriff  """ & TextBox301.Value & _
"""  wurde nicht gefunden.", _
48, "   Hinweis für " & Application.UserName
TextBox301.SetFocus
End If
Loop While Not rng Is Nothing And rng.Address  strFirst
End If
End With
If ListBox1.ListCount > 0 Then
ListBox1.ListIndex = 0
Label14.Caption = "Trefferliste"
Else
Label14.Caption = "Keinen Eintrag gefunden!"
End If
Set rng = Nothing
End Sub

Gruß Peter

Anzeige
AW: VBA Suche
22.01.2013 15:44:32
Peter
Hallo Erol,
also sollte Deine Abfrage evtl. so lauten
strFirst = rng.Address
Do
If TextBox301.Value = Sheets("Tabelle1").Range("S" & rng.Row).Value Or _
TextBox301.Value = Sheets("Tabelle1").Range("T" & rng.Row).Value Or _
TextBox301.Value = Sheets("Tabelle1").Range("U" & rng.Row).Value Then
If Not (IsNumeric(Application.Match(rng.Row, vtmp, 0))) Then

Gruß Peter

AW: VBA Suche
22.01.2013 16:11:04
Erol
strFirst = rng.Address
Do
If TextBox301.Value = Sheets("Tabelle1").Range("S" & rng.Row).Value Or _
TextBox301.Value = Sheets("Tabelle1").Range("T" & rng.Row).Value Or _
TextBox301.Value = Sheets("Tabelle1").Range("U" & rng.Row).Value Then
If Not (IsNumeric(Application.Match(rng.Row, vtmp, 0))) Then
Hey,
Danke- so funktionierts schon mal!Leider geht es nur, wenn der Suchtext genau dem Text in Spalte "S" "T" und "U" entspricht
In TextBox300 brauch ich nur Bruchstücke eingeben und er liefert mir ein Ergebnis.
In TextBox301 müssen die Eingabe und der tatsächliche Wert genau übereinstimmen (Klein und Großschreibung)
Gibt es da auch noch eine Lösung?
Dank im Voraus...
Gruß Erol

Anzeige
AW: VBA Suche
22.01.2013 16:39:54
Erol
Habs gelöst ;)
Vielen Dank noch einmal an Alle für die schnelle Hilfe
Gruß Erol

AW: VBA Suche
22.01.2013 17:20:38
Peter
Hallo Erol,
dann evtl. so:
Option Explicit
Private Sub CommandButton1_Click()   '  Suchen
Dim rng        As Range
Dim strFirst   As String
Dim vtmp()     As Long
Dim tntC       As Integer
Dim rZelle     As Range
Dim bGefunden  As Boolean
If Len(Trim(TextBox300)) = 0 Then Exit Sub
ListBox1.Clear
For IntC = 1 To 22
Controls("TextBox" & IntC) = ""
Next
ReDim vtmp(0)
With Sheets("Tabelle1")
Set rng = .Range("B:V").Find(What:=TextBox300, LookAt:=xlPart)
If Not rng Is Nothing Then
strFirst = rng.Address
Do
For Each rZelle In Sheets("Tabelle1").Range("S:U")
If LCase(rZelle.Value) Like "*" & LCase(TextBox301.Value) & "*" Then
bGefunden = True
Exit For
End If
Next rZelle
If bGefunden Then
If Not (IsNumeric(Application.Match(rng.Row, vtmp, 0))) Then
ReDim Preserve vtmp(UBound(vtmp) + 1)
vtmp(UBound(vtmp)) = rng.Row
ListBox1.AddItem .Cells(rng.Row, 2)
ListBox1.List(ListBox1.ListCount - 1, 1) = .Cells(rng.Row, 3)
ListBox1.List(ListBox1.ListCount - 1, 2) = .Cells(rng.Row, 4)
ListBox1.List(ListBox1.ListCount - 1, 3) = .Cells(rng.Row, 5)
ListBox1.List(ListBox1.ListCount - 1, 4) = .Cells(rng.Row, 6)
ListBox1.List(ListBox1.ListCount - 1, 5) = .Cells(rng.Row, 7)
ListBox1.List(ListBox1.ListCount - 1, 6) = .Cells(rng.Row, 8)
ListBox1.List(ListBox1.ListCount - 1, 7) = rng.Row
End If
Else
MsgBox "Der gesuchte Begriff  """ & TextBox301.Value & _
"""  wurde nicht gefunden.", _
48, "   Hinweis für " & Application.UserName
TextBox301.SetFocus
End If
Set rng = .Range("B:V").FindNext(rng)
Loop While Not rng Is Nothing And rng.Address  strFirst
End If
End With
If ListBox1.ListCount > 0 Then
ListBox1.ListIndex = 0
Label14.Caption = "Trefferliste"
Else
Label14.Caption = "Keinen Eintrag gefunden!"
End If
Set rng = Nothing
End Sub

Gruß Peter
Anzeige

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige