Automatically restart formula given that parameter is met? - excel

I am in need of some assistance. I have a list of multiple id's in column A, column b contains data of the number of items linked to the ID. I want to generate a list of every page pertaining to each ID as follows, so i want in column C (for example) "a - Page 0001" all the way until "a - 1000" given that a had 1000 pages but then when it reaches 1000, i want it to restart from b as follows:
Column A Column B
a 1000
b 2000
c 1500
d 1200
e 700
a - Page 0001
a - Page 0002
a - Page 0003
a - Page 0004
…
a - Page 1000
b - Page 0001
b - Page 0002
b - Page 0003
b - Page 0004
…
b - Page 0001
…
b - Page 2000
c - Page 0001
I have tried using the following formula:
=IF(ROW(C1)< B1+1,CONCATENATE($A$1," - Page ",TEXT(ROW(C1),"0000"),""))
The problem is that once it reaches 1000 I get errors (#VALUE!), firstly, I believe I have to $ the &A$1 otherwise when I drag the formula down it will just refer to the column to the left an i'll get a - Page 0001, b - page 0002, etc. Secondly, I am using the ROW function in order to generate the page numbers but I don't understand how I can force it to restart from 1 once it reaches the maximum (i.e. 1000 for a).

This formula will generate you list of individual pages:
=IFERROR(INDEX($A$1:$A$5,IFERROR(MATCH(ROW(C1)-1,$C$1:$C$5,1)+1,1))&" - Page "&RIGHT("0000"&ROW(C1)-IFERROR(INDEX($C$1:$C$5,MATCH(ROW(C1)-1,$C$1:$C$5,1)),0),4),"")
The key to making it work is column C which is a helper formula. In C we are going to place a running total of the number of pages. In C1 use:
=SUM($B$1:$B1)
note the missing $ in the last address, its important that it not be there. copy that down for the length of your table.
Note the hidden rows

Related

how to count distinct values in excel for a matrix form

I have looked if this has been asked, but could not find out exactly.
I' ve been trying to count distinct values.
I tried sumproduct,sum(1/countif) etc, so far I got nothing but a div error or 0.
Basically, I' ve two columns: Campaign_no and customer_id.
what I need is count unique customers for each campaigns and count unique customers that appears in the campaigns at the same time, sort of matrix.
The table is as follows:
Campaign_no
Cust_id
A
1
A
2
A
2
B
1
B
4
B
5
B
9
C
4
C
5
C
6
C
7
What I need is below:
Campaigns
A
B
C
A
2
1
0
B
1
4
2
C
0
2
4
As you see Campaign A has 2 unique customers, so A-A cell is 2.
Campaign A and B have one customer in common, so A-B cell is 1.
Campaign A and C have no common customer, this box got 0.
Campaign B and C has 4 unique customer on their own,
but they have two common customers, so B-C box has 2 ( if those customers were the same, it would have been 1) .
Is there way of calculating this without vba or PT? I'm using Excel 2017.
Much appreciated.
Here is a solution using helper cells.
C2 is =A2&B2. Copy it to C3:C12.
D2 is =IF(ISNA(MATCH(B2,D1:$D$1,0)),B2,""). Copy it to D3:D12.
E2 is =IF($D2="","",1-ISNA(MATCH(E$1&$D2,$C$2:$C$12,0))). Copy it to E2:G12.
E15 is =SUMIFS($E$2:$E$12,E2:E12,1). Copy it to F15:G15.
E16 is =SUMIFS($F$2:$F$12,E2:E12,1). Copy it to F16:G16.
E17 is =SUMIFS($G$2:$G$12,E2:E12,1). Copy it to F17:G17.
You may be able to get away without using the helper column C in Office 2017. I only have Office 365, so I couldn't it correctly.
Here's one that you could try, but it assumes that the data is sorted into contiguous blocks in alphabetical order of campaign exactly as shown in the sample data:
=SUMPRODUCT((COUNTIFS($A$2:$A$12,F$1,$B$2:$B$12,INDEX($B$2:$B$12,MATCH($E2,$A$2:$A$12,0)):INDEX($B$2:$B$12,MATCH($E2,$A$2:$A$12,1)))>0)
/COUNTIFS($A$2:$A$12,$E2,$B$2:$B$12,INDEX($B$2:$B$12,MATCH($E2,$A$2:$A$12,0)):INDEX($B$2:$B$12,MATCH($E2,$A$2:$A$12,1))))
The idea is that you use countifs to check through each customer ID in campaign A (for example) to see if it's present in campaign B. But it's possible that a customer ID appears more than once in campaign A, so you still have to divide by the count of each customer number in campaign A to get the unique count.

Aggregating records with two main IDs in [VBA macro]

I want to make a macro in Excel that summarizes data from rows that match a composite ID generated from 2 ID columns. In my excel sheet, each row has 2 main ID columns: ID_1 is the main key, and ID_2 is a secondary key from which I only care about the first 2 letters (Which I have gotten using LEFT). I want to group rows with the same ID_1 and first 2 letters of ID_2 and report the SUM of the value, count, and sum columns.
In the example picture below, I want to turn the data in columns A:J into the data in columns M:V
So, with this example -> We have 6 records 1015 (ID_1) with 3 different ID_2 (AB, AZ, AE). I want to sum them up to a one cell each (1015 AB ; 1015 AZ ;1015 AE) with values which each record had (there is 3 records: 1015 AB with VALUE of 2,3,4 so in result I want to get just one row 1015 AB 9(sum of value) 4(sum of count) 17 (sum of(value * count)). It's important to see that this 17 dosn't come from 9 * 4. It's =sum(I4:I6) (but it may be spread out like in 1200 FF example below! I am still trying to sort them both at one time, but I cant get past it..)
Add a helper column in D to combine the ID_1 and the first 2 characters of ID_2. =A4 & LEFT(C4,2). Copy that down then go to L4 and type in:
=+INDEX($D$4:$D$25,MATCH(0,COUNTIF(L$3:L3,$D$4:$D$25),0)
and hold down Ctrl + Shift + Enter to make it an array function. Copy down to get a list of unique combinations, and then split these values into the separate columns.
Finally to pull in the numbers, put this in Q4:
=SUMIFS(E$4:E$25,$A$4:$A$25,M4,$C$4:$C$25,O4 & "*")
and then copy down and across.

How to Add Data Validation in a Column with leading Zero

For example I want the data in my Column A to be in 4digits whole number only.
1111
2222
3333
4444
0033
0032
Now, I've added a data validation in this column,
Allow = Whole Number
Min = 0
Max = 9999
Now, What if the data contains "0" at the beginning? How can I include this value?
Also, I've tried the formula below but it will not retain the leading zero.
=IF(LEN(A1),MOD(A1,1)=0,"")

Hierarchical auto-numbering (with three levels) in Excel

What if we have a three-level hierarchy and need to enumerate only "B"s (according to the pattern), but some "C-"s interfere with "B"s.
Problem: to get column B result from a given column A.
A+
B 1
B 2
С-
B 3
A+
B 1
С-
B 2
A+
С-
B 1
B 2
B 3
P.S. The task arose from the need to enumerate complex hierarchy in Excel.
Imagine that A - level 1; B - level 2; C - level 3. (with some abuse of logic in the example above as C- in the pattern goes after A, which in practice is usually not the case).
The simple case of two-level hierarchy is shown here.
Easiest would be to add in two intermediary helper columns, the first of which we'll call column C. Here, we will count only which "A+" we're on, like so [starting at C2; C1 is hardcoded as 1]:
=IF(A2="A+",A1+1,A1)
This will increment every time a new row has "A+" in column A.
Then column D will track the highest # reached so far, for that iteration in column C [starting at D2; D1 is hardcoded at 1]:
=IF(A2="A+",0,if(A2="B",B2,D1))
This will restart at 0 for each new "A+", and for each "B" it will take the value shown in column B. Then for each "C", it will simply repeat the value from the row above (the previous "B" reached).
Finally you can put in your sort, as follows [starting at E1]:
=IF(A1="B",B1,"")
This will show BLANK for either "A+" or "C", and will show the B-count if column A = "B".
I also went with a helper column (as much as I detest them personally) to show the row of each A+
Put this in C1: =ROW(A1)
Put this in C2: =IF(A2="A+",ROW(A2),C1)
It uses an expanding range with a rebasable starting point. Drag down as far as your data goes.
Put this in B2: =IF(OR(A2="C-",A2="A+"),"",IF(A1="A+",1,MAX(INDIRECT("B" & C2 & ":B" & ROW(A1)))+1))
Drag down as far as your data goes.
Hope that helps. Here are the results I received:
A+ 1
B 1 1
B 2 1
C- 1
B 3 1
A+ 6
B 1 6
C- 6
B 2 6
A+ 10
C- 10
B 1 10
B 2 10
B 3 10

Index/match with two criteria anyway to get the second set of data using unique identifiers?

I've been stuck for some time trying to match up two different data spread sheets. I'm trying to match off dollar amounts with matching names that correspond to the dollar amount. I went ahead and assigned unique identifiers to each row thinking that would help me match off the amounts, but my problem comes when there are two or more dollar amounts that are the same and have the same name.. is there anyway to get the second id to show up?
Formula sheet: (I need The second 112 to pull 113.. anyway thats possible??
A B C D E F G
IDSheett2 IDsheet1 NameSheet1 NameSheet2 Item AmountSheet1 AmountSheet2
554 112 Jim Jim Hat 25 25
555 112 Jim Jim Shoe 25 25
Formula in column B2: it should go through both sheet 1 and 2 and automacially fill in the matching id - id that matches the name and amount.
=IFERROR(INDEX(sheet1!$C$2:$C$1000,MATCH(1,INDEX((sheet1!$A$2:$A$1000=D1)*(sheet1!$B$2:$B$1000=G1),0,1),0)),"")
Data from Sheet2:
A= ID B= Name C= Item Name ( ID basically row 1 = 1, 2 = 2.. )
A B C D
554 Jim Hat 25
555 jim Shoe25
Data from Sheet1:
A= name B= amount C= assigned ID ( ID basically row 1 = 1, 2 = 2.. )
A B C
Jim 25 112
Jim 25 113
I'm also open to other ideas. Thanks for the help.
UPdated::: Based on comments
im not trying to create a database.. i have two sets of data that i'm trying to compare and match off like items(i take matched items and email it out to a group of people). i had to change some of the names because this is work related but the overall concept should remain.
it is Impossible for me to know how many names will be on incoming wires and how many names will be on my expected wires lists. I assigned unique ids per row on each page so i can do iferror/index/match to pull from a unique row to the main page.
What im trying to accomplish:
I have two sets of data: Sheet1 is incoming wires (it gives me a name and an amount) Sheet 2 is the account name, Item its for, and expected amount.
I'm trying to match the name and amount on the formula sheet but i run into the problem of two items with the same name and the same amount only pulling the first ID number it runs into and this becomes a problem because i have another vba code that will delete multiple ID's so i know i'm not counting something twice.
my final page should read:
ID sheet2 IDsheet1 Name Item(sheet2) Amount
554 112 JIM Hat 25
555 113 JIM Shoe 25
but right now the 113 id wont pull and it will just be 112 twice (which will end up getting deleted so i will miss that second match)
..+ everything works perfectly unless there are two items for the same name and the same amount +.. that is the only time i run into this problem.
Is there any code or process that can have the sheet realize that it has already used the ID of 112 once and then automatically fill in the id of 113 (so it won't get deleted by my vba code)?
In your formula sheet, B2 and copied down:
=IFERROR(INDEX(Sheet1!$C$2:$C$1000,MATCH(1,INDEX((Sheet1!$A$2:$A$1000=D2)*(Sheet1!$B$2:$B$1000=G2)*(COUNTIF(B$1:B1,Sheet1!$C$2:$C$1000)=0),),0)),"")

Resources