Display rows as blank if another cell is empty - excel

I have the following 'hospitals' sheet in excel:
A B C D E
1 Regions Region 1 Region 2 Region 3 Region 4
2 Region 1 Hospital 1 Hospital 6 Hospital 11 Hospital 15
3 Region 2 Hospital 2 Hospital 7 Hospital 12 Hospital 16
4 Region 3 Hospital 3 Hospital 8 Hospital 13 Hospital 17
5 Region 4 Hospital 4 Hospital 9 Hospital 14 Hospital 18
6 Region 5 Hospital 5 Hospital 10
7 Region 6
8 Region 7
9 Region 8
On my 'report' sheet, I have the following table set up with column headers 'Region' in A6 and 'Hospital' in B6:
A B C D E
6 Region Hospital Dept Admissions Discharges
7 Region 1 Hospital 1 A&E 24 12
8
9
10
11 Hospital 2 Opth 45 76
12
13
14
15 Hospital 3
16
17
18
19 Hospital 4
20
21
22
23 Hospital 5
24
A7 in the table above is a drop-down menu with the values from A2-A9 from my 'hospitals' sheet. When this is entered, I'd like to return a list of hospitals from that particular region in cells B7, B11, B15, B19, B23 etc of my 'report' sheet.
However, when it gets to the last hospital in the respective column on the 'hospitals' sheet, I would then like the formatting of columns A:E on the report sheet to appear as blank, rather than have zeros or #N/A values in columns C-E of my report sheet. Is this something that can be done in VBA?
To summarise, basically, I need some code for the workbook that will show the following range of cells report!A27:E30 to be empty/blank of all formatting if there is no value in 'hospitals!B7'. i.e. when the formulas in column B of the 'report' sheet stop pulling values from column b of the 'hospitals' sheet, everything below this will appear empty.
I'm not sure this is possible.

VBA is not required for the task.
For Sheet 'report' the formula in cells:
B7: =IFERROR("Hospital "&(RIGHT($A$7,1)/1-1)*5+1,"")
B11: =IFERROR("Hospital "&(RIGHT($A$7,1)/1-1)*5+2,"")
B15: =IFERROR("Hospital "&(RIGHT($A$7,1)/1-1)*5+3,"")
B19: =IFERROR("Hospital "&(RIGHT($A$7,1)/1-1)*5+4,"")
B23: =IFERROR(IF(RIGHT($A$7,1)<3,("Hospital "&RIGHT($A$7,1)/1-1)*5+5,""),"")

Related

Return row label for largest & 2nd largest value in criteria range

I have the following table
Name
Point
Bonus
Total
Pos
1st
Name
2nd
Name
Bob
10
8
6
Point
11
10
Sue
9
5
3
Bonus
12
9
Joe
11
2
4
Total
10
7
Susan
7
9
10
Tim
1
12
4
Ellie
9
8
7
In G2 I have the following formula
{=LARGE(IF($B$1:$D$1 =$F2, $B:$D),1)}
Which returns the largest Point value, as 11.
In H2 I want to return the name where the Point value is 11. so the value for H2 should be Joe
Then in J2 want to do the same for the 2nd largest value. So the value of J2 should be Bob
I have used following formulas as per attached scheenshot.
G2=LARGE(INDEX($B$2:$D$7,,MATCH(F2,$B$1:$D$1,0)),1)
H2=INDEX($A$2:$A$7,MATCH(G2,INDEX($B$2:$D$7,,MATCH(F2,$B$1:$D$1,0)),0))
I2=LARGE(INDEX($B$2:$D$7,,MATCH(F2,$B$1:$D$1,0)),2)
J2=INDEX($A$2:$A$7,MATCH(I2,INDEX($B$2:$D$7,,MATCH(F2,$B$1:$D$1,0)),0))
And if you have Microsoft-365 then could try below formula to get names directly.
=LET(x,FILTER($B$2:$D$7,$B$1:$D$1=F2),FILTER($A$2:$A$7,x=LARGE(x,1)))

Aggregate values based on lookup IDs while meeting multiple criteria

