VBA - Sum issue

gd6noob

Board Regular
Joined
Oct 20, 2017
Messages
170
Office Version
  1. 2016
Platform
  1. Windows
Hi there,

I have a spreadsheet that has a list of hours for each day an employee worked. Each day is listed in a vertical table.
This is the formula Im using for one of the column for the Input tab:
VBA Code:
Range("K2:K" & LastRow4).formula = "=SUMPRODUCT(($A2='Input 2'!$C$2:$C$3000)*(""CAN ""&$K$1&"" Hours Worked""='Input 2'!$L$1:$AZ$1),'Input 2'!$L$2:$AZ$3000)/24"
While this work, it takes a long time to process and I have a lot of columns to run through. Sometimes I have too much data from Input2 tab, that excel crashes on me.
Also the reason Im using wildcards to search is that sometimes the data in Input2 is different. i.e "CAN Regular Hours Worked" can be labelled as ""CAN ON Regular Hours Worked" and so forth, depends on the region where the employees are working in. the number of columns can also differ based on region.

Is there a better method so sum up the total hours worked?


60 Hour Tool V3.0 (Make copy).xlsb
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAI
1Apply DateHistorical DateEmployee IDEmployee NameEmployee LogonEE TypeManagerPay RuleLocationCost CenterLast Hire DateCAN Approved Time OffCAN Attendance Remove 0.5 PtsCAN Attendance Remove 1.0 PtsCAN Attendance Remove 1.5 PtsCAN Banked HolidayCAN Bereavement PaidCAN Bereavement UnpaidCAN Company BusinessCAN ExcusedCAN Meal ExceptionCAN Night Shift PremiumCAN Night Shift Premium OTCAN Non Worked Paid TimeCAN ON HolidayCAN OvertimeCAN Paid SuspensionCAN Paid Time OffCAN Regular Hours WorkedCAN Role Premium 1CAN Role Premium OT1CAN Show Up PayCAN Vacation PaidCAN Voluntary Time OffGBL FC Missed Time Tracking
23/10/2021108444603abcHYYZ4 129910/18/202010.20
33/11/2021108444603abcHYYZ4 129910/18/202010.10
43/7/2021108197952abcHYYZ4 129911/5/202010.1510.15
53/8/2021108197952defHYYZ4 129911/5/202010.1210.12
63/9/2021108197952defHYYZ4 129911/5/202010.1710.17
73/10/2021108197952defHYYZ4 129911/5/20209.570.600.609.57
Input 2



60 Hour Tool V3.0 (Make copy).xlsb
ABCDEFGHIJKLMNOPQRSTUVWX
1Employee IDEmployee NameEmployee LogonPayroll IDPay GroupSchedule GroupEE TypeDeptManager NameAgencyRegularOvertimeDoubletimeHolidayHoliday OvertimeHoliday DoubletimeBanked HolidayPaid Personal TimeVacationTotal Regular HoursTotal OT HoursTotal Paid Time OffTotal Payable HoursTotal Unpaid Time Off
21049812611189
31084194071290
4109394474DB1C07301299040
5100096392NA0C17451299010
6104678271DB0C06451299020
Input
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
As far as I can tell, at the moment your formula is only totaling 1 column from Input 2.
If this can be relied on then SumIfs will work for you and is far more efficient.

I would also suggest you consider using tables. By using tables and Table structure formulas you won't need to go out to column AZ and Row 3000 as the formulas will adjust to the table size.

The below assumes the Input sheet to be the activesheet.

Excel Formula:
=SUMIFS(INDEX('Input 2'!$L$2:$AI$3000,0,MATCH("CAN "&K$1&" Hours Worked",'Input 2'!$L$1:$AZ$1,0)),
'Input 2'!$C$2:$C$3000,$A2)
 
Upvote 0
Solution
Im thinking if this way is more feasible.

Search/Finding the cells I need in row 1 and move the entire column closer....
 
Upvote 0
Not really following what you mean.
Sorry, to clarify.

Use a code to find on sheet2 "Regular Hours Worked", then cut that entire column or until end of data and insert it to column B for example. Then repeat for all the columns I need, then when when I use the sum function, it wont be from A:AL but to A:D or something like that.
 
Upvote 0
Sorry, to clarify.

Use a code to find on sheet2 "Regular Hours Worked", then cut that entire column or until end of data and insert it to column B for example. Then repeat for all the columns I need, then when when I use the sum function, it wont be from A:AL but to A:D or something like that.

Rereading your requirements and comments it still looks like you only want to sum each employee for 1 column, the column being selected based on a lookup value.
If that is the case did the sumifs not work for you ?
You can add a wildcard into the formula in place of CAN.
(I forgot to add the /24 you had in yours. If you want to report in days, you will need to add that in.)

If you want more than 1 column but only 2 or 3 then Sumifs1 + Sumifs2 + Sumifs3, would be worth considering.

If its more than that there are alternate design options we can go into.
 
Upvote 0
Thats correct, summing total hours per column, but I thought that if moving the required columns closer to the Employee ID, the lookup range would be smaller and more efficient, or would that not be an issue. Since the number of rows are around 20,000 - 24,000.
 
Upvote 0
The distance will matter for the SumProduct which creates a huge array that it cross multiplies. The Sumifs won't be impacted and should be more efficient even than SumProduct with the columns rearranged.
With larger volumes of data Power Query and Power Pivot are likely to scale up better.
 
Upvote 0

Forum statistics

Threads
1,215,024
Messages
6,122,729
Members
449,093
Latest member
Mnur

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