Clever-Excel-Forum

Normale Version: Listbox fülle
Du siehst gerade eine vereinfachte Darstellung unserer Inhalte. Normale Ansicht mit richtiger Formatierung.
Seiten: 1 2 3
Hallo Zusammen

Ich komme nicht weiter und wäre um Hilfe froh:

Excel 365

Situation:

Ich habe eine UserForm welche folgende Objekte enthält:

1 X ComboBox
1 X TextBox
1 X ListBox
1 X CommandButton

ComboBox: Diese wird aus einem Sheet (Tabelle1) befüllt.
TextBox: Hier wird eine Zahl manuell eingegeben

Mittels CommandButton möchte ich nun die Listbox folgender massen befüllen.

Spalte eins in ListBox = Auswahl aus ComboBox
Spalte zwei in ListBox = Eingabe aus TextBox
Spalte drei in ListBox = *Preis (Siehe Beschreibung *)
* Tabelle 1 = [attachment=35011]
Spalte G (Preis/Einheit) enthält Formel  =[@Einkaufspreis]/([@Einkaufsmenge]*[@Umrechnungsfaktor])

In Spalte drei soll nun folgendes ausgegeben werden (Schreibe mal den Code welchen ich verwenden möchte in normaler Sprache):

Suche in Tabelle 1 die Zeile mit eingegebenen Wert aus ComboBox.
Nimm den Wert aus Spalte (Preis/Einheit) welcher in der gefunden Zeile liegt und multipliziere den Wert mit dem eingegebenen Wert aus TextBox
Gib den Wert in Spalte 3 in ListBox wieder.

(Produkt|Menge|Preis)

Ende *

Diese ganze Abfrage möchte ich dann endlos wiederholen können. Jede Eingabe soll in der Listbox eine weitere Zeile generieren.

Am Ende sollte ich dann eine Liste erhalten mit allen eingegebenen werten.

Was ich bis jetzt geschafft habe.

Ich bringe es fertig, dass die erste Spalte (Produkt) aufgelistet wird.
Jedoch kriege ich keine zweite Zeile hin und bei der Dritten Zeile bin ich absolut ahnungslos wie anstellen.

Den Code welchen ich bis jetzt habe: 
rivate Sub CommandButton1_Click()

  Dim a As Range
  Dim b As Range
  Dim c As Range
    Dim rngBereich As Range
    Dim lngAnzahl As Long
    Dim strFirst As String
   
 
       
    With Sheets("Material Liste")
        Set rngBereich = .Columns("A:A")
        Set a = rngBereich.Find(ComboBox1, LookIn:=xlValues, lookat:=xlWhole)
       
       
    If Not a Is Nothing Then
        strFirst = a.Address
            Do
            ListBox1.AddItem .Cells(a.Row, 1)
            lngAnzahl = ListBox1.ListCount
            ListBox1.List(lngAnzahl - 1, 1) = .Cells(a.Row, 1)
            ListBox1.List(lngAnzahl - 1, 2) = .Cells(a.Row, 2)
            ListBox1.List(lngAnzahl - 1, 3) = .Cells(a.Row, 8)
            ListBox1.List(lngAnzahl - 1, 4) = .Cells(a.Row, 3)
            ListBox1.List(lngAnzahl - 1, 5) = .Cells(a.Row, 74)
            Set a = rngBereich.FindNext(a)
            Loop While Not a Is Nothing And a.Address <> strFirst
        End If

With TextBox1
    Set b = rngBereich.Find(TextBox1)
   
    If Not b Is Nothing Then
        strFirst = b.Address
            Do
            ListBox1.AddItem .Text(b.Row, 1)
            lngAnzahl = ListBox1.ListCount
            ListBox1.List(lngAnzahl - 1, 1) = .Text(b.Row, 1)
            ListBox1.List(lngAnzahl - 1, 2) = .Text(b.Row, 2)
            ListBox1.List(lngAnzahl - 1, 3) = .Text(b.Row, 8)
            ListBox1.List(lngAnzahl - 1, 4) = .Text(b.Row, 3)
            ListBox1.List(lngAnzahl - 1, 5) = .Text(b.Row, 74)
            Set b = rngBereich.FindNext(b)
            Loop While Not b Is Nothing And b.Address <> strFirst
        End If
   
    End With
       
    End With

End Sub


So wie ich mir das gedacht habe, müsste man nun Dim b und c noch befüllen um die Spalten 2 und 3 in der Listbox zu erhalten?
Bin echt ratlos.
Dim b funktioniert überhaupt nicht.

Bin froh für jegliche Hinweise.

Gruss
Demech
Hallöchen,

DIm b ... sollte doch noch keinen Fehler bringen, der kommt bestimmt weiter unten, insbesondere, wenn z.B. nix gefunden wird. Wäre sicher nicht verkehrt, die Zeile nebst der Fehlermeldung zu veröffentlichen.
Servus

Leider erhalte ich einen Fehler:

Hier das Bild:

[attachment=35012]
Hallöchen,

