SUM of Columns based on multiple criteria with User Form

Denny57

Board Regular
Joined
Nov 23, 2015
Messages
235
Office Version
  1. 365
Platform
  1. Windows
I am looking to populate a field in a User Form with the sum of a range of cells based on two criteria.

Criteria 1 = Range to be determined by matching all rows in a column (Column A) in Sheet 1 that contain the same value as a textbox on the user form.
Criteria 2 = SUM of the corresponding rows in a second column (Column B) in Sheet 1 to be returned to a specific TextBox in the User Form.

I am just looking for help with the coding for one record, I can readily create the full code once I have the best way to gather this information

USER FORM FIELD DETAILS
txtPayPeriod = Value to be matched with records in Sheet 1 Column A
CmdPayPeriod = Command Button to action the sub routine (For Information Only)
txtGrossPay = SUM of Records in Sheet 1 Column B where value of Column A = txtPayPeriod value.

Many thanks
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I’ve seen quite a few people use and suggest box.
Other options might be Google Drive or onedrive. You do need the permissions to be set to anyone with link.
Alex

Hopefully this link will allow you access to the file


I am only looking for additional code for the Search By Pay Month Command to return the 6 controls in the bottom right of the User Form.

Dave
 
Upvote 0
Does this format not give you what you need ?
(add or remove criteria as required)

VBA Code:
    txtMonthlyWorkHours = Format(WorksheetFunction.SumIfs( _
                                Columns("J"), _
                                Columns("B"), cboSchedulingType.Value, _
                                Columns("C"), cboLocation.Value, _
                                Columns("D"), cboPayMonthSearch.Value), _
                            "#,##0.00")
 
Upvote 0
Solution
Does this format not give you what you need ?
(add or remove criteria as required)

VBA Code:
    txtMonthlyWorkHours = Format(WorksheetFunction.SumIfs( _
                                Columns("J"), _
                                Columns("B"), cboSchedulingType.Value, _
                                Columns("C"), cboLocation.Value, _
                                Columns("D"), cboPayMonthSearch.Value), _
                            "#,##0.00")
Alex

Thank you, I have eventually managed to code the Command Button so the code works with a couple of exceptions.

VBA Code:
Private Sub cmdPayMonthSearch_Click()

txtMonthlyPayMonth.Value = cboPayMonthSearch.Value
txtMonthlyWorkHours = Format(WorksheetFunction.SumIfs(Columns("J"), Columns("B"), "Work", Columns("D"), cboPayMonthSearch.Value), "#,##0.00")
txtMonthlyWorkEarningsGross = Format(WorksheetFunction.SumIfs(Columns("L"), Columns("B"), "Work", Columns("D"), cboPayMonthSearch.Value), "#,##0.00")
txtMonthlyLeaveHours = Format(WorksheetFunction.SumIfs(Columns("J"), Columns("B"), "Leave", Columns("D"), cboPayMonthSearch.Value), "#,##0.00")
txtMonthlyLeaveEarningsGross = Format(WorksheetFunction.SumIfs(Columns("L"), Columns("B"), "Leave", Columns("D"), cboPayMonthSearch.Value), "#,##0.00")
txtMonthlyEarningsGross = txtMonthlyWorkEarningsGross.Value + txtMonthlyLeaveEarningsGross.Value
End Sub
txtMonthlyWorkEarningsGross and txtMonthlyLeaveEarningsGross are populated with Currency (£) totals. If there a simple cahnge that can show the values as currency.
Also
txtMonthlyEarningsGross = txtMonthlyWorkEarningsGross.Value + txtMonthlyLeaveEarningsGross.Value does not work and should provide the total of these two textboxes

Thank you again for you invaluable help

Dave
 
Upvote 0
Alex

Thank you, I have eventually managed to code the Command Button so the code works with a couple of exceptions.

VBA Code:
Private Sub cmdPayMonthSearch_Click()

txtMonthlyPayMonth.Value = cboPayMonthSearch.Value
txtMonthlyWorkHours = Format(WorksheetFunction.SumIfs(Columns("J"), Columns("B"), "Work", Columns("D"), cboPayMonthSearch.Value), "#,##0.00")
txtMonthlyWorkEarningsGross = Format(WorksheetFunction.SumIfs(Columns("L"), Columns("B"), "Work", Columns("D"), cboPayMonthSearch.Value), "#,##0.00")
txtMonthlyLeaveHours = Format(WorksheetFunction.SumIfs(Columns("J"), Columns("B"), "Leave", Columns("D"), cboPayMonthSearch.Value), "#,##0.00")
txtMonthlyLeaveEarningsGross = Format(WorksheetFunction.SumIfs(Columns("L"), Columns("B"), "Leave", Columns("D"), cboPayMonthSearch.Value), "#,##0.00")
txtMonthlyEarningsGross = txtMonthlyWorkEarningsGross.Value + txtMonthlyLeaveEarningsGross.Value
End Sub
txtMonthlyWorkEarningsGross and txtMonthlyLeaveEarningsGross are populated with Currency (£) totals. If there a simple cahnge that can show the values as currency.
Also
txtMonthlyEarningsGross = txtMonthlyWorkEarningsGross.Value + txtMonthlyLeaveEarningsGross.Value does not work and should provide the total of these two textboxes

Thank you again for you invaluable help

Dave
Alex

Thank you for all invaluableyour help

I have finally managed to generate VBA code that will return Currency values in the required textboxes and also to sum these boxes and return the total in a third textbox.

I will store the code should I require to use similar in the future.

Best Regards

Dave
 
Upvote 0
Glad you were able to figure out how to apply it to your specific situation. 👍
I am in Australia and being in a different time zone can make it challenging to keep the ball rolling ;)
 
Upvote 0

Forum statistics

Threads
1,223,790
Messages
6,174,594
Members
452,574
Latest member
hang_and_bang

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