Excel VBA to add unique numbers to multiple accounts - vba

I cannot get to a satisfactory answer. I need to add comments to a data input sheet. I have added a form and I am able to populate my worksheet. However, I wish to add a unique identifier (number) to each unique customer. Row a1 has customer A; row a2 has customer B; row a3 has customer A - I would like to number the rows 1, 1, 2 respectively in column b

Assuming that you have your customers in column A and your unique numbers in column B you could use the following formula for cell B2 and then copy it down:
=IFERROR(INDEX($B$1:B1,MATCH(A2,$A$1:A1,0)),MAX($B$1:B1)+1)
Cell B1 will be set to 1 or whichever the starting number should be.
The result would be something like this:

If by "add a unique identifier (number) to each unique customer" you mean that you are trying to generate a sequence number which is unique within each unique customer, (i.e. if column A contained A, B, A, C, C then you are after 1, 1, 2, 1, 2 in column B) then you can just use the formula
=COUNTIF(A$1:A1,A1)
in cell B1 and copy it down.

Related

VBA code to sum total of like account numbers

I have a code that matches 2 columns A VS C for like account numbers. If numbers match than it finds if the amounts in Column B & D match. If it does it pastes the lines of data to another spreadsheet.
I am coding a statement to find the like account numbers in column A VS C, but if the amounts don't match in column B & D than it needs to find the like account numbers in column A and sum there amounts to see if it matches the amount in column B.
How would I write the code to collect the sum of the same account numbers in column A?
If Acct = MatchAcct Then
If Amount <> MatchAmount Then
If Acct = nonmatchacct Then
(sum the values in column B for the matched account #s in column A)
The code above says if the Acct (Column A account #'s) = MatchAcct (Column C account #'s) then if Amount (Column B value) doesn't equal MatchAmount (Column D value) then the code sees if Acct (Column A account #'s) equals nonmatchacct (any other account # in Column A). If the account # matches then I want it to sum those amounts in column B.

Search a range with 3 tables according to dropdown value

I'm quite new to macros/formulae of excel. I have a sheet (Sheet1) exported which has the approvers list.
I also have 3 tables ( A, B, C)
In Sheet2 which has like below values:
Table A: {aaa,bbb,ccc}
Table B: {xxx,yyy,zzz}
Table C: {d12,e12,c12}
I need to search Column Q in sheet1 with either one table as per the dropdown menu.
If I select Table A, then I need to have all rows with text aaa OR bbb OR ccc in Column Q. I used the below formula, but I can search only one table at time.
I can't select through dropdown menu.
=SUMPRODUCT(--ISNUMBER(SEARCH(A[ID],Q51)))>0
This returns true or false. I have to keep changing the formulae for each table. Can I do that through drop down menu.
Use INDIRECT : place INDIRECT(Right(myRange, 1) & "[ID]") in the place of A[ID].
myRange is the address of the cell where the drop-down is placed; i.e. could be D1 for example, and the formula becomes:
=SUMPRODUCT(--ISNUMBER(SEARCH(INDIRECT(Right(D1, 1) & "[ID]"),Q51))) > 0

Check if one of multiple values is present in a column

I have a table in Excel 2013 that has has thousands of records of food items (Beef-frozen, beef-chilled, beef-brisket, beef-ribs, chicken-fillet, chicken-whole, fish-skinned, fish-whole, yogurt, lettuce-imported, lettuce-frozen, tomato-fresh,tomato, water, milk,...etc) stored in column A. Notice the value may contain other content than the food item name.
I created column B next to column A. I want column B to hold the category of the food item in column A. For example, if A1 has in it "Beef" or "Chicken" or "Fish" then B1 should equal "Meat". If A1 has in it "Tomato" or "Lettuce" or "Onion" then B1 should equal "Vegetable".
What is the best way to achieve it?
Assuming you have column headers, enter this formula in cell B2:
=REPT("Meat",MAX(IFERROR(MATCH({"*beef*","*chicken*","*fish*"},A2,),))) & REPT("Vegetable",MAX(IFERROR(MATCH({"*tomato*","*lettuce*","*onion*"},A2,),)))
This is an array formula and must be confirmed with Ctrl+Shift+Enter.
Now copy B2 and select B3 down as far as you need and paste.
Note: please look closely at the big gap in the middle of the formula. You'll see that this is really two separate formulas concatenated together with an ampersand. You can easily extend this formula in the same way by adding another phrase similar to the first two for a new category. In fact, you could add many more categories in this fashion.
Set up a two column table. Name it, for example FoodTable. Have the first column Named Word (for keyword) and the second column Type, for the type of product. Something like this:
Then, with your data in column A, enter the following formula in B1 and fill down:
=LOOKUP(2,1/ISNUMBER(FIND(FoodTable[Word],A1)),FoodTable[Type])
Results:

Removing duplicate values from a list if it meets conditions

I have been trying to write a for each loop to go through each row in in one sheet (sheet 2) to remove duplicates in another sheet (sheet 1). I have had no luck researching either.
In sheet 1, I have a list of customer numbers in column B with the type of product they purchased in column c and the cost of that product in column d. In another sheet 2, I have a list of customers in column a and list of products in column b.
I have been trying to write a for each loop to go through each row in sheet 2 to check the customer number and product, find all the duplicates in sheet 1 with the same customer number and product, and deleting the row with the higher balance.
Sheet 1
A(Year) B(Customer #) C(Product Type) D(Cost)
1) 2015 100 A 1
2) 2015 100 A 2
Sheet 2
A(Customer #) B(Product Type)
1) 100 A
For example, if sheet 2 had 100 in column a and A in column b, it would delete row 2.
You could try using the remove duplicates option within excel, would that solve your problem? Or is Sheet 2 update to remove certain customer orders that you would also like to remove.
Edit: To expand on this. Take the list, sort it by customer and cost (low to high). Then if you click remove duplicates, you'll have the option to select what columns to use as a basis for removing duplicates, so obviously remove cost, and then it'll clear out all but the topmost row, which as now been sorted to be the lowest value.

Searching and comparing various values using VBA

I have to create a macro which will:
Get the value from the cell A1 search this value in column C.
If the value in cell A1 exists in column C, the macro needs to be compare the value in cell B1 with values in column D.
If the value in cell A1 exists in column C AND the value in cell B1 exists in column D, then the text "Values found" should appear in cell E1.
The above needs to happen for all non empty rows in column A.
I was trying to use the following formula:
=IF(ISERROR(MATCH(A2,$C$2:$C$138,0)),"Load number not found","Load number found")
But it not working as I want. I have limited access to internet so I can't check all web sites. Would you please help me. Thanks a lot.
To check if A1 is in column C and if B1 is in column D (in the same row A1 was found in column C), then you need to do the following:
=IF(ISERROR(MATCH(A1,$C:$C,0)),"Load number not found",IF(B1=INDEX($D:$D,MATCH(A1,$C:$C,0),1),"Load number found","Load number not found"))
The first IF checks if A1 is in column C, the second IF checks if B1 is in column D (in the same row A1 was found in column C)
It will return "Load number found" only if both conditions are true. Otherwise it will return "Load number not found".
You can write a macro to do the same thing. But the easier way is to lock the cells in column E only and protect the sheet so that users will not accidentally change any of the formulas.
Update:
Since Column C can have duplicates, need to use the following array formula:
=IF(ISERROR(MATCH(1,(A1=$C:$C)*(B1=$D:$D),0)),"Load number not found","Load number found")
When you paste this formula to E1, make sure to press CTRL + Shift + Enter instead of just pressing the Enter key.
If I understand, a conventional solution with formulae is to concatenate your C and D column data and then search that. If you insert a new columnC with:
=D2&E2
copied down to suit you could apply (but say in ColumnF rather than ColumnE) your existing formula with only slight modification:
=IF(ISERROR(MATCH(A1&B1,$C$2:$C$138,0)),"Load number not found","Load number found")
subject to quite what is in which row.