Returning text by searching quantities in week numbers

douglasjoh

New Member
Joined
Nov 17, 2016
Messages
15
Hi,

I am trying to return a contract number from a table of data by looking at the Week number. I am only looking to retrieve contracts with a quantity in the desired Week. I.e. by look at week 11 I would like to receive Orange and Pear.

The table looks like:
Q1 2017Q2 2017Q3 2017Q4 2017
ContractTotal QuantityJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
Orange3012222222211111111111
Apple11
Pear511111
Bannana17112121122121
Tangerine2712121211222222121
Pineapple1011111
Mango321

<tbody>
</tbody><colgroup><col><col><col><col><col span="7"><col span="45"></colgroup>
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi Douglas,

I'm having some issues copying the formulas, but I hope I can make myself clear.

I used Named Ranges to make formulas easier to read, but you can get the same result by using Array Formulas.
ABCDEFGHIJKLMNO
1
January​
February​
March​
2
Contract
Total
1
2
3
4
5
6
7
8
9
10
11
12
13
3
Orange
30​
1​
2​
2​
2​
2​
2​
2​
2​
2​
1​
1​
1​
1​
4
Apple
1​
5
Pear
5​
1​
1​
1​
1​
6
Bannana
17​
7
Tangerine
27​
8
Pineapple
10​
9
Mango
3​

<tbody>
</tbody>


Using the dummy worksheet shown above, I created Named Ranges for the data cells, the week headers and the fruit names (RowIDs). I also named a cell 'Week' to make it more readable.
Workbook Defined Names
NameRefers To
ActiveRows=IF(WeekColumn>0,RowIDs,"")
Data=Hoja2!$C$3:$BB$9
Headers=Hoja2!$C$2:$BB$2
RowIDs=Hoja2!$A$3:$A$9
Week=Hoja2!$A$12
WeekColumn=INDEX(Data,,MATCH(Week,Headers,0))

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>

I then created a range called WeekColumn that returns an array with all the cells corresponding to that week. It first finds the week within the headers using MATCH() and then retrieves the entire data column using INDEX(). Notice the second argument for INDEX() is empty, this tells Excel you want it to return the entire column as an array.

WeekColumn=INDEX(Data,,MATCH(Week,Headers,0))

<tbody>
</tbody>

You can the use the IF() function to return the fruit names (RowIDs) if the corresponding cell has a value greater than 0, otherwise return an empty string.

ActiveRows=IF(WeekColumn>0,RowIDs,"")

<tbody>
</tbody>

You could stop there and paste this formula over 7 cells, hit F2, CTRL+SHIFT+ENTER and get the results seen in cells C12:C18. If you want the results in one cell, separated by commas, head on to the next formula.

ABCD
11WeekWeekColumnActiveRows
12111OrangeOrange, Pear
130
141Pear
150
160
170
180

<tbody>
</tbody>

This will only work if you have a relatively small number of fruit names cause it will otherwise become to boring to build the formula. In this case it was fast to write the formula.
  • It uses INDEX() to get the nth value of the ActiveRows array and concatenates it with the next one separated by a space.
  • TRIM() then reduces the numer of spaces to 1 between each word, because there will be spaces also for empty strings in the array.
  • Having only one space separating each work, SUBSTITUTE() can change each for a comma and a space ", " to make it easier to read.

Worksheet Formulas
CellFormula
D12=SUBSTITUTE(
TRIM(
INDEX(ActiveRows,1)&" "&
INDEX(ActiveRows,2)&" "&
INDEX(ActiveRows,3)&" "&
INDEX(ActiveRows,4)&" "&
INDEX(ActiveRows,5)&" "&
INDEX(ActiveRows,6)&" "&
INDEX(ActiveRows,7)
),
" ",
", "
)

<tbody>
</tbody>

<tbody>
</tbody>

I hope it helps.

PS. This is how it works with array formulas, just enter them using CTRL+SHIFT+ENTER.

