Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1832to1836
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

Anzahl gefundene Zeilen

Anzahl gefundene Zeilen
12.06.2021 16:59:07
Eberhard
Hallo zusammen
Ich habe einen Code zusammengebaut welcher in einer Spalte nach einem Suchbergriff sucht. Dies funktioniert alles wie gewünscht. Nun möchte ich noch, dass es mir in einer MsgBox die Anzahl der gefundenen Zeilen ausgibt. Wie und wo muss ich das einbauen?
Besten Dank und einen schönen Abend.
Gruss Daniel

Private Sub CommandButton1_Click()
Dim i%, strAusgabe$
Dim Suchen As String
Dim ws As Worksheet
Dim LetzteZeile, LetzteSpalte As Integer
Dim sFirstAdress As String
Dim rng As Range
Set ws = Worksheets("Lagerliste_drucken")
With ListBox1
If .Selected(i) = False Then
MsgBox "Bitte Lagerort(e) auswählen!", vbInformation
Exit Sub
Else
For i = 0 To .ListCount - 1
If .Selected(i) = True Then
Suchen = strAusgabe & .List(i)
Set rng = Worksheets("WSCAR_Daten").Range("F:F").Find(Suchen, _
LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)
If rng Is Nothing Then
MsgBox "Lagerort " & Suchen & " nicht gefunden!", vbInformation
Else
If ws.Cells(1, 1) = "" Then
ws.Cells(1, 1) = "Lagerort " & Suchen
ws.Cells(1, 1).Borders.LineStyle = xlContinuous
ws.Cells(1, 1).Font.Bold = True
ws.Cells(2, 1) = "Vorname": ws.Cells(2, 2) = "Name": ws.Cells(2, 3) = "Marke"
ws.Cells(2, 4) = "Typ": ws.Cells(2, 5) = "Kontrollschild"
ws.Range(ws.Cells(2, 1), ws.Cells(2, 5)).Font.Bold = True
ws.Range(ws.Cells(2, 1), ws.Cells(2, 5)).Borders.LineStyle = xlContinuous
ws.Cells(1, 1).MergeArea.BorderAround xlContinuous, xlMedium, xlColorIndexAutomatic
Else
LetzteZeile = ws.Cells(Rows.Count, 1).End(xlUp).Row
LetzteSpalte = ws.Cells(LetzteZeile, Columns.Count).End(xlToLeft).Column
ws.Cells(LetzteZeile + 2, 1) = "Lagerort " & Suchen
ws.Cells(LetzteZeile + 2, 1).Font.Bold = True
ws.Cells(LetzteZeile + 3, 1) = "Vorname": ws.Cells(LetzteZeile + 3, 2) = "Name": ws.Cells(LetzteZeile + 3, 3) = "Marke"
ws.Cells(LetzteZeile + 3, 4) = "Typ": ws.Cells(LetzteZeile + 3, 5) = "Kontrollschild"
ws.Range(ws.Cells(LetzteZeile + 3, 1), ws.Cells(LetzteZeile + 3, 5)).Font.Bold = True
ws.Range(ws.Cells(LetzteZeile + 3, 1), ws.Cells(LetzteZeile + 3, 5)).Borders.LineStyle = xlContinuous
ws.Cells(LetzteZeile + 2, 1).MergeArea.BorderAround xlContinuous, xlMedium, xlColorIndexAutomatic
End If
sfirstaddress = rng.Address
Do
rng.Offset(0, -5).Resize(1, 5).Copy Destination:=Worksheets("Lagerliste_Drucken").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Set rng = Worksheets("WSCAR_Daten").Range("F:F").FindNext(rng)
Loop While Not rng Is Nothing And rng.Address  sfirstaddress
End If
Else
End If
Next
End If
End With
End Sub

4
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Anzahl gefundene Zeilen
12.06.2021 17:15:49
GerdL
Moin Daniel,
ungetestet.
Gruß Gerd

