Sum values and divided into textboxes on form based on showing data in listbox

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
713
Office Version
  1. 2019
Hello ,
in TB2 will search for BRAND after select optionbutton1,optionutton2 based on sheet name .
so what I want to sum column 5 and put in TB3 , TB4 = sum column 7 and divide on sum column 5 (80,150.00/40.00=2,003.75), TB5= TB3*TB4
and populate column 3 and put in TB1

this is what I have
V1.JPG

this is what I want
Y1.JPG


this code is search BRAND when write in TB2

VBA Code:
Option Explicit
Dim a As Variant


Sub FilterData()
  Dim txt1 As String
  Dim i As Long, j As Long, k As Long
  ListBox1.Clear
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  For i = 1 To UBound(a)
    If TextBox2 = "" Then txt1 = a(i, 4) Else txt1 = TextBox2
   
    If LCase(a(i, 4)) Like LCase(txt1) & "*" Then
   
     
      k = k + 1
      For j = 1 To 7
      If j = 1 Then 'Change the 1 in this line for the column number where you have the date
          b(k, j) = Format(a(i, j), "dd/mm/yyyy")
        Else
          b(k, j) = a(i, j)
        End If
       
      Next
    End If
  Next
  If k > 0 Then ListBox1.List = b
  With ListBox1
  .ColumnWidths = "80;80;80;180;100;80;80"
 
        For i = 0 To .ListCount - 1
            .List(i, 4) = Format(.List(i, 4), "#,##0.00")
            .List(i, 5) = Format(.List(i, 5), "#,##0.00")
            .List(i, 6) = Format(.List(i, 6), "#,##0.00")
        Next i
   
  End With
 
 
End Sub


Private Sub OptionButton1_Click()
    If OptionButton1.Value = True And OptionButton2.Value = False Then sheet5.Select
    Call ChangeSheet
    Call FilterData
End Sub

Private Sub OptionButton2_Click()
    If OptionButton2.Value = True And OptionButton1.Value = False Then sheet6.Select
    Call ChangeSheet
    Call FilterData
End Sub

Private Sub TextBox2_Change()
Call FilterData
End Sub




Private Sub ChangeSheet()
    With ActiveSheet
        a = .Range("A2:G" & .Range("D" & Rows.Count).End(3).Row).Value
    End With
End Sub
I hope anybody can help.
 
For populating TB3-4 and 5, try with these added lines:

VBA Code:
Sub FilterData()
  Dim Tot4 As Double, Tot5 As Double, Tot6 As Double        '++
  Dim txt1 As String
'...
'...
  With Listbox1
  .ColumnWidths = "80;80;80;180;100;80;80"
        For i = 0 To .ListCount - 1
            Tot4 = Tot4 + .List(i, 4)                       '++
            .List(i, 4) = Format(.List(i, 4), "#,##0.00")
            Tot5 = Tot5 + .List(i, 5)                       '++
            .List(i, 5) = Format(.List(i, 5), "#,##0.00")
            Tot6 = Tot6 + .List(i, 6)                       '++
            .List(i, 6) = Format(.List(i, 6), "#,##0.00")
        Next i
  End With
  Me.TextBox3 = Tot4                        '++
  Me.TextBox4 = Tot6 / Tot5                 '++
End Sub
The added lines are marked ++

As fa as TB1 is concerned, I don't seem you explained which is the logic to follow; my best guess is that you want in TB1 the content of third column; so try adding this line:
VBA Code:
        If j = 1 Then 'Change the 1 in this line for the column number where you have the date
          b(k, j) = Format(a(i, j), "dd/mm/yyyy")
        Else
          b(k, j) = a(i, j)
          If k = 1 And j = 3 Then Me.TextBox1.Value = a(i, j)       '+++
        End If
Try...
 
Upvote 0
thanks Antony for your trying
first it doesn't seem to be correct for price as in picture
QW1.PNG




second I would get rid of error overflow in this line
VBA Code:
 Me.TextBox4 = Tot6 / Tot5                 '++
this occurs when BRAND is error in writing.
thanks again.
 
Upvote 0
Is that TextBox4, thus populated via Me.TextBox4 = Tot6 / Tot5?
Since Tot5 and Tot6 are dimmed to be a double I expect that Tot6 / Tot5 (and thus the TextBox) should be a number, I cannot decode what is written in that box.
Considering also the Overflow error, try replacing Me.TextBox4 = Tot6 / Tot5 with
VBA Code:
If Tot5 > 0 then Me.TextBox4.Value = Format(Tot6 / Tot5, "0.00")


If this doesn't fix the error then we shall investigate
 

Attachments

  • Screenshot 2025-02-23 190704.png
    Screenshot 2025-02-23 190704.png
    96.1 KB · Views: 1
Upvote 0

Forum statistics

Threads
1,226,864
Messages
6,193,418
Members
453,798
Latest member
jasonsd

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