Excel VBA - When 2 cells in a column are the same, add the 2 corrisponce number in the next column - excel

I have three columns in my spreadsheet ID, Time and Height. What I want to do is run through the ID column and where the IDs match (Same ID occurs more than once in the list) sum the Height together for the earliest time (see example 2)
Example 1 (What I currently have)
ID Time Height
AB123 06:00 10
AD1234 10:00 10
AC12345 14:00 10
AC12345 06:00 10
AE123456 10:00 10
Example 2 (What I'm looking for)
ID Time Height
AB123 06:00 10
AD1234 10:00 10
AC12345 14:00 10
AC12345 06:00 20
AE123456 10:00 10
Within the sheet there may be 2, 2, 3 or 4 occurrence of the same ID number with multiple times.
I someone could help me with the VBA code for this I'd be very grateful.

Related

Count values of an Excel column in different ranges

I want to count number of values in different ranges in an Excel column.
Example 1:
Imagine I have some data in 40 rows, each one happened in different time of day. Like the below image:
now I want to count number of row repeated in different ranges, for example, count number of rows that have time between 12:00 to 18:00, again count 18:00 to 00:00, and more to 11:59 (next 12:00)
Time range
Count
00:00 to 6:00
?
06:00 to 12:00
?
12:00 to 18:00
?
18:00 to 23:59
?
Finally I have a table with 4 rows that shows how many row I have in those ranges and I can create a chart by that.
Example 2:
Count people based on age range. result would be like this:
Age range
Count
12 to 18
3
18 to 25
5
25 to 35
4
35 to 45
1
45 to 60
2
P.S:
I used countif with logical AND, but it didn't work. like this: =COUNTIFS(C:C,"AND(<00:00, >2:00)")
A more correct use of COUNTIFS (which is different from COUNTIF), would be :
'Counts values strictly between 00:00 and 2:00
=COUNTIFS(C:C,">00:00",C:C,"<2:00")
Hope it helps

Allocating staff to 30 minute intervals

I’m trying to create either a spreadsheet or macro that allows me to put and employee start and finish time in and then show me by each half hour how many half hours they are in.
For example;
Employee 1 - 08:30 to 10:15
The output I would want is;
08:00 0
08:30 1
09:00 1
09:30 1
10:00 0.5
10:30 0
11:00 0
So between 8:30 and 9:00 shows as one because that employee was in during that half hour. But between 10:00 and 10:30 it shows 0.5 because the employee was only in for half of that period
Then as I add more data it tells me which half hours they are covering.
I’ve tried using the ceiling and floor values, counta, countif, countifs, v and hlookups but just can’t get the desired effect.
I’m guessing it would need to be a macro.
Assuming C1 and D1 contain the start and end times of a shift and column A contains the times, use the following formula in column B:
=IF(A2>D$1,C2,IF(AND(A2>=C$1,A3<=D$1),1,MAX(0,A3-D$1)/(1/48)))
Limitations: shifts cannot cross over days, days cannot start before 00:00 or end after 23:59

How to split the time between two dates over daily columns

I am in much need of help please.
I have the table below.
It has about 300 rows and columns on the right for each day of the year starting 2015, ending 2019. I only entered a few columns here as an example.
What I need:
A formula to split the time (it can be hours or decimal days, it doesn't matter) between the two days on the left on the columns to the right.
In the rest of the days, which are not included in the period, I need a zero in those columns.
I manually entered the results I need so it can help you understand.
E.g.
On the first row:
The employee has spent
15 hours at destination in the first day of travel,
24 hours in the second day of travel
11:50 hours in the last day of travel.
All the rest of the columns to the right, which are not here, starting 1/4/2015 23:59 should give the result zero.
Thank You!!
Entry Date Exit Date 1/1/2015 23:59 1/2/2015 23:59 1/3/2015 23:59
1/1/15 9:00 1/3/15 11:50 15.00 24.00 11.50
As far as I can tell, it boils down to this
1.0 days 2.0 days 3.0 days 4.0 days
period=p in h/d.d p-(1-(so far)) p-(2-(so far)) p-(3-(so far)) 0
EDIT 2: I've come up with a formula.
NOTE:
This formula assumes existence of a column that specifies 0 hours of work between the "end date/time" column ("B") and first work hour ("D"), so you have it like this:
A B C D E F G H
1 Entry Date Exit Date ZERO 1/1/2015 23:59 1/2/2015 23:59 1/3/2015 23:59 1/4/2015 23:59 1/5/2015 23:59
2 1/1/15 9:00 1/3/2015 11:50 0 0.6243055556 1 0.49375 0 0.00
3
The extra column is there to avoid circular dependencies, since
we must "iteratively" accumulate the hours
The main formula for row 2 from column D being:
=MIN(D1-$A2,MIN(MAX(($B2-$A2) - SUM($C2:C2), 0),1))
Note: semicolons may be needed instead of commas in Excel as delimiters.
Note: the last digit in the formula depends on the unit. Here it is in days decimal. If hours, put 24.
In plain English, for a cell
Calculate sum of hours/days decimal in the range C to wherever - 1
Subtract result from the difference between start and end (total hours)
If less than 0, put 0 (MAX(x, 0))
If more than 1, put 1 (MIN(x, 1))
Choose between the result, or the difference between current date and start time, if it is smaller

Count num of values within range on every Nth cell

We have a data-set generated by an HR system of staff times, which isn't in the most helpful of formats.
We need to count the # of staff who start work before 10.00, and split those counts by week number (1-4) and day (Sun-Sat).
Every group of rows (1-4) is an individual.
Here's an example day's data:
Week Start
1 -
2 10:00
3 09.00
4 09.55
1 09:25
2 10.00
3 10:25
4 10.25
1 09:25
2 09.55
3 08:00
4 10.00
1 10:25
2 09.25
3 09:25
4 10.15
1 10.00
2 08:25
3 09.45
4 11:25
So I can count the occurence of start times before 10am on the entire column with:
=COUNTIFS(L5:L203,">=00:30",L5:L203,"<=10:00")
but to do separate counts for every 4th row (so sum all for ones on week 1, all on week 2, etc) is beyond me.
I tried the following after searching similar questions, but it didn't work:
=COUNTIFS((MOD(ROW(L5:L203),5)=0),">=00:30",(MOD(ROW(L5:L203),5)=0),"<=10:00")
Ideally I'd be able to put values into a table with a count for week 1 on Sunday, week 2 on Sunday, week 1 on Monday, and so on.
Any ideas?
To count the weeks use:
=COUNTIFs(B$2:B$21,"<=10:00",$A$2:$A$21,"=1")
=COUNTIFs(B$2:B$21,"<=10:00",$A$2:$A$21,"=2")
Changing =1, =2, =3, =4 for each weeks count. If column B is Monday and column C is Tuesday, you can drag the above formulas right across the row to get the weekly counts for other days.

Search text column for certain strings and return the time in the cell, Excel

I work in a hospital and need to keep track of how much time and when I spend that time in different areas. Clinical and Research. There are multiple things that count as clinical hours though they are not the same thing. In the picture CFW, Clinic, AMJ, etc count as clinical. Training, Research, lunch, data, r383, experiment are not needed to keep track of. It appears like this:
___ A ____ B
1 8:30 Training
2 9:00 CFW
3 9:30 CFW
4 10:00 CFW
5 10:30 Clinic
6 11:00 Clinic
7 11:30 Research
8 12:00 Lunch
9 12:30 Data
10 1:00 AMJ
11 1:30 AMJ
12 2:00 AMJ
13 2:30 R383
14 3:00 Experiment
15 3:30 Experiment
16 4:00 Research
My end goal is to be able to have a list of text that I can search for, like "Clinic", and it tell me which times I was there and the total time spent, like 10:30-11:30 1 hour.
Clinic: 10:30-11:30
1 Hour
So if I search all of the text phrases it would have the times spent there (beginning - end), and the sum total length of time. I have a separate sheet for each day of the week and ideally would like to be able to search through all 5 sheets at once and have all the days pop up all organized and such.
For example:
Monday
Clinic: 10:30-11:30
1 Hour
AMJ: 1:00-2:30
1.5 Hours
Tuesday
CFW: 9:00-10:30
1.5 Hours
etc...
Sorry for my formatting. Let me know if I need to any more information. I'm not able to post the picture yet. Any ideas? Please and thank
You want to create a VBA Macro on your Excel Spreadsheet that will go through each sheet, look for what value you want (e.g. "Clinic"), capture your times and calculate the elapsed.
You can refer to each sheet, and the cells in loops. Here is a shell to get you started.
Public Function elapsedTime (byval searchFor As String)
Dim shIdx as Integer
Dim rIdx as Integer
Dim calc as Integer
rIdx = 2
For shIdx = 1 to 5
While Sheets(shIdx).Range("A" + CStr(rIdx)).Value <> ""
If Sheets(shIdx).Range("B" + CStr(rIdx)).Value = searchFor Then
Additional logic here to calculate your value and
store it in "calc"...
End If
rIdx = rIdx + 1
Wend
rIdx = 2
Next shIdx
elapsedTime = calc
End Function

Resources