Clever-Excel-Forum

Normale Version: Weitersuchen mit CommandButton
Du siehst gerade eine vereinfachte Darstellung unserer Inhalte. Normale Ansicht mit richtiger Formatierung.
Hallo Zusammen,

ich habe eine Userform mit mehreren Textboxen und einen CommandButton für die Suche. Sobald ich in einer TextBox einen Suchbegriff eingebe sollen alle Daten aus der Tabelle in die restlichen Textboxen geschrieben werden. Das funktioniert auch so weit aber wenn es mehre Ergebnisse gibt soll diese mit erneuten Betätigung des Buttons abgerufen und in die Textboxen überschrieben werden. Leider funktioniert das nicht. Ich hoffe es kann mir jemand helfen denn ich bin am verzweifeln. Schön wäre es wenn beim letzten gefunden Datensatz eine MsgBox angezeigt wird. Vielen Dank.

Code:
Private Sub CommandButton4_Click()
  Dim rZelle As Object
  Dim firstAddress As String
 
  If TextBox46.Value = "" Then
  MsgBox "Bitte eine Eingabe tätigen!", 48, "  Hinweis für " & Application.UserName
          TextBox46.SetFocus
          End If
  If TextBox46.Value <> "" Then
      With Worksheets("Gesamt")
        Set rZelle = Range(Columns(1), Columns(42)).Find("*" & TextBox46 & "*", MatchCase:=False,LookAt:=xlWhole, LookIn:=xlValues)
       
        If Not rZelle Is Nothing Then
            firstAddress = rZelle.Address
          Do
    TextBox1.Value = .Cells(rZelle.Row, 1).Value
    TextBox2.Value = .Cells(rZelle.Row, 2).Value
    TextBox7.Value = .Cells(rZelle.Row, 7).Value
    TextBox44.Value = .Cells(rZelle.Row, 3).Value
    TextBox13.Value = .Cells(rZelle.Row, 4).Value
    TextBox5.Value = .Cells(rZelle.Row, 5).Value
    TextBox6.Value = .Cells(rZelle.Row, 6).Value
    ComboBox1.Value = .Cells(rZelle.Row, 9).Value
    TextBox8.Value = .Cells(rZelle.Row, 10).Value
    TextBox9.Value = .Cells(rZelle.Row, 11).Value
    TextBox10.Value = .Cells(rZelle.Row, 12).Value
    TextBox11.Value = .Cells(rZelle.Row, 13).Value
    TextBox12.Value = .Cells(rZelle.Row, 14).Value
    TextBox21.Value = .Cells(rZelle.Row, 15).Value
    TextBox14.Value = .Cells(rZelle.Row, 16).Value
    TextBox15.Value = .Cells(rZelle.Row, 17).Value
    TextBox16.Value = .Cells(rZelle.Row, 18).Value
    TextBox17.Value = .Cells(rZelle.Row, 19).Value
    TextBox18.Value = .Cells(rZelle.Row, 20).Value
    TextBox19.Value = .Cells(rZelle.Row, 21).Value
    TextBox20.Value = .Cells(rZelle.Row, 22).Value
    TextBox22.Value = .Cells(rZelle.Row, 23).Value
    TextBox23.Value = .Cells(rZelle.Row, 24).Value
    TextBox25.Value = .Cells(rZelle.Row, 25).Value
    TextBox31.Value = .Cells(rZelle.Row, 26).Value
    TextBox26.Value = .Cells(rZelle.Row, 42).Value
    TextBox32.Value = .Cells(rZelle.Row, 28).Value
    TextBox28.Value = .Cells(rZelle.Row, 36).Value
    TextBox29.Value = .Cells(rZelle.Row, 40).Value
    TextBox30.Value = .Cells(rZelle.Row, 41).Value
    TextBox27.Value = .Cells(rZelle.Row, 27).Value
    TextBox33.Value = .Cells(rZelle.Row, 29).Value
    TextBox34.Value = .Cells(rZelle.Row, 30).Value
    TextBox35.Value = .Cells(rZelle.Row, 31).Value
    TextBox36.Value = .Cells(rZelle.Row, 32).Value
    TextBox37.Value = .Cells(rZelle.Row, 33).Value
    TextBox38.Value = .Cells(rZelle.Row, 34).Value
    TextBox24.Value = .Cells(rZelle.Row, 35).Value
    TextBox39.Value = .Cells(rZelle.Row, 37).Value
    TextBox40.Value = .Cells(rZelle.Row, 38).Value
    TextBox41.Value = .Cells(rZelle.Row, 39).Value
   
            Set rZelle = Range(Columns(1), Columns(42)).FindNext(rZelle)
        Loop While rZelle.Address <> firstAddress
        Else
          MsgBox "Die gesuchte EIngabe:  """ & TextBox46 & """  wurde nicht gefunden!", 48, " Hinweis für " & Application.UserName
          TextBox46.SetFocus
        End If
      End With
    End If 
   
  End Sub
Hallo,

teste mal damit:
Private Sub CommandButton4_Click()
Static rZelle As Object
Static firstAddress As String

If firstAddress = "" Then
If TextBox46.Value = "" Then
MsgBox "Bitte eine Eingabe tätigen!", 48, " Hinweis für " & Application.UserName
TextBox46.SetFocus
Else
With Worksheets("Gesamt")
Set rZelle = .Range(.Columns(1), .Columns(42)).Find( _
What:="*" & TextBox46 & "*", _
MatchCase:=False, _
LookAt:=xlWhole, _
LookIn:=xlValues)
End With
If Not rZelle Is Nothing Then
firstAddress = rZelle.Address
Else
MsgBox "Die gesuchte EIngabe: """ & TextBox46 & """ wurde nicht gefunden!", _
48, " Hinweis für " & Application.UserName
TextBox46.SetFocus
End If
End If
Else
With Worksheets("Gesamt")
Set rZelle = .Range(.Columns(1), .Columns(42)).FindNext(rZelle)
End With
End If
If Not rZelle Is Nothing Then
If rZelle.Address <> firstAddress Or Me.Tag = "" Then
With Worksheets("Gesamt")
TextBox1.Value = .Cells(rZelle.Row, 1).Value
TextBox2.Value = .Cells(rZelle.Row, 2).Value
TextBox44.Value = .Cells(rZelle.Row, 3).Value
TextBox13.Value = .Cells(rZelle.Row, 4).Value
TextBox5.Value = .Cells(rZelle.Row, 5).Value
TextBox6.Value = .Cells(rZelle.Row, 6).Value
TextBox7.Value = .Cells(rZelle.Row, 7).Value
ComboBox1.Value = .Cells(rZelle.Row, 9).Value
TextBox8.Value = .Cells(rZelle.Row, 10).Value
TextBox9.Value = .Cells(rZelle.Row, 11).Value
TextBox10.Value = .Cells(rZelle.Row, 12).Value
TextBox11.Value = .Cells(rZelle.Row, 13).Value
TextBox12.Value = .Cells(rZelle.Row, 14).Value
TextBox21.Value = .Cells(rZelle.Row, 15).Value
TextBox14.Value = .Cells(rZelle.Row, 16).Value
TextBox15.Value = .Cells(rZelle.Row, 17).Value
TextBox16.Value = .Cells(rZelle.Row, 18).Value
TextBox17.Value = .Cells(rZelle.Row, 19).Value
TextBox18.Value = .Cells(rZelle.Row, 20).Value
TextBox19.Value = .Cells(rZelle.Row, 21).Value
TextBox20.Value = .Cells(rZelle.Row, 22).Value
TextBox22.Value = .Cells(rZelle.Row, 23).Value
TextBox23.Value = .Cells(rZelle.Row, 24).Value
TextBox25.Value = .Cells(rZelle.Row, 25).Value
TextBox31.Value = .Cells(rZelle.Row, 26).Value
TextBox26.Value = .Cells(rZelle.Row, 42).Value
TextBox32.Value = .Cells(rZelle.Row, 28).Value
TextBox28.Value = .Cells(rZelle.Row, 36).Value
TextBox29.Value = .Cells(rZelle.Row, 40).Value
TextBox30.Value = .Cells(rZelle.Row, 41).Value
TextBox27.Value = .Cells(rZelle.Row, 27).Value
TextBox33.Value = .Cells(rZelle.Row, 29).Value
TextBox34.Value = .Cells(rZelle.Row, 30).Value
TextBox35.Value = .Cells(rZelle.Row, 31).Value
TextBox36.Value = .Cells(rZelle.Row, 32).Value
TextBox37.Value = .Cells(rZelle.Row, 33).Value
TextBox38.Value = .Cells(rZelle.Row, 34).Value
TextBox24.Value = .Cells(rZelle.Row, 35).Value
TextBox39.Value = .Cells(rZelle.Row, 37).Value
TextBox40.Value = .Cells(rZelle.Row, 38).Value
TextBox41.Value = .Cells(rZelle.Row, 39).Value
End With
Me.Tag = "x"
TextBox46.Enabled = False
Else
MsgBox "Das wars"
Set rZelle = Nothing
firstAddress = ""
Me.Tag = ""
TextBox46.Enabled = True
End If
End If
End Sub
Gruß Uwe
Hallo Uwe,

ich möchte dir herzlich Danken. Du hast mir mit dem Code sehr geholfen und es funktioniert einwandfrei.
Falls es dir keine Umstände macht, würde mich noch interessieren was an dem Ursprungs Code falsch war.
Ansonsten nochmal vielen lieben Dank.
Hallo,

der Code lief durch die Do-Loop-Schleife halt immer bis zum letzten Treffer durch. Direkt falsch war er nicht.
Ich hatte nur die fehlenden Punkte in den With-Anweisungen ergänzt, damit der Code auch fehlerfrei läuft, wenn das Blatt Gesamt nicht aktiv ist.

Gruß Uwe