Private Sub CommandButton1_Click()
Dim i%, strAusgabe$
Dim Suchen As String
Dim ws As Worksheet
Dim LetzteZeile, LetzteSpalte As Integer
Dim sFirstAdress As String
Dim rng As Range
Dim Anzahl As Integer
Set ws = Worksheets("Lagerliste_drucken")
With ListBox1
If .Selected(i) = False Then
MsgBox "Bitte Lagerort(e) auswählen!", vbInformation
Exit Sub
Else
For i = 0 To .ListCount - 1
If .Selected(i) = True Then
Suchen = strAusgabe & .List(i)
Set rng = Worksheets("WSCAR_Daten").Range("F:F").Find(Suchen, _
LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)
If rng Is Nothing Then
MsgBox "Lagerort " & Suchen & " nicht gefunden!", vbInformation
Else
If ws.Cells(1, 1) = "" Then
ws.Cells(1, 1) = "Lagerort " & Suchen
ws.Cells(1, 1).Borders.LineStyle = xlContinuous
ws.Cells(1, 1).Font.Bold = True
ws.Cells(2, 1) = "Vorname": ws.Cells(2, 2) = "Name": ws.Cells(2, 3) = "Marke"
ws.Cells(2, 4) = "Typ": ws.Cells(2, 5) = "Kontrollschild"
ws.Range(ws.Cells(2, 1), ws.Cells(2, 5)).Font.Bold = True
ws.Range(ws.Cells(2, 1), ws.Cells(2, 5)).Borders.LineStyle = xlContinuous
ws.Cells(1, 1).MergeArea.BorderAround xlContinuous, xlMedium, xlColorIndexAutomatic
Else
LetzteZeile = ws.Cells(Rows.Count, 1).End(xlUp).Row
LetzteSpalte = ws.Cells(LetzteZeile, Columns.Count).End(xlToLeft).Column
ws.Cells(LetzteZeile + 2, 1) = "Lagerort " & Suchen
ws.Cells(LetzteZeile + 2, 1).Font.Bold = True
ws.Cells(LetzteZeile + 3, 1) = "Vorname": ws.Cells(LetzteZeile + 3, 2) = "Name": ws.Cells(LetzteZeile + 3, 3) = "Marke"
ws.Cells(LetzteZeile + 3, 4) = "Typ": ws.Cells(LetzteZeile + 3, 5) = "Kontrollschild"
ws.Range(ws.Cells(LetzteZeile + 3, 1), ws.Cells(LetzteZeile + 3, 5)).Font.Bold = True
ws.Range(ws.Cells(LetzteZeile + 3, 1), ws.Cells(LetzteZeile + 3, 5)).Borders.LineStyle = xlContinuous
ws.Cells(LetzteZeile + 2, 1).MergeArea.BorderAround xlContinuous, xlMedium, xlColorIndexAutomatic
End If
sfirstaddress = rng.Address
Do
Anzahl = Anzahl + 1
rng.Offset(0, -5).Resize(1, 5).Copy Destination:=Worksheets("Lagerliste_Drucken").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Set rng = Worksheets("WSCAR_Daten").Range("F:F").FindNext(rng)
Loop While Not rng Is Nothing And rng.Address  sfirstaddress
End If
Else
End If
Next
End If
End With
If Anzahl > 0 Then MsgBox "AnZahl: " & Anzahl
End Sub

Anzeige
AW: Anzahl gefundene Zeilen
12.06.2021 17:24:30
Daniel
Hi
Am einfachsten mit
MsgBox Worksheetfunction.CountIf(Worksheets("WSCAR_Daten").Range("F:F"), Suchen)
Gruß Daniel
ListBox Fehlermeldung wenn keine Auswahl
13.06.2021 12:17:35
Eberhard
Guten Morgen
Vielen Dank für Eure Hilfe. Habe den Code von Daniel angewendet. Funktioniert auch.
Nun habe ich festgestellt, dass mein Code gar nicht richtig funktioniert.
Wenn ich in der Listbox die zweite oder dritte Auswahl anwähle, kommt die Fehlermeldung "Bitte Lagerort(e) auswählen!".
Wähle ich die erste Auswahl oder mehrere, so funktioniert es wie gewünscht. Irgendwo habe ich noch einen Fehler eingebaut. Sieht vielleicht jemand gerade wo der Fehler liegt?
Besten Dank und einen schönen Sonntag.
Gruss Daniel Eberhard
Anzeige
AW: ListBox Fehlermeldung wenn keine Auswahl
14.06.2021 09:05:22
hary
Moin
Eine Moeglichkeit mit einer Public Variablen die z.B. durch ListBox1_MouseDown gesetzt wird. Die Variable muss ausserhalb der Sub stehen.
Probier mal.

