Excel VBA Lookup Methods - excel

I have an issue that I've been scratching my head at; I've looked into the Index:Match lookup method, and V/HLookup, but I'm not sure these will help just yet. Here's what's happening:
I have two worksheets in excel-2007. One has a Customer ID column (which does and will have duplicate ID's in the instance that the customer did "x activity" more than one time in a month) and then the date that this happened in another column. The second sheet is for giving an overview of a specific day, IE what happened on 7-1-13.
The issue is that my raw data sheet is sorted via the first of the month descending, so 7-1,7-2,etc, and when I run the Vlookup, if a Customer ID has a record on 7-2 and on 7-15, the VLookup will pull data only from the 7-2 (first) row. Has anyone experienced this and found a workaround?
My current workaround would be to make either a new table for each day's data, or instead of using my VLookup of =Vlookup(A2, 'Data Sheet' A:D, 4, 0) to give the columns row numbers, like =Vlookup(A2, 'Data Sheet' A$1:D$30, 4, 0). This is a daily report, and that would be intense. Please help if you can!
(Another side note, I have one main sheet for the view, one data sheet where it's all collected, and then 30 sheets, one for each day of the month, this case being July). For each sheet, I've named them the day of the month, so I'm reflecting the data as such:
Sheets("7-1-13") has data from the 1st on it. The Data Sheet in it's entireity has data from 7-1-13 to 7-31-13. I need to reference ID's on the 1st to the data for the 1st and the 1st only.
I want to use something like this, but I'm having a hard time with it
=Vlookup(A2, 'Data Sheet', A:D (ONLY IF THE CREATE DATE OF THIS ITEM IS 7-1), 4,0)`
but of course it's not that easy :p

This may not give you your results in a format you like and still requires a bit of manual work, but without going the route of macros, I think this will get you one step closer. I thought of using an array formula to get all the IDs by a specific date.
Example:
A B
ID Date
1 5 7/1/2013
2 2 7/2/2013
3 5 7/3/2013
In this situation, I assume you want 5 from the first row to appear on your 7/1 sheet, 2 to appear on your 7/2 sheet, and 5 from the third row to appear on your 7/3 sheet
on your 7/1 sheet. you'll need to select the number of blank rows that matches your raw data (using the example above, you would be selecting A1:A3 on your 7/1 sheet). Once you have your cells selected, then enter the following formula in the formula bar and press Ctrl+Shift+Enter. This is what makes the formula an array formula.
=((Raw_DataSheet!B1:B3=DATE(2013,7,1))*1)*Raw_DataSheet!A1:A3
What this formula does is looks at all the dates in B1:B3 and finds the ones that equal 7/1/2013. Since you're using an array formula, this gives you the array {TRUE,FALSE,FALSE}. Multiply this by 1, and you get the array {1,0,0}. You now have an array that has a 1 for each row of B1:B3 that was equal to 7/1/2013. This array {1,0,0} is then multiplied by your Customer IDs {5,2,5}
5 * 1 = 5
2 * 0 = 0
5 * 0 = 0
So now your entire formula is equal to the array {5,0,0}. Since you selected A1:A3 on your 7/1 sheet, the values that should appear should be
A
ID
1 5
2 0
3 0
From here, you can always filter out the 0's and you'd just have a list of all the IDs that had the date of 7/1 from your Raw Data Sheet. You would also then replicate this for each of your sheets and just change the date in the formula...Yes, I know, way more complicated than you probably wanted but it's what I came up with!

Related

Single formula with Multiple Countifs and where data table contains duplicate values in more than one column

The excel file contains a structure table. (which I find difficult to work with but I can't alter the file structure, I am just adding a summary tab with various counts from the data table.)
Item
Month
1
2
2
2
2
2
2
3
3
3
3
4
The item number itself can repeat and be repeated within the same month.
I was able to get a count of total number of unique items using this formula.
=SUMPRODUCT((Item_tab[Item]<>"")/COUNTIF(Item_tab[Item],Item_tab[Item]&""))
Looking for how to count the number of unique items within a specific month.?
Example of Desired output.
Edit/Set formula for Month 2 , output should be 2
Edit/Set formula for Month 3 , output should be 2
Edit/Set formula for Month 4 , output should be 1
I cannot figure out how to apply countifs to something that needs to avoid duplicates in multiple columns.
Tried this but returns #VALUE!
=SUMPRODUCT((Item_tab[Item]<>"")/COUNTIFS(Item_tab[Item],Item_tab[Item]&"",'Item'!M:M,6))
The month column was added later and I don't know how why or how to get that column to have the structured table column Header like everything else.
The sheet name is Item
The Table Name is Item_tab
I can find where to rename the table but not the columns. Not sure the variance is causing an issue. The month column is absolutely part of the table in every way that I can see.
You can try the following in cell D2:
=LET(A, A2:A7, B, B2:B7, uxB, UNIQUE(B), cnts, BYROW(uxB, LAMBDA(u,
ROWS(UNIQUE(FILTER(A, B=u))))), HSTACK(uxB, cnts))
Here is the output:
If you want to get the result repeated instead of consolidating the result by unique months, you can try the following:
=LET(A, A2:A7, B, B2:B7, uxB, UNIQUE(B), BYROW(B, LAMBDA(u,
ROWS(UNIQUE(FILTER(A, B=u))))))
If you don't have such function available, you can try the following formula in D2, it produces the same result as the first formula:
=CHOOSE({1,2}, UNIQUE(B2:B7),
MMULT(N(MMULT(TRANSPOSE(N(B2:B7=TRANSPOSE(UNIQUE(B2:B7)))),
N(A2:A7=TRANSPOSE(UNIQUE(A2:A7))))>0), SEQUENCE(ROWS(UNIQUE(A2:A7)),,1,0)))
or using LET for a better reading and maintenance:
=LET(A, A2:A7, B, B2:B7, CHOOSE({1,2}, UNIQUE(B),
MMULT(N(MMULT(TRANSPOSE(N(B=TRANSPOSE(UNIQUE(B)))),
N(A=TRANSPOSE(UNIQUE(A))))>0), SEQUENCE(ROWS(UNIQUE(A)),,1,0))))

All combinations of 4 out of 7 columns with totals using excel

I have 7 columns to choose from and I need to pick 4 of those columns and generate a total for each row. I also need every combination of 4, which means I'll have 35 new columns with the totals for each of those combinations showing in each row. I need the code for this and if it can be done only using Excel. Here is an image of the columns and the grayed ones are the 7 columns I'm talking about. My knowledge of Excel is very limited. There are over 1,500 rows if that matters.
multi step approach that is going to use some helper rows. there may be a more elegant formula that will do this, and much slicker options in VBA, but this is a formula only approach.
Step 1 - Generate List of Column Combination
To generate the list 4 helper rows will need to be insert at the top of your data. either above or below you header row. These 4 rows will represent the column number you are going to pick. To keep the math simpler for me I just assumed the 1 for the first column and 7 for the last column. those numbers will get converted to later to account for column in between in your spreadsheet. For the sake of this example The first combination sum will occur in column AO and the first helper row will be row 1. The first combination will be hard coded and it will seed the pattern for the remainder of column combinations. Enter the following values in the corresponding cells:
AO1 = 1
AO2 = 2
AO3 = 3
AO4 = 4
In the adjacent column a formula will be placed and copied to the right. It will automatically augment the bottom value by 1 until it hits its maximum value at which point the value in the row above will increase by 1 and the the value of the current will be 1 more than the cell above. This will produce a pattern that covers all 35 combinations by the time column BW is reached. Place the formulas below in the appropriate cell and copy to the right:
AP1
=IF(AO2=5,AO1+1,AO1)
AP2
=IF(AO2=5,AP1+1,IF(AO3=6,AO2+1,AO2))
AP3
=IF(AO3=6,AP2+1,IF(AO4=7,AO3+1,AO3))
AP4
=IF(AO4=7,AP3+1,AO4+1)
Step2 - Sum The Appropriate Columns
I was hoping to use a some sort of array type operation to read through the column reference numbers above, but I could not get my head around it. Since it was just 4 entries to worry about I simply added each reference manually in a SUM function. Now the important thing to note is that we will be using the INDEX function over the 13 columns that cover the range of your columns so to convert the index number we figured out above, to something that will work to grab every second row, the number that was calculated will be multiplied by 2 and then 1 will be subtracted. That means 1,2,3,4 for the first column combination becomes 1,3,5,7. You can see this in the following formula. Place the following formula in the appropriate cell and copy down and to the right as needed.
AO5
=INDEX($AB5:$AN5,AO$1*2-1)+INDEX($AB5:$AN5,AO$2*2-1)+INDEX($AB5:$AN5,AO$3*2-1)+INDEX($AB5:$AN5,AO$4*2-1)
pay careful attention to the $ which will lock row or column reference and prevent them from changing as the formula is copied.
Now you may need to adjust the cell references to match your sheet.

Fill in table based a column of categories in Excel

I have a table that looks like this:
Type Value
Movie 5
Food 3
Gas 10
Food 2
.... ....
And There's a second table I want to fill in with "Value" based on their type in the first table, so that the corresponding rows look like this:
Rent Food Movie Gas Clothing ... ( appear in specific order bc they are subcategories)
5
3
10
2
The title row is already there, so I was thinking there might be some kind of lookup method to do this? How do I do that?
your second table apperas to hold one value per row but it doesn't have a label. it does correlate to the original row number, is this by design or coincidence?
if this is by design then you can use those 2 columns, hide them if you like, get a unique list of categories by copying you r abels to a new colum, removing duplicates in the data tab, then paste special transpose in c1 to create colum headers.
so column a and b remain unchanged
row 1 contains header starting at column c
your data starts at c2
this is the formula
=Iferror(vlookup(C$1,$A2:$B2,2,false),"")
drag it down and to the right
you can copy paste special values when done to remove the formulas
for something with only a hundred or thousand cells this will be one of the easier options but i would not do this on large tables, for those i would use power query or VBA
Assuming your 1st table is in Sheet1 and 2nd table is in sheet2.. you may try to fill in Sheet2!A2
=IF(Sheet1!$A2=A$1,Sheet1!$B2,"")
and drag it all the way.. Hope you get how it works.. and what you need.

Transfer data from one worksheet to the next based on cell values

I had trouble titling this, and I think it is better explained with examples. I am not an extremely experienced excel user, but was asked to figure this out.
Worksheet 1 (delivered by software) is formatted like this:
12/17/2013
Hour Delivered
00.00-00.59
Employee 1 18
Employee 2 17
Total For Hour 35
01.00-01.59
Employee 1 18
Employee 2 17
Employee 3 12
Total For Hour 47
... etc until hours 24.00-24.59
The number of employees in the group per hour is different each day, so i don't think that I can just simply reference the cells.
The worksheets that I want to transfer the data from worksheet 1 to are based on date, so there is one for each day. (12/17 worksheet, 12/18 worksheet, etc...)
And this is the format of the date worksheets:
Employee 00.00-00.59 | 01.00-01.59 | etc. until hours 24.00-24.59
Employee 1 18 18
Employee 2 17 17
Employee 3 12
Employee 4
Employee 5
So basically I need the data from worksheet 1 transferred over to the individual date worksheets. I believe, the amount of employees being different for each hour/day makes this difficult. Does anyone here have any ideas of how this can be accomplished?
Also, if there are any questions, please let me know.
This should be possible without any VB in two steps. First step is to add further columns to Worksheet 1 that normalise the data. Second step is to create a pivot table using that normalised data.
By "normalise" I mean add columns in Worksheet 1 for Date, Hour, Employee and Delivered using formulae that copy values from your existing columns A and B. Let me know if you need more help with that.
Edit: adding details ...
Suppose Worksheet 1 has the values you indicated in column A and B, and that you want Hour in column D. Suppose row 1 just contains column headings. Leave row 2 totally empty. The formula in col D needs to say "If the value in col A looks like an hour, then copy it, otherwise repeat the hour from the line above." A simple way to determine if a row in Worksheet 1 is an Hour is to look for a decimal point in position 3. So put =IF(MID(A3,3,1)=".",A3,D2) in cell D3 and copy that formula down.
I'm sure you can construct a similar formula for the Date, Employee and Delivered columns.
Maybe add a condition to the formulae to say "If the value in col A starts with 'Total' then leave the cell empty".
If there aren't a lot of data and the setup is exactly how you've displayed them above, a simple formula can solve this.
Assuming that my raw export is in sheet Base, cell A1 and my intended output starts in sheet Output, cell A1, you can use this formula on cell B2:
=IF(INDEX(Base!$A:$A,MATCH(B$1,Base!$A:$A,0)+ROW(1:1),0)=$A2,OFFSET(INDEX(Base!$A:$A,MATCH(B$1,Base!$A:$A,0)+ROW(1:1),0),0,1),0)
The premise is simple. It locates the correct hour using INDEX + MATCH, then OFFSETs it by the correct number of ROWs. This is assuming, of course, that your employees are in the same location under every hour (ie Employee 1 is always one (1) row below the hour, Employee 2 is always two (2) rows below, etc). We also add a check if the employee name matches so that it will return 0 if the employee is not there.
Here are some screencaps:
Sample data:
Output:
Of course, this is just the basic premise. If you have multiple dates in a sheet, it's just a matter of manipulating this formula further. Off the top of my head, locating the correct date and adding the correct offset can also work.
Let us know if this helps or if a VBA or the Pivot option seems best.

Excel count unique occurrences of a text entry based on a status contained in a seperate column

Alright, this is driving me insane...
I have a section of data in a spreadsheet that looks like this:
Column A Column B Column C
lksdf-46-we-32 Fire 1
lksdf-46-we-32 Fire 2
lksdf-46-we-32 Fire 3
lksdf-46-we-32 Fire 4
wgw3f-18-bw-11 Ice 1
wgw3f-18-bw-11 Ice 2
wgw3f-18-bw-11 Ice 3
wgw3f-18-bw-11 Ice 4
possf-12-he-91 Fire 1
possf-12-he-91 Fire 2
possf-12-he-91 Fire 3
possf-12-he-91 Fire 4
oiwen-20-lw-93 Water 1
oiwen-20-lw-93 Water 2
oiwen-20-lw-93 Water 3
oiwen-20-lw-93 Water 4
In another spreadsheet, named 'Variables', I have a lookup category that looks something like this:
Column A
Fire
Water
I need to find the number of distinct entries in column A of the raw data sheet where column B matches any entry in column A of the Variables sheet. What I'm looking for is an excel formula, but everything I've tried either returns duplicates (as a starting point) or returns 0. Also, could you please explain in detail how the query works in excel? I'm a fairly experienced programmer, but I'm having a heck of a time wrapping my head around these functions in excel that I've been tasked to finish by the end of the day.
Try this "array formula" somewhere in the raw data sheet
=SUM(IF(FREQUENCY(IF(ISNUMBER(MATCH(B2:B100,Variables!A:A,0)),IF(A2:A100<>"",MATCH(A2:A100,A2:A100,0))),ROW(A2:A100)-ROW(A2)+1),1))
confirmed with CTRL+SHIFT+ENTER
The formula uses FREQUENCY function, with the "bins" being the row numbers, and counts bins that have 1 or more entry. Entries are only made when the column B item matches Variables column A.....and the second MATCH function ensures that the same row number (the first match) is entered for each repeated item in column A, which guarantees that duplicates are not counted
This formula looks at 100 rows of data in raw data sheet, increase as required but note that formula is very "expensive" so may prove impractical with very large datasets

Resources