so, nun schaue Dir die Zeile mal genau an wo der Fehler passiert und vergleiche sie mit a

bei a hast Du
ListBox1.AddItem .Cells(a.Row, 1)

bei b hingegen nimmst Du
ListBox1.AddItem .Text(b.Row, 1)

Bei a beziehst Du einen Zellinhalt, bei b versucht Du aus einer Textbox irgendwo einen Eintrag in Spalte 1 in irgendeiner Zeile zu entnehmen.

Gesucht hast Du aber dem Blatt:
Set b = rngBereich.Find(TextBox1)

Fehler gefunden? Smile
Ou ok ich verstehe was du meinst.

Da ich aber kein Blatt habe mit den Werten findet er klar nichts.
Das bedeutet, ich muss b.adress auf die UserForm beziehen.
Genauer auf den eingegebenen Wert in der TextBox auf der UserForm.

Habe es mal mit:

With TextBox
    Set rngObject = .TextBox("TextBox1")
    Set b = rngObject.Find("TextBox1")
   
    If Not b Is Nothing Then
        strFirst = b.Address
            Do
            ListBox1.AddItem .TextBox(b.TextBox1)
            lngAnzahl = ListBox1.ListCount
            ListBox1.List(lngAnzahl - 1, 1) = .TextBox1(b.TextBox1, 1)
            ListBox1.List(lngAnzahl - 1, 2) = .TextBox1(b.TextBox1, 2)
            ListBox1.List(lngAnzahl - 1, 3) = .TextBox1(b.TextBox1, 8)
            ListBox1.List(lngAnzahl - 1, 4) = .TextBox1(b.TextBox1, 3)
            ListBox1.List(lngAnzahl - 1, 5) = .TextBox1(b.TextBox1, 74)
            Set b = rngObject.FindNext(b)
            Loop While Not b Is Nothing And b.Address <> strFirst
        End If
   
    End With



Hier bekomme ich jedoch einen Laufzeitfehler.
Ich glaube, das ich mich hier definitiv überfordere  Undecided
Verstehe hier wohl etwas falsch mit dem Befehl rng....

Hast du mir noch einen Tipp?
Werde wohl noch fiel lesen müssen.

Danke dir auf jeden Fall für deine Konstruktiven Hilfestellungen.
Schätze es, das du auf den Lerneffekt setzt  :35:
Hallöchen,

Du hast doch in Deiner textbox keine Zellen ...

Also nicht
ListBox1.AddItem .Text(b.Row, 1)

sondern
ListBox1.AddItem .Cells(b.Row, 1)
Meinst du so?:

With TextBox1
  With TextBox1
    Set b = rngBereich.Find(TextBox1)
 
    If Not b Is Nothing Then
        strFirst = b.Address
            Do
            ListBox1.AddItem .Cells(b.Row, 1)
            lngAnzahl = ListBox1.ListCount
            ListBox1.List(lngAnzahl - 1, 1) = .Cells(b.Row, 1)
            ListBox1.List(lngAnzahl - 1, 2) = .Cells(b.Row, 2)
            ListBox1.List(lngAnzahl - 1, 3) = .Cells(b.Row, 8)
            ListBox1.List(lngAnzahl - 1, 4) = .Cells(b.Row, 3)
            ListBox1.List(lngAnzahl - 1, 5) = .Cells(b.Row, 74)
            Set b = rngBereich.FindNext(b)
            Loop While Not b Is Nothing And b.Address <> strFirst
        End If
 
    End With


Da erhalte ich einen Fehler das er .Cells nicht kennt (Object wird nicht gefunden)

Ich glaube jedoch habe ich noch ein ganz anderes Problem

rngBereich deklariere ich am Anfang mit .Columns("A:A") = a
Was ja für die erste Eintragung korrekt ist.

In der Zweiten Spalte müsste ich jedoch den rngBereich für b.Address auf die TextBox1 umdeklarieren :16:

Ich glaube ich fang noch mal von Vorne an und versuche einmal nur den Wert aus der TextBox in die ListBox zu übertragen.
Da ich ja den Wert in der TextBox immer wieder ändere von hand, bräuchte ich ja keine Schlaufe. 
Ich muss Ihm nur sagen, das er den Wert aus der TextBox nehmen soll und diesen in der 2ten Spalte zugehörig zur ComboBox abfrage eintragen soll.

Habe echt gerade einen Knoten.
Hallöchen,

auch wieder wie bei a

Statt
With TextBox1

Dann
With Sheets("Material Liste")

Der Inhalt einer Textbox ist kein Bereich, sondern Text.

Wenn der text der Name einer Spalte ist musst Du das mit der Spalte verarbeiten (Columns) und nicht direkt als rng... Allerdings wäre dann die Frage, wo steht, was Du suchst ...
Danke für den Hinweis mit der TextBox. Werde mir dies mal separat noch anschauen.

Durch deinen Hinweis habe ich das Problem jetzt anders Gelöst.
Ich habe aus der TextBox eine weiter ComboBox gemacht und ein Hilfsblatt erstellt mit den Werten 1-1000.

