Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

ListBox in a form: Load data at the same when the form is loaded

0

HI there,

Is there a way to load all the data in a listbox directly as soon as the form opens. Currently once the form is loaded , only then i click another Command Button to load the listbox. Finally it copies my selections from the listbox into a sheet. ( see the three subroutines below). Thanks

Private Sub Cmb_PriceLists_Click()
    Form_ListServices.Show
End Sub
Private Sub Cmb_ShowList_Click()
    ListBox1.ColumnCount = 3
    'ListBox1.RowSource = ("Rng_Services")
    ListBox1.List = Range("Rng_Services").Value
End Sub
Private Sub CommandButton1_Click()
Dim i As Long
Dim x As Integer

For i = 0 To Me.ListBox1.ListCount - 1
    If Me.ListBox1.Selected(i) = True And Me.ListBox1.List(i, 1) <> "" Then
    Sheets("Invoices").Range("A1").End(xlDown).Offset(1, 0) = Me.ListBox1.List(i, 1)
        For x = 0 To 2
        Sheets("Invoices").Range("A1").End(xlDown).Offset(0, x) = Me.ListBox1.List(i, x)
        Next x
    End If
Next i
End Sub
Answer
Discuss

Discussion

hi there
the code stopped working at the point where the .list ( shoudl have loaded into the listbox automaticaaly). I coudl send you/attach  a 'printscreen' which will illustrate it more clearly . How do i go about attaching a .png file ? 
martinARG (rep: 4) Jun 22, '20 at 12:56 pm
Add to Discussion

Answers

0
Selected Answer

You are absolutely right! What you are asking is standard fare. But you should bear in mind that Microsoft will always teach the methods they perceive to be easier to learn rather than the correct way. Therefore they teach a lot of rubbish. One of those things is how to call a form.

The correct way is to first create the form as a variable, then show and hide it as many times as you like, and only unload it when you're done. This scenario gives you access to the form and all its controls both before showing it and after. This would (and does) enable you to load your ListBox before you show it and transfer values from it to a worksheet in the time after hiding and before unloading.

The code below demonstrates this process.

Private Sub Cmb_PriceLists_Click()
    ' TXL 054

    Dim FrmService      As Form_ListServices
    Dim Ws              As Worksheet
    Dim R               As Long                 ' row number
    Dim x               As Long
    Dim i               As Integer              ' array index

    ' this command creates the form
    '   the Initialize event fires
    Set FrmService = New Form_ListServices

        ' the form now exists with all its controls
        ' and you can access any one of them
    With FrmService
        With .ListBox1
            .ColumnCount = 3
            .List = Range("Rng_Services").Value
        End With

        .Show vbModal                   ' the Activate event occurs
        ' from this point forward the modal form takes control

        ' =====

        ' Code resumes HERE when a command in the form Hides it.
        '   Make sure not to Unload the form from within the form.

        ' The form is still fully loaded but again invisible
        ' you can access all controls and transfer data out

        ' a 'Sheet' isn't the same as a 'Worksheet'
        Set Ws = Worksheets("Invoices")
        With Ws
            ' this code defines the last cell in the sheet and looks
            ' for the first used cell going UP
            R = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
            ' your code defines A as a start and looks for the first blank
            ' cell going dow, presuming the one above it to be the last used.
            ' It will find a blank cell between existing data.
        End With

        With .ListBox1
            ' not tested:-
            For i = 0 To .ListCount - 1
                If (.Selected(i) = True) And (.List(i, 1) <> "") Then
                    Ws.Cells(R, "A").Value = .List(i, 1)
                    For x = 0 To 2
                        Ws.Cells(R + x + 1, "A").Value = .List(i, x)
                    Next x
                End If
            Next i
        End With
    End With

    Unload FrmService                   ' now unload the form
    Set FrmService = Nothing            ' and destroy it
End Sub
Discuss

Discussion

(Oops, sorry i copied this reply onto the wrong spot, hope is in the correct place now).
hi there
the code stopped working at the point where the .list ( shoudl have loaded into the listbox automaticaaly). I coudl send you/attach  a 'printscreen' which will illustrate it more clearly . How do i go about attaching a .png file ? 
martinARG (rep: 4) Jun 22, '20 at 12:57 pm
Martin, My solution was to show you how to structure the procedure that calls the user form. There was no mention of a PNG file. Please ask a new question. Be sure to attach your workbook, with your new code in it. Perhaps your question then is how to upload a PNG file into a user form.
Variatus (rep: 4889) Jun 22, '20 at 8:50 pm
HI there. Many thanks once more. The only reason why i menitoned the PNG file was in case you wanted to see a screen capture showing where your suggested code went wrong. So in essence, the problem is still that the code stopped working at the " .list" point ( i.e where the list should have been loaded automatically into the listbox). Any further suggestions , please ?
martinARG (rep: 4) Jun 25, '20 at 6:01 pm
Ask a question about how to load a list box. This question is about the timing and you gave the impression that you know how to load. If the code I included in my demonstrtation of timing doesn't work for you use the code you already have in its place. Just replace it with a call of your existing function.
Variatus (rep: 4889) Jun 25, '20 at 8:02 pm
Thanks very much
regards
martinARG (rep: 4) Jun 26, '20 at 3:22 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login