I am given sales data as per "sheet 1" below. Each row contains the quarter, the product, the region ID, and the sales figure. Each region can have multiple IDs, and I have a lookup table (sheet 2) which denote which region each ID belongs to.
My goal is to get to sheet 3. Essentially I am trying to write a formula that will reference the product name in column A, the quarter in cell A1 (which the user will input), and aggregate the relevant sales figures under each region in column B.
I have tried nesting an INDEX & MATCH function within a SUMIFS within a SUMPRODUCT as per below, but I am getting a #VALUE! error:
=SUMPRODUCT(SUMIFS(INDEX(sheet1!$D:$D$,MATCH(1,($A4=sheet1!$B:$B)*($A$1=Sheet1!$A:$A),0),0),sheet1$C:$C,sheet2!$A$2:$A$8)*(sheet2!$B$2:$B$8=$B4))
Does anyone know what is wrong with my formula, or if there is a better approach to this problem?
Sheet 1 (Raw data)
A
B
C
D
1
Quarter
Product
ID
2021 Sales
2
Q1
A
1
39
3
Q1
A
3
41
4
Q1
A
7
20
5
Q1
A
14
7
6
Q1
A
25
2
7
Q1
A
27
2
8
Q1
A
44
45
9
Q1
B
1
28
10
Q1
B
3
34
11
Q1
B
7
29
12
Q1
B
14
48
13
Q1
B
25
5
14
Q1
B
27
15
15
Q1
B
44
32
16
Q2
A
1
19
17
Q2
A
3
28
and so forth…
Sheet 2 (region ID lookup table)
A
B
1
ID
Region
2
1
East
3
3
East
4
7
Central
5
14
Central
6
25
Central
7
27
West
8
44
West
Sheet 3 (Report)
A
B
C
1
Q1
2
3
Product
Region
Sales
4
A
East
29
5
A
Central
42
6
A
West
31
A little rework:
=SUMPRODUCT(SUMIFS(Sheet1!D:D,Sheet1!C:C,IF(Sheet2!$B$2:$B$8=$B4,Sheet2!$A$2:$A$8),Sheet1!A:A,$A$1,Sheet1!B:B,A4))
Depending on version one may need to confirm with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
With the dynamic array formula FILTER we can replace the IF() part with FILTER():
=SUMPRODUCT(SUMIFS(Sheet1!D:D,Sheet1!C:C,FILTER(Sheet2!$A$2:$A$8,Sheet2!$B$2:$B$8=$B4),Sheet1!A:A,$A$1,Sheet1!B:B,A4))
And it will save a few iterations.

Remove unique row from csv

I have a csv with the following structure:
Source Target LinkId LinkName Throughput
==================================================
1 12 1250 link1250 5
1 12 3250 link3250 14
1 14 1250 link1250 5
1 14 3250 link3250 14
1 18 1250 link1250 5
1 18 3250 link3250 14
1 25 250 link250 24 //to be removed
2 12 2250 link2250 5
2 12 5250 link5250 14
2 14 2250 link2250 5
2 14 5250 link5250 14
2 18 2250 link2250 5
2 18 5250 link5250 14
2 58 50 link50 34 //to be removed
In the above csv, I am supposed to removed rows that are occurring only once, like the ones highlighted above. In the highlighted row, there is only one row existing for link 250. I was wondering if there is an easy way to do this using excel.
1) "Quick Fix" Approach - helper column and filter
Create a new column F "Occurances". In F3, enter
=COUNTIF(D:D,D3)
Then copy cell F3 to F4:F16. Now you can simply filter everything that only has one occurance:
Select your whole range (A1:F16, including column F and row 1)
Click Data -> Filter
Click Occurance(F1) -> unselect "1" to filter the unwanted rows
2) "Continuous Fix" - helper column and Index/Match
Create a new column F "Helper". In F3, enter
=F2+(COUNTIF(D:D,D3)<>1)
Then copy cell F3 to F4:F16. You can now create a "cleaned" or filtered sheet of your data:
Create a new Excel Worksheet (Sheet2, I assume the old one is named Sheet1)
Copy your headers (A1:E2) from Sheet1
Into A3, enter =INDEX(Sheet1!A$3:F$16,MATCH(ROW()-2,Sheet1!$F$3:$F$16,0),1)
Copy A3 to A3:E14
Sheet2 will now automatically create a filtered copy of Sheet1.

Formula to add text based on values of two other cells

