VBA msgbox duplicate value base on 2 columns - vba

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.

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

How to substract two cells of x row on excel

I'm making an Excel sheet to keep track of some activities. The thing is that I have 2 cells that are date type; I want the third cell to subtract the them to get the time that the x person spent on the activity.
I know that if I type =A2-A1 it's going to give me what I want, but, since its going to be a big Excel sheet with lots of records, I don't want to input the same formula for each row just changing the row number.
Is there a way to make Excel detect the row that the user is inputing data in and then make the requested formula to get the time?
you can turn your data range into a table by highlighting the range and going to the insert tab and clicking table. Then when you type the formula into the first cell and click in the cells when selecting instead of typing it out, you will notice that it is using the column names instead, also it will automatically fill the column with the new formula. That would be my suggestion.