Calculate listbox column values

asyamonique

Well-known Member
Joined
Jan 29, 2008
Messages
1,286
Office Version
  1. 2013
Platform
  1. Windows
Code:
Private Sub CommandButton1_Click()



Dim r As Long, Ac As Long, c As Long
  If ComboBox1.Value <> -1 And ComboBox2.Value <> -1 Then
        ListBox1.Clear
        With Worksheets("reports").Range("A1:A1000")
           ReDim Ray(1 To .Count, 1 To 6)
            For r = 1 To .Rows.Count
               With ListBox1
                    .ColumnCount = 6
                    .ColumnWidths = "0;0;80;350;100;100"
                End With
                If ComboBox1.Value = .Cells(r, 1).Value And ComboBox2.Value = .Cells(r, 2).Value Then
                   c = c + 1
                   For Ac = 1 To 6
                        Ray(c, Ac) = .Cells(r, Ac)
                   Next Ac
                End If
            Next r
        End With
End If
ListBox1.list = Ray




End Sub


Hello,
Is it possible add additional code to given above code to show on textbox1 sum of listbox1 5th column vaues?
Many Thanks
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
try updates to your code shown in red & see if helps you

Rich (BB code):
Private Sub CommandButton1_Click()
    Dim r As Long, Ac As Long, c As Long
    Dim SumValue As Double

    If ComboBox1.Value <> -1 And ComboBox2.Value <> -1 Then
        ListBox1.Clear
        With Worksheets("reports").Range("A1:A1000")
           ReDim Ray(1 To .Count, 1 To 6)
            For r = 1 To .Rows.Count
               With ListBox1
                    .ColumnCount = 6
                    .ColumnWidths = "0;0;80;350;100;100"
                End With
                If ComboBox1.Value = .Cells(r, 1).Value And ComboBox2.Value = .Cells(r, 2).Value Then
                   c = c + 1
                   For Ac = 1 To 6
                   If Ac = 5 Then SumValue = SumValue + Val(.Cells(r, Ac).Value)
                        Ray(c, Ac) = .Cells(r, Ac)
                   Next Ac
                End If
            Next r
        End With
End If
ListBox1.List = Ray

Me.TextBox1.Value = SumValue
End Sub

Dave
 
Last edited:
Upvote 0
Another option:-
Code:
ListBox1.List = Ray

With Application
   TextBox1.Value = IIf(c > 0, .Sum(.Index(Ray, Evaluate("Row(1:" & c & ")"), 5)), "")
End With
 
Upvote 0
Thanks a lot...
I did formatted textbox value with below code...
Code:
TextBox1.Text = Format(TextBox1.Text, "###,##")
How can I do the same onto listbox?
I want the numbers will populate deciminal when listbox shows all datas...
Thanks
 
Last edited:
Upvote 0
Thanks a lot...
I did formatted textbox value with below code...
Code:
TextBox1.Text = Format(TextBox1.Text, "###,##")
How can I do the same onto listbox?
I want the numbers will populate deciminal when listbox shows all datas...
Thanks

Hi,
assuming your cells have been formatted in required manner then try this update to your code

Code:
Private Sub CommandButton1_Click()
    Dim r As Long, Ac As Long, c As Long
    Dim SumValue As Double
    Dim rng As Range
    Dim box1 As Object, box2 As Object
    Dim Ray() As Variant
    
    Set box1 = Me.ComboBox1
    Set box2 = Me.ComboBox2
    
    If Not CBool(Len(box1) And Len(box2)) Then Exit Sub
    
    With ThisWorkbook.Worksheets("reports")
        Set rng = .Range(.Range("A2"), .Range("A" & .Rows.Count).End(xlUp))
    End With
    
    With rng
        ReDim Ray(1 To .Count, 1 To 6)
        For r = 1 To .Rows.Count
            If box1.Value = .Cells(r, 1).Value And box2.Value = .Cells(r, 2).Value Then
                c = c + 1
                For Ac = 1 To 6
                    If Ac = 5 Then SumValue = SumValue + Val(.Cells(r, Ac).Value)
                    Ray(c, Ac) = .Cells(r, Ac).Text
                Next Ac
            End If
        Next r
    End With
                
    With ListBox1
        .Clear
        .RowSource = ""
        .ColumnCount = 6
        .ColumnWidths = "0;0;80;350;100;100"
        .List = Ray
    End With
                
    Me.TextBox1.Value = Format(SumValue, "###,##")
End Sub

Dave
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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