code summing into textboxes based on combobox,optionbuttons doesn't give right values

tubrak

Board Regular
Joined
May 30, 2021
Messages
216
Office Version
  1. 2019
Platform
  1. Windows
hi
I would populate total of textbox1 (DEBIT) and textbox2 (CREDIT) and shows the value into textbox3 (NET) after subtract DEBIT-CREDIT
so if I select the name from combobox1 then should populate TOTAL values into textbox1,2 which is relating the name , if I select the name from combobox1 and select option button (CASH) then should sum the values just the credit in textbox2 based on column G, if if I select the name from combobox1 and select option button (BANK) then should sum the values just the credit in textbox2 based column G ,if I select the name from combobox1 and select option button (BANK,CASH) then should sum the values together just the credit in textbox2 based on column G ,if I select the name from combobox1 and select option button (not paid) then should sum the values just the debit in textbox3 based on column F .
note: the combobox1 depends on column C and the optionbuttons depends on column E , the textbox1 (debit) based on column F ,the textbox2 (CREDIT ) based on column G .
I try collecting some ideas from the internet and this is what I got but unfortunately it gives wrong values by summing all of values in columns F,G with ignore selected optionbutton,combobox . so I hope from the experts fix this problem.
VBA Code:
Private Sub ComboBox1_Change()
   Dim sh As Worksheet, f As Range
  If ComboBox1.Value = "" Or ComboBox1.ListIndex = -1 Then Exit Sub
  Set sh = Sheets("DATA")
  Set f = sh.Range("C:C").Find(ComboBox1.Value, , xlValues, xlWhole)
  If Not f Is Nothing Then
  TextBox1.Value = Application.Sum(Worksheets("DATA").Range("f:f"))
  TextBox2.Value = Application.Sum(Worksheets("DATA").Range("g:g"))
  End If
 
End Sub

Private Sub OptionButton1_Click()
If OptionButton1.Value = True And ComboBox1.Value <> "" Then
Set f = sh.Range("C:C").Find(ComboBox1.Value, , xlValues, xlWhole)
If Not f Is Nothing Then
TextBox2.Value = Application.Sum(Worksheets("DATA").Range("g:g"))
End If
End If
End Sub

Private Sub OptionButton2_Click()
If OptionButton2.Value = True And ComboBox1.Value <> "" Then
Set f = sh.Range("C:C").Find(ComboBox1.Value, , xlValues, xlWhole)
If Not f Is Nothing Then
TextBox2.Value = Application.Sum(Worksheets("DATA").Range("g:g"))
End If
End If
End Sub

Private Sub OptionButton3_Click()
If OptionButton3.Value = True And ComboBox1.Value <> "" Then
Set f = sh.Range("C:C").Find(ComboBox1.Value, , xlValues, xlWhole)
If Not f Is Nothing Then
TextBox2.Value = Application.Sum(Worksheets("DATA").Range("g:g"))
End If
End If
End Sub

Private Sub OptionButton4_Click()
If OptionButton4.Value = True And ComboBox1.Value <> "" Then
Set f = sh.Range("C:C").Find(ComboBox1.Value, , xlValues, xlWhole)
If Not f Is Nothing Then
TextBox2.Value = Application.Sum(Worksheets("DATA").Range("f:f"))
End If
End If
End Sub

Private Sub TextBox2_Change()
TextBox3.Value = Val(TextBox1.Value) - Val(TextBox2.Value)
End Sub
this is the data in sheet
cs1.xlsm
ABCDEFG
1ITEMDATENAMEINVOICE NOCASEDEBITCREDIT
2101/01/2021TUBRIKOAS-12NOT PAID2000-
3202/01/2021TUBRIKOAS-12CASH-100.00
4305/03/2021TUBRIKOAS-12BANK-1,500.00
5405/05/2021TUBRIKOAS-13CASH1000500.00
6505/05/2021TRIBOKOAS-14BANK20001,500.00
7602/02/2021TRIBOKOAS-15BANK30003,500.00
8704/02/2021TRIBOKOAS-14BANK-500.00
9804/05/2021TRIBOKOAS-15BANK-1,000.00
10904/04/2021TARKONOAS-16NOT PAID2500-
111004/04/2021TARKONOAS-17NOT PAID1000-
121104/04/2021TARKONOAS-16CASH500.00
data


and the userform
1.PNG

I hope somebody help
 
try this update

VBA Code:
Sub SumRange(ByVal Form As Object, ByVal objOptionButton As Object)
    Dim Index           As Long
    Dim sh              As Worksheet
    Dim i               As Long
    Dim strName         As String, strCase As String
    Dim SumDebit        As Double, SumCredit As Double
    
    Set sh = ThisWorkbook.Worksheets("DATA")
    
    Index = Val(Right(objOptionButton.Name, 1))
    
    strName = Trim(Form.ComboBox1.Text)
    
    If Index = 3 Then
        For i = 1 To 2
            strCase = Choose(i, "CASH", "BANK")
            
            SumDebit = SumDebit + Application.SumIfs(sh.Columns(6), sh.Columns(3), strName, sh.Columns(5), strCase)
            SumCredit = SumCredit + Application.SumIfs(sh.Columns(7), sh.Columns(3), strName, sh.Columns(5), strCase)
        Next i
        
    Else
        strCase = Choose(Index, "CASH", "BANK", "BANK & CASH", "NOT PAID")
        
        SumDebit = Application.SumIfs(sh.Columns(6), sh.Columns(3), strName, sh.Columns(5), strCase)
        SumCredit = Application.SumIfs(sh.Columns(7), sh.Columns(3), strName, sh.Columns(5), strCase)
    End If
    
    
    'Debit
    Form.TextBox1.Value = SumDebit
    'Credit
    Form.TextBox2.Value = SumCredit
    
    With Form.TextBox3
        .ForeColor = IIf(Val(.Value) > 0, rgbGreen, IIf(Val(.Value) < 0, rgbRed, rgbBlack))
    End With
    
End Sub

Dave
 
Upvote 0
Solution

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
magnificent ! all things are great!
many thanks for your time and assistance :)
 
Upvote 0

Forum statistics

Threads
1,215,450
Messages
6,124,912
Members
449,195
Latest member
Stevenciu

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