A
B
C
D
11
WeekWeekColumnActiveRows
12
11​
{=INDEX(Data,,MATCH(Week,Headers,0))}​
{=IF(WeekColumn>0,RowIDs,"")}=SUBSTITUTE(
TRIM(
INDEX(ActiveRows,1)&" "&
INDEX(ActiveRows,2)&" "&
INDEX(ActiveRows,3)&" "&
INDEX(ActiveRows,4)&" "&
INDEX(ActiveRows,5)&" "&
INDEX(ActiveRows,6)&" "&
INDEX(ActiveRows,7)
)," ",", "
)

<tbody>
</tbody>
 
Upvote 0
Hi Franz,

Thanks for this. Sorry for the late reply.

But, whenever trying to start applying the WeekColumn Formula I am presented with the Error #REF!. This is coming from the Data range. I'm not sure it likes using different columns. Maybe I am not applying the formula correct? =INDEX('Export Planning'!F6:BB30,MATCH('Export Planning'!M5,'Export Planning'!F5:BE5,0))

I have tried both array and non array formulas. Still get the same Error Message.

Hopefully you can help,

Thanks
 
Upvote 0
Maybe I am not applying the formula correct? =INDEX('Export Planning'!F6:BB30,MATCH('Export Planning'!M5,'Export Planning'!F5:BE5,0))

I think the issue might be that you need to use 2 commas between your data range and the MATCH() function.
Incorrect:=INDEX('Export Planning'!F6:BB30,MATCH('Export Planning'!M5,'Export Planning'!F5:BE5,0))
Correct:=INDEX('Export Planning'!F6:BB30,,MATCH('Export Planning'!M5,'Export Planning'!F5:BE5,0))

The double comma tells Excel that you are using the 3 argument version of INDEX() and that the second argument, which corresponds to the row index, is blank because you want all the rows of that columns to be returned.

Try it out and let me know if that solved the problem.
 
Upvote 0
That's worked a trick, appreciate it Franz.

Next Question - how to I bring return more than 1 fruit. As when copying down the formula for Weekcolumn and ActiveRows, I am presented with the same answer over and over.

How do I rectify this?

Thanks
 
Upvote 0
You have to select the entire range (C12:C18 in the dummy), paste your formula in the Formula Bar and hit CTRL+SHIFT+ENTER. This way all cells in the range will share the same array formula, but each will return the result according to its position within the array. A lot has been written about array formulas, in case you want to learn more about them.

Let me give you a quick tip about entering formulas. I know 3 ways of entering:
  1. ENTER - I am sure you know this one, it simply confirms whatever you have typed in the Formula Bar and saves it in the Active Cell. If the current selection includes more than one cell, the formula will only be saved in the Active Cell. (SHIFT+ENTER, TAB, and SHIFT+TAB have the same behavior)
  2. CTRL+ENTER - This method saves the formula to all cells in the current selection, not only the Active Cell. I use it mainly to avoid having to fill down a formula or copy pasting it.
  3. CTRL+SHIFT+ENTER - This combination will do 4 things:
    1. Calculate the formula in array or CSE mode, allowing calculations otherwise impossible which may return more than one result.
    2. Store the formula in all cells in the current selection.
    3. Convert the current selection to an array that can only be modified as a group.
    4. Display multiple results if the formula returns more than one value and the current selection includes more than one cell. If your formula returns 10 results, you need to enter it in a range of 10 cells so all results are displayed. If the range is smaller, some values will not be displayed.

In your case, when you entered the formula in a single cell, only the first result is displayed and if you copy the formula down, it will always display the first result. That's why you need to select the entire range before typing the formula and using CTRL+SHIFT+ENTER.

I hope it is not too confusing.
 
Upvote 0
Thanks Franz that's it all working!

One final question - In column D is it possible to split the answer into multiple rows?

For example:

Orange

Pear

Thanks
 
Upvote 0
You can enter the following formula in range E12:E18 (in the dummy) using CTRL+SHIFT+ENTER.

Code:
{=IFERROR(
	INDEX(
		ActiveRows,
		SMALL(
			(
				ROW(WeekColumn)*
				IF(
					WeekColumn=0,
					ROWS(ActiveRows),
					1
				)
			),
			ROW()-ROW(MultiRowHeader)
		)-ROW(MultiRowHeader)
	),
	""
)}

MultiRowHeader is the cell just above your output range, in this case E11.
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,681
Members
449,048
Latest member
81jamesacct

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