Adding up values in columns with conditions - excel

Being beginner and first time on this site, I truly appreciate your help.
WK 1 WK 2 WK 3 WK 4 WK 5 TOTAL HOURS TOTAL OF FIRST 3 WEEKS <> 0
John 10 0 5 6 5 26 21
Smith 4 1 10 3 4 22 15
Peter 0 4 4 4 2 14 12
Susan 5 5 0 5 8 23 15
From my table I want to add only the first three columns that contain no zero. If there's zero on first three, check on next column and add it up to complete three columns again with no zero value. Some function like in Col H TOTAL OF FIRST 3 WEEKS <>0 (where I had to do it manually).
If I can learn set of VB code or any example with formula or macros, thank you so so much. I'm using Excel 2007.

This is the complicated formula Ali M refers to. It's an array formula entered with ctrl-shift-enter:
=IF(COUNTIF(A2:F2,"<>0")=0,0,SUM(A2:INDEX(A2:F2,SMALL(IF(A2:F2<>0,COLUMN(A2:F2),""),MIN(3,COUNTIF(A2:F2,"<>0"))))))
Note that it works if there are less than three non-zero values.

you can use formula but it would be complicated. instead you can use this subroutine that act exactly as you want!
Public Sub y()
Dim i, sum, c As Integer
Dim Rng, Row, cell As Range
Set Rng = Range("B2:F5")
i = 0
For Each Row In Rng.Rows
For Each cell In Row.Cells
If (cell.Value <> 0 And i < 3) Then
sum = sum + cell.Value
i = i + 1
End If
Next cell
Cells(Row.Row, 7).Value = sum
sum = 0
i = 0
Next Row
End Sub
It always put the sum in column H. you can change it by changing this line:
Cells(Row.Row, 7).Value = sum

Related

How to COUNT total consecutive numbers of a value in a column in Excel?

I have a column consists of 0 and 1. I need to count the total number of consecutive occurrences of 1.
Column
0
0
1
1
1
0
0
0
1
1
0
1
1
1
The answer should be: 5 (5 consecutive 1's)
No need for VBA or a UDF:
=COUNTIFS(A1:A13,1,A2:A14,1)
a SUMPRODUCT also works:
=SUMPRODUCT(--(A1:A13=A2:A14),--(A2:A14=1))
In both cases, not the row offset in the ranges
Judging by the answer 5, and the comments, I'm guessing you want to get the number of 1 following another 1.
There's probably an easy function for that, but since I'm better with VBA...
This would check the first column (A) and all the way down, and return the amount of 1 that follow another 1.
Sub oneFollowingOne()
Dim r As Range, total As Long
total = 0
For Each r In Range("A2", ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp))
If r.Value = 1 And r.Offset(-1).Value = 1 Then total = total + 1
Next r
MsgBox "Total: " & total
End Sub

Looping a column and inserting a formula that has relative reference, but made absolute/fixed each time the formula is inserted

This is a follow up from a previous question which has been kindly solved by Jonathan Willcock.
He has helped me with a solution to loop through 2 columns and insert a formula in another column where applicable.
However, my problem now is that the formula I want to insert (in column D below), should be the first corresponding row in column C each time the start is 'triggered'. ie, I'd like column D to show the trigger date each time the loop is triggered.
What this looks like is:
A B C D
1 31/01/2018
2 1 01/02/2018 01/02/2018
3 02/02/2018 01/02/2018
4 03/02/2018 01/02/2018
5 04/02/2018 01/02/2018
6 05/02/2018 01/02/2018
7 1 06/02/2018 01/02/2018
8 07/02/2018
9 08/02/2018
10 09/02/2018
11 1 10/02/2018 10/02/2018
12 11/02/2018 10/02/2018
13 12/02/2018 10/02/2018
14 13/02/2018 10/02/2018
15 1 14/02/2018 10/02/2018
16 15/02/2018
17 16/02/2018
18 17/02/2018
19 1 18/02/2018 18/02/2018
20 19/02/2018 18/02/2018
21 20/02/2018 18/02/2018
Column A is the start trigger, column B is the end trigger, and column D is where I want the formula inserted, which is the date of the trigger.
This is a modification of the code he wrote, which finds the start and stop point of inserting a formula in anothr column:
Sub Button1_Click()
Dim endRow As Integer
Dim doFormula As Boolean
Dim i As Integer
doFormula = False
endRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To endRow
If Cells(i, 1) = 1 Or doFormula Then
Cells(i, 4) = "[formula that returns trigger date of that incident]"
doFormula = True
End If
If Cells(i, 1) <> 1 And Cells(i, 2) = 1 Then
doFormula = False
End If
Next
End Sub
I was wondering if anyone might have some insight on solving this issue?
I understand how to use relative vs absolute RC reference, but not sure how to 'fix' a relative value each time the loop is triggered.
Thank you!

How to count cells until a value is greater than 0?

In Excel I am trying to count the number of days until an appointment is available.
in the below data set I need to be able to count until a value is reached that is not 0.
the result I need is
Example 1 = 3
Example 2 = 5
Example 3 = 0
In plain English I need it to check if cell = 0 if it is then count it, and stop counting when cell is no longer = 0
If there is a VBA solution that would be best but ill accept anything that works.
Example1 Example2 Example3
May 13 2019 0 0 2
May 14 2019 0 0 0
May 15 2019 0 0 6
May 16 2019 6 0 0
May 17 2019 0 0 3
May 20 2019 3 7 0
May 21 2019 6 14 0
May 22 2019 6 0 1
May 23 2019 12 14 0
May 24 2019 7 0 0
I have tried multiple methods however the closest i got was with the below VBA which seems to give the right answer before crashing my excel so I suspect its counting something it shouldn't.
Dim iCntr As Integer
iCntr = 2
Do While (Cells(iCntr, 3).Value) = 0
Range("C13").Value = Application.WorksheetFunction.Count("C:C")
Loop
End Sub
Formula;
For example 1 but editing returns the example 2 and 3.
=MATCH(TRUE,INDEX($B$2:$B$11>0,0),0)-1
Perhaps the simplest way is the next formula:
=IFERROR(MATCH(0,B:B,1)-MATCH(0,B:B,0)+1;0)
Assuming we're dealing with data on column B.
Function DAYS_UNTIL_APPOINTMENT(ByVal OnThisRange As Range) As Byte
Dim rng As Range
For Each rng In OnThisRange
If rng.Value <> 0 Then
Exit For
Else
DAYS_UNTIL_APPOINTMENT = DAYS_UNTIL_APPOINTMENT + 1
End If
Next rng
End Function
Please, note this only will work if you select 1 column of data. Also, I made it Byte type, so if the number of days is higher than 255, then it will raise an error. Just change it to Integer if you need it.
you could use a match formula =IF(B2<>0,0,MATCH(0,B2:B20,1)) with ascending order (edit:added if as this doesn't work if first day is available)
Like this:
Public Function count_zeroes(ByVal columnID As Long) As Long
Dim i As Long: i = 1
Dim cell As Range: Set cell = ActiveSheet.Cells(i, columnID)
If Not IsEmpty(cell) Then
Do Until cell <> 0 'we'll keep counting until cell <> 0
i = i + 1
Set cell = ActiveSheet.Cells(i, columnID)
Loop
End IF
count_zeroes = i - 1
End Function
Yet another option using an array formula that works anywhere (at least I think it does) ...
{=MIN(IF(B4:B13>0,ROW(B4:B13)-MIN(ROW(B4:B13)),""))}
Be sure to commit using Shift + Ctrl + Enter

Replacing values in a column according to a map

I have two columns A and B in an Excel sheet, similar to the following:-
A B
1 1
2 2
3 4
4 5
5 6
6 7
7 8
8 10
9 11
10 12
11 13
12 15
13 16
14 17
15 18
Now, in a different sheet, I have a column of B values, and I want to 'map' them to their corresponding A values. By 'map' them, I mean replace a B value with the A value that is adjacent to it in the first sheet. How do I do this?
Option 1)
In sheet2 column C you want your results and lets say and your B data is in column D just to mix things up.
=INDEX(SHEET1!$A$1:$A$15,MATCH(D2,SHEET1!$B$1:$B$15,0))
Option 2)
Same setup but lets use the LOOKUP function
=LOOKUP(D2,SHEET1!$B$1:$B$15,SHEET1!$A$1:$A$15)
With Sheet1 like:
and Sheet2 like:
Running this short macro:
Sub Translate()
Dim B As Range, RangeToFix As Range, r As Range
Dim fnd As Range
Set B = Sheets("Sheet1").Range("B1:B15")
Set RangeToFix = Sheets("Sheet2").Range("B1:B11")
For Each r In RangeToFix
Set fnd = B.Find(What:=r.Value, After:=B(1))
If fnd Is Nothing Then
r.Offset(0, 1).Value = "not found"
Else
r.Value = fnd.Offset(0, -1).Value
End If
Next r
End Sub
will Produce this in Sheet2:
This does the "translation" in-place.

Creating a border around cells with the same value

I have a table like the one below. How can I get Excel to put borders around groups with the same number in the 4th column so that there is a border around the groups. I was thinking conditional formatting could do it but I can't think how. So I think the only option is a macro. Could anybody help?
1 64436 549419 1
2 64437 549420 1
3 64438 549421 1
4 64439 549422 1
5 64440 549423 1
6 64441 549424 1
7 64442 549425 1
8 64443 549426 1
9 64444 549427 1
10 64445 549428 1
11 64446 549429 1
12 64447 549430 1
13 64448 549431 2
14 64449 549432 2
15 64450 549433 2
16 64451 549434 2
17 64452 549435 2
18 64453 549436 2
19 64454 549437 2
20 64455 549438 2
21 64456 549439 2
22 64457 549440 4
23 64458 549441 4
24 64459 549442 5
25 64460 549443 5
26 64461 549444 5
27 64462 549445 5
28 64463 549446 5
29 64464 549447 5
30 64465 549448 6
31 64466 549449 6
32 64467 549450 6
33 64468 549451 6
34 64469 549452 6
35 64470 549453 6
36 64471 549454 6
37 64472 549455 9
38 64473 549456 9
39 64474 549457 9
You need to use relative referencing.
Select the column range you want to do the conditional formatting on.
Enter the following three formulas in their own conditions:
=AND($C2=$C3,$C3=$C4)
This one is for the middle items. (Borders on both sides)
=AND($C2<>$C3,$C3=$C4)
This one is for the first in the group. (Border on left, top, right)
=AND($C2=$C3,$C3<>$C4)
This one is for the last in the group. (Border on left, bottom, right)
Format them as you want.
Replace all '$C' with '${Your Column}'. Note that this will not place any borders around single items since you can have no more the three conditional formatting conditions in a selection.
I cannot see a simple non-macro solution to exactly what you need but the solution from PowerUser seems okay.
Here is a macro based solution that will put a border around rows that have the same digit in the final column. I will assume your data are in columns A:D.
To use this macro just click any cell within your list and then fire the macro.
As a quick guide:
AddBorders is the main macro that simply loops through all the cells in the final column and works out when a border is appropriate
AddBorder is a short routine that adds the border.
As a bonus, AddBorder selects a random color from Excel's 56 color palette so that each of your borders are different colors to make easier viewing
Sub AddBorders()
Dim startRow As Integer
Dim iRow As Integer
startRow = 1
For iRow = 2 To ActiveCell.CurrentRegion.Rows.Count
If WorksheetFunction.IsNumber(Cells(iRow + 1, 4)) Then
If Cells(iRow, 4) <> Cells(iRow - 1, 4) Then
AddBorder startRow, iRow - 1
startRow = iRow
End If
Else
AddBorder startRow, iRow
End If
Next iRow
End Sub
Sub AddBorder(startRow As Integer, endRow As Integer)
Dim borderRange As Range
Dim randomColor As Integer
randomColor = Int((56 * Rnd) + 1)
Set borderRange = Range("A" & startRow & ":D" & endRow)
borderRange.BorderAround ColorIndex:=randomColor, Weight:=xlThick
End Sub
I came out with this solution, it works strange on my Excel 2010 :/
I cannot test it on 2003, so please let me know if thats fine.
Sub PaintBorder()
Dim iRow As Integer
iRow = 1
Dim strTemp As String
strTemp = Range("D" & iRow).Value
Dim strPrev As String
Dim sectionStart As Integer
sectionStart = 1
Do
strPrev = strTemp
strTemp = Range("D" & iRow).Value
If strPrev <> strTemp Then
ActiveSheet.Range(Cells(sectionStart, 1), Cells(iRow - 1, 4)).BorderAround xlSolid, xlMedium, xlColorIndexAutomatic
sectionStart = iRow
End If
iRow = iRow + 1
Loop Until strTemp = vbNullString
End Sub
Are you just trying to make it more readable to human eyes? If so, I recommend alternating background colors. For example, every time, the number in that 4th column changes, the background color would change from white to blue and vice-versa. I do this all the time:
Make an additional column E. Since your reference column is D, enter:
=MOD(IF(D5<>D4,E4+1,E4),2)
(i.e. if this row's column D is different from the last row's D, then change from either 0 to 1 or 1 to 0)
Hide the column so that the end-user doesn't see it.
Make 2 conditional formulas. The first will change the row color to white if your hidden value is 0. The second will change it to blue if your hidden value is 1.
No macros. No VBA coding. Just 1 hidden column and a few conditional formulas. And the colors should still alternate properly even though your column D is skipping numbers :)
(I use this daily on XL 2003. I hope it works on 2007)

Resources