vba sumif into textbox on userform

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,507
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi, everyone
i have sheet contains data begins a3:h10000 i try writing code about function sumif to show me the total values into textbox2 , textbox3 it gives me total=0 of course it also contains the userform combobox 1 when i choose additem in combobox1 it suppose gives me the total in textbox2,3 the total in textbox2 it depends on 2 columns d= values and h= name textbox3 it deoends on 2 columns e= values h= name
this is my code
VBA Code:
Private Sub ComboBox1_Change()
Dim ws As Worksheet
Dim Rng As Range
Dim Sel
Set ws = Sheets("sheet2")
Sel = Me.ComboBox1.Value
If Sel <> "" Then
 Set Rng = ws.Columns(2).Find(Sel, lookat:=xlWhole)
 If Not Rng Is Nothing Then
 Me.TextBox1.Value = ws.Cells(Rng.Row, "c")
Me.TextBox2.Value = Application.WorksheetFunction.SumIf(Range("d:d"), Me.ComboBox1, Range("g:g"))
Me.TextBox3.Value = Application.WorksheetFunction.SumIf(Range("e:e"), Me.ComboBox1, Range("g:g"))
Me.TextBox4 = Val(Me.TextBox2.Value) - Val(Me.TextBox3.Value)
 
End If
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
thanks but i would code not formula i hope help me
?

I just want to see a sample of your data.

For example:

Book1
ABCDEFG
1ABCDEFG
2A2D2C2D2E2F21
3A3B3C3D3E3F32
4A4D2C4D2E4F43
5A5B5C5D5E5F54
6A6D2C6D2E6F65
7A7B7C7D7E7F76
8A8D2C8D2E8F87
sheet2


1579808631180.png


According to your code:
You are looking for the combobox1 data in column "B"
Set Rng = ws.Columns(2).Find(Sel, lookat:=xlWhole)

If it exists then you add the values of column G if the data in column D is equal to combobox1.

In my example. In cell B2 I have the data "D2".
In combobox1 I have "D2". Then the first condition is met.
Then add the values 1 + 3 + 5 + 7 = 16

The code works, but I don't know what data you have.
 
Upvote 0
Try this:

Updated code:
- Columns(7)
- Missing "End if"
- Missing ws.Range("G:G")
- ws.Range("G:G"), Me.ComboBox1, ws.Range("D:D")

VBA Code:
Private Sub ComboBox1_Change()
  Dim ws As Worksheet, Rng As Range, Sel As Variant
  Set ws = Sheets("sheet2")
  Sel = Me.ComboBox1.Value
  If Sel <> "" Then
    Set Rng = ws.Columns(7).Find(Sel, lookat:=xlWhole)
    If Not Rng Is Nothing Then
      Me.TextBox1.Value = ws.Cells(Rng.Row, "C")
      Me.TextBox2.Value = WorksheetFunction.SumIf(ws.Range("G:G"), Me.ComboBox1, ws.Range("D:D"))
      Me.TextBox3.Value = WorksheetFunction.SumIf(ws.Range("G:G"), Me.ComboBox1, ws.Range("E:E"))
      Me.TextBox4 = Val(Me.TextBox2.Value) - Val(Me.TextBox3.Value)
    End If
  End If
End Sub
 
Upvote 0
i'm s
Try this:

Updated code:
- Columns(7)
- Missing "End if"
- Missing ws.Range("G:G")
- ws.Range("G:G"), Me.ComboBox1, ws.Range("D:D")

VBA Code:
Private Sub ComboBox1_Change()
  Dim ws As Worksheet, Rng As Range, Sel As Variant
  Set ws = Sheets("sheet2")
  Sel = Me.ComboBox1.Value
  If Sel <> "" Then
    Set Rng = ws.Columns(7).Find(Sel, lookat:=xlWhole)
    If Not Rng Is Nothing Then
      Me.TextBox1.Value = ws.Cells(Rng.Row, "C")
      Me.TextBox2.Value = WorksheetFunction.SumIf(ws.Range("G:G"), Me.ComboBox1, ws.Range("D:D"))
      Me.TextBox3.Value = WorksheetFunction.SumIf(ws.Range("G:G"), Me.ComboBox1, ws.Range("E:E"))
      Me.TextBox4 = Val(Me.TextBox2.Value) - Val(Me.TextBox3.Value)
    End If
  End If
End Sub
i'm sorry i forget to inform the code this is relating another sheet to show the names in combobox
VBA Code:
Dim ws As Worksheet
Dim Rng As Range
Dim Sel
Set ws = Sheets("sheet2")
Sel = Me.ComboBox1.Value
If Sel <> "" Then
 Set Rng = ws.Columns(2).Find(Sel, lookat:=xlWhole)
 If Not Rng Is Nothing Then
 Me.TextBox1.Value = ws.Cells(Rng.Row, "c")
 

Attachments

  • 3.JPG
    3.JPG
    18.2 KB · Views: 27
Upvote 0
Then:

VBA Code:
Private Sub ComboBox1_Change()
  Dim ws As Worksheet, Rng As Range, Sel As Variant
  Set ws = Sheets("sheet2")
  Sel = Me.ComboBox1.Value
  If Sel <> "" Then
    Set Rng = ws.Columns(2).Find(Sel, lookat:=xlWhole)
    If Not Rng Is Nothing Then
      Me.TextBox1.Value = ws.Cells(Rng.Row, "C")
      Me.TextBox2.Value = WorksheetFunction.SumIf(Range("G:G"), Me.ComboBox1, Range("D:D"))
      Me.TextBox3.Value = WorksheetFunction.SumIf(Range("G:G"), Me.ComboBox1, Range("E:E"))
      Me.TextBox4 = Val(Me.TextBox2.Value) - Val(Me.TextBox3.Value)
    End If
  End If
End Sub
 
Upvote 0
Then:

VBA Code:
Private Sub ComboBox1_Change()
  Dim ws As Worksheet, Rng As Range, Sel As Variant
  Set ws = Sheets("sheet2")
  Sel = Me.ComboBox1.Value
  If Sel <> "" Then
    Set Rng = ws.Columns(2).Find(Sel, lookat:=xlWhole)
    If Not Rng Is Nothing Then
      Me.TextBox1.Value = ws.Cells(Rng.Row, "C")
      Me.TextBox2.Value = WorksheetFunction.SumIf(Range("G:G"), Me.ComboBox1, Range("D:D"))
      Me.TextBox3.Value = WorksheetFunction.SumIf(Range("G:G"), Me.ComboBox1, Range("E:E"))
      Me.TextBox4 = Val(Me.TextBox2.Value) - Val(Me.TextBox3.Value)
    End If
  End If
End Sub
thanks so much it's perfect
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,222
Members
453,024
Latest member
Wingit77

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