I have a large spreadsheet that needs a bit of fine-tuning. Column A contains numbers from 1-4000, sequentially-listed. Column B contains some of those same numbers, but many numbers are skipped (for example: 1, 5, 6, 7, 11, 12, 25...); those numbers are not "spaced out" relative to the numbers in column A. Column C contains text entries related to the Column B values. I need to add a formula in column D that will evaluate whether the number in A1 (for example) matches the number in B1, and if it does match it should place the value of C1 in D1. Likewise, if A2 matches B2, then the value of C2 should be placed in D1.
I tried writing some formulas similar to this, but they become far too large when you have to take into account every cell combination: =IF(A1=E1,F1). If I can just get the column B cells to "space themselves out" so that they match up with the cells in column A, that would get me where I need to be. Of course, the Column C data values would need to "follow along" with the Column B values when they are spaced out.
Any ideas on how I can make this happen?
If the data is like this:
A B C
---------------
1 1 Red
2 5 Blue
3 6 Red
4 7 Yellow
5 11 Black
6 12 Green
7 25 Yellow
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
And the desired result is this:
A B C D
--------------------
1 1 Red Red
2 5 Blue
3 6 Red
4 7 Yellow
5 11 Black Blue
6 12 Green Red
7 25 Yellow Yellow
8
9
10
11 Black
12 Green
13
14
15
16
17
18
19
20
21
22
23
24
25 Yellow
We can accomplish that using an INDEX-MATCH based formula starting in D1 and auto filling down:
=IFERROR(INDEX(C:C,MATCH(A1,B:B,0)),"")
It will look like this:
A B C D
-----------------------------------------------------------
1 1 Red =IFERROR(INDEX(C:C,MATCH(A1,B:B,0)),"")
2 5 Blue =IFERROR(INDEX(C:C,MATCH(A2,B:B,0)),"")
3 6 Red =IFERROR(INDEX(C:C,MATCH(A3,B:B,0)),"")
4 7 Yellow =IFERROR(INDEX(C:C,MATCH(A4,B:B,0)),"")
5 11 Black =IFERROR(INDEX(C:C,MATCH(A5,B:B,0)),"")
6 12 Green =IFERROR(INDEX(C:C,MATCH(A6,B:B,0)),"")
7 25 Yellow =IFERROR(INDEX(C:C,MATCH(A7,B:B,0)),"")
8 =IFERROR(INDEX(C:C,MATCH(A8,B:B,0)),"")
9 =IFERROR(INDEX(C:C,MATCH(A9,B:B,0)),"")
10 =IFERROR(INDEX(C:C,MATCH(A10,B:B,0)),"")
11 =IFERROR(INDEX(C:C,MATCH(A11,B:B,0)),"")
12 =IFERROR(INDEX(C:C,MATCH(A12,B:B,0)),"")
13 =IFERROR(INDEX(C:C,MATCH(A13,B:B,0)),"")
14 =IFERROR(INDEX(C:C,MATCH(A14,B:B,0)),"")
15 =IFERROR(INDEX(C:C,MATCH(A15,B:B,0)),"")
16 =IFERROR(INDEX(C:C,MATCH(A16,B:B,0)),"")
17 =IFERROR(INDEX(C:C,MATCH(A17,B:B,0)),"")
18 =IFERROR(INDEX(C:C,MATCH(A18,B:B,0)),"")
19 =IFERROR(INDEX(C:C,MATCH(A19,B:B,0)),"")
20 =IFERROR(INDEX(C:C,MATCH(A20,B:B,0)),"")
21 =IFERROR(INDEX(C:C,MATCH(A21,B:B,0)),"")
22 =IFERROR(INDEX(C:C,MATCH(A22,B:B,0)),"")
23 =IFERROR(INDEX(C:C,MATCH(A23,B:B,0)),"")
24 =IFERROR(INDEX(C:C,MATCH(A24,B:B,0)),"")
25 =IFERROR(INDEX(C:C,MATCH(A25,B:B,0)),"")

Sum number according to date and name in excel

To sum the third column (numbers o companies) I've used this
=SUM(1/COUNTIF(Names;Names))
Names is name of array in C column and CTRL+SHIFT+ENTER and it works perfectly.
Now I'd like to sum earnings but only for each company once and with the latest data. For example, the result shoud be like this
=C4+C6+C7+C8+C9+C10
(93)
Thanks
A B C D
1 # company earnings date
2 1 ISB 12 10/11/2011
3 2 DTN 15 11/11/2011
4 3 ABC 13 12/11/2011
5 4 ISB 17 13/11/2011
6 5 RTV 18 14/11/2011
7 6 DTN 22 15/11/2011
8 7 PVS 11 16/11/2011
9 8 ISB 19 17/11/2011
10 9 ANH 10 18/11/2011
Sum 6 93
Assuming ascending dates, you could try with CTRL+SHIFT+ENTER in C11:
=SUM((MAX(A2:A10)-MATCH(B2:B10,LOOKUP(MAX(A2:A10)-A2:A10,A2:A10-1,B2:B10),0)=A2:A10-1)*C2:C10)
I'd suggest using a helper column as the easiest approach. In E2 use this formula
=IF(COUNTIF(B2:B$1000,B2)=1,C2,"")
and copy down the column. Now sum column D for the required answer.
Note that the above formula assumes 1000 rows of data maximum, increase if required.

Resources