Option Explicit
Public a As Boolean
Private Sub CommandButton1_Click()
Dim i%, strAusgabe$
Dim Suchen As String
Dim ws As Worksheet
Dim LetzteZeile, LetzteSpalte As Integer
Dim sFirstAdress As String
Dim rng As Range
Set ws = Worksheets("Lagerliste_drucken")
With ListBox1
If a = False Then
MsgBox "Bitte Lagerort(e) auswählen!", vbInformation
Exit Sub
Else
For i = 0 To .ListCount - 1
If .Selected(i) = True Then
Suchen = strAusgabe & .List(i)
Set rng = Worksheets("WSCAR_Daten").Range("F:F").Find(Suchen, _
LookIn:=xlValues, lookat:=xlWhole, MatchCase:=True)
If rng Is Nothing Then
MsgBox "Lagerort " & Suchen & " nicht gefunden!", vbInformation
Else
If ws.Cells(1, 1) = "" Then
ws.Cells(1, 1) = "Lagerort " & Suchen
ws.Cells(1, 1).Borders.LineStyle = xlContinuous
ws.Cells(1, 1).Font.Bold = True
ws.Cells(2, 1) = "Vorname": ws.Cells(2, 2) = "Name": ws.Cells(2, 3) = "Marke"
ws.Cells(2, 4) = "Typ": ws.Cells(2, 5) = "Kontrollschild"
ws.Range(ws.Cells(2, 1), ws.Cells(2, 5)).Font.Bold = True
ws.Range(ws.Cells(2, 1), ws.Cells(2, 5)).Borders.LineStyle = xlContinuous
ws.Cells(1, 1).MergeArea.BorderAround xlContinuous, xlMedium, xlColorIndexAutomatic
Else
LetzteZeile = ws.Cells(Rows.Count, 1).End(xlUp).Row
LetzteSpalte = ws.Cells(LetzteZeile, Columns.Count).End(xlToLeft).Column
ws.Cells(LetzteZeile + 2, 1) = "Lagerort " & Suchen
ws.Cells(LetzteZeile + 2, 1).Font.Bold = True
ws.Cells(LetzteZeile + 3, 1) = "Vorname": ws.Cells(LetzteZeile + 3, 2) = "Name": ws.Cells(LetzteZeile + 3, 3) = "Marke"
ws.Cells(LetzteZeile + 3, 4) = "Typ": ws.Cells(LetzteZeile + 3, 5) = "Kontrollschild"
ws.Range(ws.Cells(LetzteZeile + 3, 1), ws.Cells(LetzteZeile + 3, 5)).Font.Bold = True
ws.Range(ws.Cells(LetzteZeile + 3, 1), ws.Cells(LetzteZeile + 3, 5)).Borders.LineStyle = xlContinuous
ws.Cells(LetzteZeile + 2, 1).MergeArea.BorderAround xlContinuous, xlMedium, xlColorIndexAutomatic
End If
sfirstaddress = rng.Address
Do
rng.Offset(0, -5).Resize(1, 5).Copy Destination:=Worksheets("Lagerliste_Drucken").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Set rng = Worksheets("WSCAR_Daten").Range("F:F").FindNext(rng)
Loop While Not rng Is Nothing And rng.Address  sfirstaddress
End If
Else
End If
Next
a = False
End If
End With
End Sub
Private Sub ListBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
a = True
End Sub
gruss hary
Anzeige

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige