Excel add up values in one cell based on mutliple results from one look up value - vba

hope you are all doing well today! I've been working on a spreadsheet that tracks invoices and the hours recorded for each one. Each invoice has multiple entries with different numbers of hours for each entry, all with the same reference number which combines the project number and the invoice number. For example, for Invoice #1 of project CZ23, the reference number would be CZ23-1. Invoice 2 would be CZ23-2, and so on. This is located in Column I, while the hours are located in Column H in a sheet named "Document Data".
Now I have another sheet that tracks the hours called "Summary". What I want to do is have cell B28 of "Summary" add up all the hours from CZ23-1 from "Document Data". Since I barely know any VBA, I don't have code but I'll display the situation here:
In this case, cell B28 on "Summary" would be 15 (all the CZ23-1 hours added up: 2+5+1+7), while B29 would be 7 (all the CZ23-2 hours added up: 4+3) and so on. It doesn't necessarily have to be B28/B29, but just as a reference I've used those cells. Thank you so much for your time - hope I was able to explain everything well.

Best way to do this would be to create a pivot table.
1. Select a cell inside your data and then on the insert tab select pivotTable.
2. A "Create PivotTable" box will pop-up. Verify that your data is captured in the Table/Range. Also, you can choose if you want to have the table appear on a new sheet or the existing sheet.
3A. In the "PivotTable Fields" box, you can drag "Reference" to the Rows box and then drag "Hours" to the Values box. This should default to Sum and should add up all the Hours by Reference.
3B. Occasionally the Values box will default to Count. To change this to SUM, you can select cell B4 or B5 (See Below). Right click and select Value Field Settings. When the "Value Field Settings" box pops-up, you can select Sum on the "Summarize Values By" tab.

It sounds like you want the sql concept of 'group by' on column 'Reference'
In excel you can do this by using pivot tables
You will find pivot tables in the toolbar at Insert-->Tables-->PivotTable

Related

How to color a cell based on values and date difference in another cell using VBA

Consider I have a Table A in Excel - details as below:
And another Table B as follows
I want out-put as follows,
The Name from Table B has to be checked with Name in Table A, and respective StartDate and EndDate should be picked from Table A and same should be compared with dates in Table B and cell under that name should be colored to green if Status in Table A is in "In Progress" or to Red if in "On hold"
For Example:
Consider Jack in Table B, it has 3 records in Table A, The first start date should be picked i.e 4-Apr-2017 and End date as 27-Apr-2017, and respective coloring has to be done based on status field.
How can I achieve this using VBA/anything in Excel. I'm new to VBA.
Yes you can do it in VBA but I would recommend using conditional formatting (formula) as it doesn't require programming knowledge. To do this, you will need to employ the Vlookup formula.
I believe the output you want is something in the picture link here?
To make conditional formatting, look in the "Home" tab, under Styles -> Conditional Formatting. Highlight the cell you want to format, then select Conditional Formatting -> New Rule. This will cause a new window to pop up. Select "Use a formula to determine which cells to format".
You would want 2 conditional formats.
1.To show green if the date is within the start and end date. To do so, select the first cell in the Gantt chart (cell B8 in example) then enter formula below.
=IF(AND(B$7>=VLOOKUP($A8,$A$2:$D$5,3,FALSE()),B$7<=VLOOKUP($A8,$A$2:$D$5,4,FALSE())),TRUE(),FALSE())
2.To show red when user has item beyond due date, enter formula below with same cell highlighted.
=IF(B$7>VLOOKUP($A8,$A$2:$D$5,4,FALSE()),TRUE(),FALSE())
Once that is done, you can apply the format painter or copy and paste the cell with conditional formatting to extend your table/Gantt chart.
Note that the $ signs are important as they lock the relative reference positions in the formulas. Using the second formula as an example, B$7 refers to the date and you need the reference to be locked to row 7, where all the dates are. On the other hand, the column reference (Column B) can be shifted as you want it to change to with the columns to properly compare against the other dates.
As for multiple items per user, can you expand upon your initial question? Would you like to track based on per user or per item? I am assuming that you're creating a Gantt chart to track a project. In that case, it would make more sense to track specific tasks assigned to people. You can modify the example given to track based on task.

How to reference a specific cell location within a changing table

I am not sure how, if at all, this can be done. Basically I have a report that is generated from SalesForce for some co-workers. It exports to Excel in this nice little table. Depending on their team, they filter the results and then filter the month (fiscal year in this case).
What I want to do is have text populate at the top which is based on the text that is in the first cell in the column C (not including header row of course) when they filter. How do I go about referencing the spot where C4 is currently when they select Team "White-1" and Fiscal Year "Aug"? That cell will become hidden and I will need to pull the data from C6 which HAPPENS to be in the location that C4 was just in before being hidden.
Here is an image of the report in Excel as well as the "location" that I want to reference no matter the filtering that the employee does:
Report
I assume VBA will be needed. Preferably I want to generate the text in A1
If you are ok with adding a "Helper Column" you can do the following to get the results you are wanting:
Add a column between B and C Give it a header of some kind I used "Helper". Then in this new Column use use the formula =Row() to generate a number for each row.
You can hide this column now if you want.
Then in whichever cell you want the answer you seek enter the following formula:
=Vlookup(Aggregate(5,5,C:C),C:D,2,false)
Now you can filter all you want and you should get the result you wanted.

Linking Data from One sheet into another on DropDown selection

I am trying to create a Monthly Net Income/Expense Sheet.
I have a small manufacturing firm and based on orders we receive i want to add monthly Expense and Income.
I have created a small sheet
I have 3 sheets right now
Orders List basically has Names of all orders i receive for example Washing Unit. I have defined a name so that it will be a drop down selection as can be seen in image as Order Name
Now my Washing Unit is EXACTLY same as this sheet except it doesn't have Order Name
I want to know if I select Washing Unit from Dropdown how can I link the entire Row data into my Washing Unit Sheet
e.g if I put Description and date,income and expense and then i select Washing Unit, it should be added as an entry in Washing Unit sheet and if I select None it will be removed from there
So long as you wish to extract data from a single cell in another sheet the best way to do it should be using the INDEX/MATCH worksheet function.
Do you have some code of your own that you've tried? I suggest you try writing some then come back for help to perfect it.
Some hints:
Use the worksheet_change event of sheet "Jan".
To reliably identify rows to add or remove, you need another field in "Jan" and "Washing Unit" - let's call that "Order Number". It doesn't have to be fancy, it just has to uniquely identify each order.
You set worksheet_change to check for changes in the "Order Name" column. If it finds one, it grabs the order number from the changed row and looks for that order number in the "Washing Unit" sheet.
If "Order Name" is "None" it deletes any rows with that order number from "Washing Unit".
If "Order Name" is "Washing Unit" and the order number is not found, it finds the first empty row in "Washing Unit" and adds the order there.
Edited to add:
That should be 'If "Order Name" is NOT "Washing Unit" it deletes any rows with that order number from "Washing unit".'
Changed because it occurred to me you might have other order names and from time to time you might need to correct an incorrect order name.
But you still need to write your own code!
Without seeing the which columns you have your data under, A, B, C etc. I'm not able to create something that will definately work on your sheet but the formula below is what myself and Variatus are suggesting you use.
=IFERROR(INDEX(Jan!$A$1:$E$1000,SMALL(IF(Jan!$D$2:$D$1000="Washing Unit",ROW(Jan!$A$2:$A$1000)),ROW(1:1)),2),"")
What you can create with a formula like this is a dynamic filtered version of the data you have on your sheet Jan so that as you add "Washing Unit" entries they will be autimatically listed on the Washing Unit sheet.
The fomula is looking at the whole table of data on sheet Jan and then it is comparing the data in column D Jan!$D$2:$D$1000="Washing Unit" to see if it contains "Washing Unit". This formula would be placed at the top of sheet Washing Unit and it displays the first match ROW(1:1) and takes the data from the second column of the specified range ROW(1:1)),2),"") so you put the formula under the corresponding column in your Washing Unit sheet.
Once you have entered the formula you must use CTRL+SHIFT+ENTER instead of just ENTER to make it an array formula and add {}. You can then drag the formula down as many cells as you think you might need. (You'll see the data in the cells as you do this).
Then, go back to the top and copy the first cell over to the next column and change the number in the formula that specifies the column you want to extract data from ROW(1:1)),2),"") to 3 of the number that corresponds with your heading and again use CTRL+SHIFT+ENTER to add the curly brackets {} and auto-fill down again.
Repeat until you have all the columns you need.
It's not an easy formula and you need to remember the CTRL+SHIFT+ENTER to ensure that it works but you can have a go and see if you can get it working.

CheckBox to limit a data validation list

Moving forward, slowly.......
I added a column, that with the combination of my checkboxes, gives the name of active employees.
Screen Shot of active list of employees
If I use $T$6:$T$16 as my validation list, I still get 11 options in the drop down. I want the drop down to show only the 5 names that are in that list.
No need to use VBA here, Here's a quick solution.
Use a 1 or 0 in a column next to the employees' names corresponding to whether they are active or not.
Then create a column in the sales sheet which has a vlookup for this value
=VLOOKUP(E4,$A$4:$B$8,2,FALSE)
This looks for value E4 (the name), in range of all names, second column for their active status, FALSE for an exact match.
Then use a filter:
Highlight range of sales
"Data" tab
"Filter"
Then you can use the filter on the new Active column to only show the employees which are still active. See the linked image for the final outcome. The vlookup lives in column D.
Final outcome image in Excel
Of course if you want this in two sheets, you can split what I've done to different sheets with no extra effort other than adding sheet references to the range in the vlookup.
Hope this helps

VBA msgbox duplicate value base on 2 columns

I'm trying to get a msgbox when a value is duplicate base on 2 columns. The first column Value can be repeated but the second column will determine if it's a duplicate or not.
i.e.
Column B = Code,
Column L = Month
The user can enter the Code several times, but if he enters it on the same month I want the msgbox pop up
Is your intention to warn\inform the user? If so, I would do this without a macro. I would use conditional formatting to make the cell change color whenever the duplicate information is entered.
Create a column on your worksheet with a formula that concatenates the information in column B&L the formula would be =B1&L1 (copy this formula down the table). You can hide the column so nobody sees it. For this example, let's say you used column "M".
Select the entire Code or Month column (or both) and click the CONDITIONAL FORMATTING button on the Home tab, choose NEW RULE, USE FORMULA TO DETERMINE WHICH CELLS TO FORMAT, then enter the following formula: =COUNTIF($M$4:$M$1000,M1)>1 (note I am assuming your range of data is less than 1000 records, otherwise increase that number). Set the format to something like a red fill and instantly duplicates will be flagged. The user will also be able to quickly locate the record where this combination was already entered as that will turn red too.
If you really do want a macro to do this, you could simply write a loop to compares the active cell value of B(activerow) & L(activerow) to each previous B#&L# combination. If a match is found, use the intersect method to pop-up a the message. Here is really a good article about the intersect method: http://www.ozgrid.com/VBA/vba-intersect.htm.