search procedure to addition my code to sum amounts for column in listbox

Mussa

Board Regular
Joined
Jul 12, 2021
Messages
245
Office Version
  1. 2019
  2. 2010
Hi
I search for procedure to add to end my code when populate the data in listbox should sum the whole amounts for the column 11 in listbox and show the amounts TOTAL in textbox1 with exclude from summing the lastrow in listbox will contain TOTAL word in first column in listbox .
thanks
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
To sum Column 11, try...

VBA Code:
    With Application
        Me.TextBox1.Value = .Sum(.Index(Me.ListBox1.List, 0, 11))
    End With

If the last row contains the total amount, you can exclude it from the sum as follows...

VBA Code:
    Dim i As Long
    Dim total As Double
    
    total = 0#
    With Me.ListBox1
        For i = 0 To .ListCount - 2
            total = total + .List(i, 10)
        Next i
    End With
    
    Me.TextBox1.Value = total

If the first row contains headers, you can also exclude it as follows...

VBA Code:
    Dim i As Long
    Dim total As Double
    
    total = 0#
    With Me.ListBox1
        For i = 1 To .ListCount - 2
            total = total + .List(i, 10)
        Next i
    End With
    
    Me.TextBox1.Value = total

Hope this helps!
 
Upvote 0
HI,
all of your suggestion causes mismatch error in theses lines
VBA Code:
        Me.TextBox1.Value = .Sum(.Index(Me.ListBox1.List, 0, 11))

VBA Code:
            total = total + .List(i, 10)
by the way the column 11 will contains formatting number like this #,##0.00 .
I no know if this is the main reason to show error !:rolleyes:
 
Upvote 0
Can you post the code that loads the ListBox with the data?
 
Upvote 0
here is the code
VBA Code:
Private Sub UserForm_Initialize()
    Dim lindex&
    Dim rngDB As Range, rng As Range
    Dim i, myFormat(1) As String
    Dim sWidth As String
    Dim vR() As Variant
    Dim n As Integer
    Dim myMax As Single
   
    Dim SH As Worksheet
    ' populate ComboBox1 list eith sheet names
    For Each SH In ThisWorkbook.Worksheets
        ComboBox1.AddItem SH.Name
    Next SH
   
   
    Set rngDB = Range("A1:J20")
    For Each rng In rngDB
        n = n + 1
        ReDim Preserve vR(1 To n)
        vR(n) = rng.EntireColumn.Width
    Next rng
    myMax = WorksheetFunction.Max(vR)
    For i = 1 To n
        vR(i) = myMax
    Next i
    With Sheets("purchase").Cells(1).CurrentRegion
        myFormat(0) = .Cells(2, 8).NumberFormatLocal
        myFormat(1) = .Cells(2, 9).NumberFormatLocal
        Set rng = .Offset(1).Resize(.Rows.Count - 1)
        Arr = .Cells(1).CurrentRegion '.Value
    End With

    sWidth = Join(vR, ";")
    'Debug.Print sWidth
    With ListBox1
        .ColumnCount = 10
        .ColumnWidths = sWidth '<~~ 63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63
        .List = rng.Value
        .BorderStyle = fmBorderStyleSingle
        For lindex = 0 To .ListCount - 1
            .List(lindex, 0) = (Format((.List(lindex, 0)), "dd/mm/yyyy"))   ' BL = dates
                      '  .List(lindex, 0) = lindex + 1

            .List(lindex, 7) = Format$(.List(lindex, 7), myFormat(0))
            .List(lindex, 8) = Format$(.List(lindex, 8), myFormat(1))
            .List(lindex, 9) = Format$(.List(lindex, 9), myFormat(1))
        Next
      
        Arr = .List
        '<--- this line
    End With
    ' Dim i As Long
    Dim total As Double
   
    total = 0#
    With Me.ListBox1
        For i = 0 To .ListCount - 2
            total = total + .List(i, 9)
        Next i
    End With
   
    Me.TextBox1.Value = total
End Sub
and the data
SSR v0 a modified v0 b.xlsm
ABCDEFGHIJ
1DATE CLIENT NOINVOICE NOID-CCBB-RTT-YOR-GGQTYPRICETOTAL
201/01/2022CC2-TRB-1INV-TRGG-1IDTR-100FFDOOD-1PACK-2TTR60.00$120.00$7,200.00
302/01/2022CC2-TRB-1INV-TRGG-1IDTR-101FFDOOD-2PACK-3BBR10.00$130.00$1,300.00
403/01/2022CC2-TRB-1INV-TRGG-1IDTR-102FFDOOD-3PACK-4LLR5.00$100.00$500.00
504/01/2022CC2-TRB-1INV-TRGG-1IDTR-103FFDOOD-4PACK-5MMR10.00$289.00$2,890.00
605/01/2022CC2-TRB-2INV-TRGG-2IDTR-104FFDOOD-5PACK-6ASDR5.00$140.00$700.00
706/01/2022CC2-TRB-2INV-TRGG-2IDTR-100FFDOOD-1PACK-2TTR30.00$100.00$3,000.00
807/01/2022CC2-TRB-2INV-TRGG-2IDTR-101FFDOOD-2PACK-3BBR50.00$120.00$6,000.00
908/01/2022CC2-TRB-2INV-TRGG-2IDTR-107FFDOOD-6PACK-7FFG12.00$200.00$2,400.00
1009/01/2022CC2-TRB-2INV-TRGG-2IDTR-108FFDOOD-7PACK-8GGF13.00$100.00$1,300.00
1110/01/2022CC2-TRB-3INV-TRGG-3IDTR-109FFDOOD-8PACK-9GGF20.00$110.00$2,200.00
1211/01/2022CC2-TRB-4INV-TRGG-4IDTR-110FFDOOD-9PACK-10GGF30.00$120.00$3,600.00
1312/01/2022CC2-TRB-5INV-TRGG-5IDTR-111FFDOOD-10PACK-11GGF40.00$130.00$5,200.00
1413/01/2022CC2-TRB-6INV-TRGG-6IDTR-112FFDOOD-11PACK-12GGF50.00$140.00$7,000.00
1514/01/2022CC2-TRB-7INV-TRGG-7IDTR-113FFDOOD-12PACK-13GGF60.00$150.00$9,000.00
PURCHASE
Cell Formulas
RangeFormula
J2:J15J2=H2*I2

with change to column 10.
 
Upvote 0
The Format function uses different formatting strings than the NumberFormat and NumberFormatLocal properties. Have a look at the following article...


So I would suggest that you simply hard-code the correct formatting string for the Format function.
 
Upvote 0

Forum statistics

Threads
1,215,566
Messages
6,125,589
Members
449,237
Latest member
Chase S

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