AW: Suche über Textbox gibt Fehlermeldung
24.12.2018 20:54:46
Nepumuk
Hallo Alfred,
ich hab mir mal deinen Suchen-Button zur Brust genommen:
'*************************************************************************
'* *
'* BUTTON: SUCHEN *
'* *
'*************************************************************************
Private Sub CommandButton2_Click()
Dim WkSh As Worksheet
Dim myRange As Range
Dim strAddress As String
Dim bolAbbruch As Boolean
' CommandButton3.Enabled = False ' den Ändern-Button sperren
' CommandButton4.Enabled = False ' den Löschen-Button sperren
Set WkSh = Worksheets("Daten")
If TextBox1.Value = "" Then
MsgBox "Es fehlt ein Suchbegriff im Feld Name - Abbruch", _
vbExclamation, " Hinweis für " & Application.UserName
TextBox1.SetFocus
Else
TextBox1.Value = WorksheetFunction.Proper(TextBox1.Value)
' nachfolgend werden die TextBox1.Werte gesucht und gefunden.
' Mit LookIn:=xlValues wird nach den Zellwerten gesucht.
' Mit LookAt:=xlPart muß der Suchbegriff nicht komplett mit
' dem Suchergebnis übereinstimmen.
With WkSh
Set myRange = .Columns(1).Find(What:=TextBox1.Value, _
LookIn:=xlValues, LookAt:=xlPart, After:=.Cells(.Rows.Count, 1))
If Not myRange Is Nothing Then
strAddress = myRange.Address
Do
FundZeile = myRange.Row
Call Anzeigen(myRange)
If MsgBox("Weitersuchen?", vbQuestion Or vbYesNo, " Abfrage") = vbNo Then
bolAbbruch = True
Exit Sub
Else
Set myRange = .Columns(1).FindNext(myRange)
End If
Loop Until myRange.Address = strAddress
If Not bolAbbruch Then
MsgBox "Keine weiteren Datensätze gefunden.", _
vbExclamation, " Information für " & Application.UserName
FundZeile = 0
Else
MsgBox "Keinen übereinstimmenden Datensatz gefunden", _
vbExclamation, " Information für " & Application.UserName
FundZeile = 0
End If
Else
MsgBox "Keinen übereinstimmenden Datensatz gefunden", _
vbExclamation, " Information für " & Application.UserName
FundZeile = 0
With TextBox2
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
End If
End With
End If
End Sub
Private Sub Anzeigen(ByRef probjRange As Range)
Dim lngIndex As Long
For lngIndex = 1 To 74
Controls("TextBox" & CStr(lngIndex)).Value = probjRange.Offset(0, lngIndex - 1).Value
Next
CommandButton3.Enabled = True ' den Ändern-Button freigeben
CommandButton4.Enabled = True ' den Löschen-Button freigeben
End Sub
Die Zahlenwerte in deinen MsgBoxen habe ich durch ihre Konstanten ersetzt. Das ist leichter zu lesen (was sagt dir 36 und was vbQuestion Or vbYesNo?).
Gruß
Nepumuk