Sum of Column in Listbox

ExcelEndeavor

New Member
Joined
Oct 13, 2020
Messages
49
Office Version
  1. 365
Platform
  1. MacOS
I have a listbox "lbxInvoiceList" that lists all invoices. The invoice amount is listed in column 3 of that listbox. I would like the total of column 3 to be shown in textbox "TxtInvoiceTotal". The code I have only works when one of the line items in the listbox is clicked. I would like to automatically display as soon as the userform "RequestForm" opens. Any ideas?


VBA Code:
Private Sub lbxInvoiceList_Change()
    Call UpdateInvoiceTotal
  
End Sub


Code:
Private Sub UpdateInvoiceTotal()
 
    Dim i As Integer
    Dim total As Double
    total = 0
  
    ' Loop through each item in the listbox
    For i = 0 To lbxInvoiceList.ListCount - 1
        ' Sum the values in column 3 (index 2)
        total = total + CDbl(lbxInvoiceList.List(i, 2))
    Next i
  
    ' Populate the total in the textbox
    TxtInvoiceTotal.Value = total
  
End Sub
 
Right click on userform in the objects pane of the vb editor. Choose view code. Select userform from top left drop down of code window, then drop down the events list and pick a suitable event or events to call the code from. If you choose initialize, it will run your sub when the form opens. If data changes the value will be out of sync (same for your list), in which case you'd need another event to call it from. Perhaps double click. Or just go with double click. I might put the dbl click event on the textbox instead of the form itself.
 
Upvote 0
Solution
Thank you - I added an initialize event and called the event in the textbox. I'm not sure how efficient that is, but it works like a champ. Thank you agiain.
 
Upvote 0
Glad I could help & thanks for the recognition. As for efficiency, having the right event is key, but in this case I don't see the need for calling the sub when you could just have its code in the initialize event. As I noted, this event will not fire if you need it to when the form is already open.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top