Der Code sieht nun so aus:
Private Sub CommandButton1_Click()

  Dim a As Range
  Dim b As Range
  Dim c As Range
    Dim rngBereich As Range
    Dim lngAnzahl As Long
    Dim strFirst As String
   
   
       
    With Sheets("Material Liste")
        Set rngBereich = .Columns("A:A")
        Set a = rngBereich.Find(ComboBox1, LookIn:=xlValues, lookat:=xlWhole)
       
       
    If Not a Is Nothing Then
        strFirst = a.Address
            Do
            ListBox1.AddItem .Cells(a.Row, 1)
            lngAnzahl = ListBox1.ListCount
            ListBox1.List(lngAnzahl - 1, 1) = .Cells(a.Row, 1)
            ListBox1.List(lngAnzahl - 1, 2) = .Cells(a.Row, 2)
            ListBox1.List(lngAnzahl - 1, 3) = .Cells(a.Row, 3)
            ListBox1.List(lngAnzahl - 1, 4) = .Cells(a.Row, 4)
            ListBox1.List(lngAnzahl - 1, 5) = .Cells(a.Row, 5)
            Set a = rngBereich.FindNext(a)
            Loop While Not a Is Nothing And a.Address <> strFirst
        End If
       

    End With
       
  With Sheets("Hilfe")
        Set rngBereich = .Columns("A:A")
        Set b = rngBereich.Find(ComboBox2, LookIn:=xlValues, lookat:=xlWhole)
       
       
    If Not b Is Nothing Then
        strFirst = b.Address
            Do
            ListBox1.AddItem .Cells(b.Row, 1)
            lngAnzahl = ListBox1.ListCount
            ListBox1.List(lngAnzahl - 2, 1) = .Cells(b.Row, 1)
            ListBox1.List(lngAnzahl - 2, 2) = .Cells(b.Row, 2)
            ListBox1.List(lngAnzahl - 2, 3) = .Cells(b.Row, 3)
            ListBox1.List(lngAnzahl - 2, 4) = .Cells(b.Row, 4)
            ListBox1.List(lngAnzahl - 2, 5) = .Cells(b.Row, 5)
            Set b = rngBereich.FindNext(b)
            Loop While Not b Is Nothing And b.Address <> strFirst
        End If
       
    End With
   
       
    With Sheets("Hilfe")
        Set rngBereich = .Columns("A:A")
        Set c = rngBereich.Find(ComboBox3, LookIn:=xlValues, lookat:=xlWhole)
       
       
    If Not c Is Nothing Then
        strFirst = c.Address
            Do
            ListBox1.AddItem .Cells(c.Row, 1)
            lngAnzahl = ListBox1.ListCount
            ListBox1.List(lngAnzahl - 3, 1) = .Cells(c.Row, 1)
            ListBox1.List(lngAnzahl - 3, 2) = .Cells(c.Row, 2)
            ListBox1.List(lngAnzahl - 3, 3) = .Cells(c.Row, 3)
            ListBox1.List(lngAnzahl - 3, 4) = .Cells(c.Row, 4)
            ListBox1.List(lngAnzahl - 3, 5) = .Cells(c.Row, 5)
            Set c = rngBereich.FindNext©
            Loop While Not c Is Nothing And c.Address <> strFirst
        End If

    End With 

End Sub


Bereich c wird dann noch knifflig. Da ich jedoch zuerst mal eine 3 Spaltige ListBox hinbekommen will, habe ich nun einfach Combox2 kopiert.

Ausgelesen wird nun alles. Jedoch noch nicht so wie ich es haben möchte.

Ich erhalte nun 3 Zeilen

Die erste Zeile enthält a und c
Zweite Zeile enthält b
Dritte Zeile enthält c

Hier ein Bild:
[attachment=35014]

Ich möchte jedoch eine Zeile mit a,b und c.

Danke für die Hilfe. Du kannst auch sagen wenn du es Hoffnungslos findest.
Dann gehe ich mal in eine Schule Wink
Irgend wie glaube ich das ich dass viel zu kompliziert machen will.

Habe jetzt die Strategie gewechselt. Dies funktioniert so weit nicht schlecht.

Habe nun folgenden Code:

Private Sub CommandButton1_Click()

Dim z As Integer


z = ThisWorkbook.Sheets("ListBoxDaten").Range("Tabelle6").End(xlDown).Row + 1
If z < 1 Then z = 1

ThisWorkbook.Sheets("ListBoxDaten").Cells(z, 1) = ComboBox1.Value
ThisWorkbook.Sheets("ListBoxDaten").Cells(z, 2) = ComboBox2.Value
ThisWorkbook.Sheets("ListBoxDaten").Cells(z, 3) = ComboBox3.Value

  ListBox1.List = Sheets("ListBoxDaten").Range("Tabelle6").Value

End Sub


Somit schreibe ich die Daten in eine Tabelle und gebe den Wert aus der Tabelle in der ListBox wieder.

Danke auf jeden Fall für die Anregungen
Seiten: 1 2 3