Highlight exceeding limit in VBA - excel

line customer product OrderQty TotalQty
1 A 11111 5 10
2 B 11111 5 10
3 c 11111 5 10
4 A 22222 5 20
5 B 22222 5 20
6 C 22222 5 20
I have a table as shown above.
I want to to highlight the lines when OrderQty is bigger than TotalQty for a product.
OrderQty represents the number of units requested for in that order, TotalQty represents total units available to fulfil all orders.
In this example, I want to highlight lines 1,2,3 as product 11111 OrderQty 5+5+5=15 is bigger than TotalQty 10.
Is there a way to automate this in VBA? I suspect using Sumifs but I can't wrap my head around..
Thanks in advance!

You don't need VBA for this. Assuming your table above starts on row 1 with Line in column A and TotalQty in column E; put a unique list of Product in column F and in cell G2 put the formula:
=IF(SUMIF(C:C,F2,D:D)>VLOOKUP(F2,C:E,3,FALSE), "Over", "Equal or under")
The SUMIF sums OrderQty for each Product, the VLOOKUP returns TotalQty for the first instance of each Product found in the table. You can then use conditional formatting to highlight rows if required.
If you did go the VBA route, I'd probably put the table into an array, create a dictionary of Product with a value for OrderQty, and either loop on the array and sum values, or loop on the dictionary keys and call the sumif worksheet function.

Maybe something like this ?
Sub test()
Range("A:E").Interior.Pattern = xlNone
Range("C1:C7").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AA1"), Unique:=True
Set Rng = Range("AA2", Range("AA100000").End(xlUp))
For Each Prod In Rng
Set c = Range("C:C").Find(Prod.Value, lookat:=xlWhole)
Tot = c.Offset(0, 2).Value
Ord = Application.SumIf(Range("C:C"), Prod.Value, Range("D:D"))
If Ord > Tot Then
For Each cell In Range("C2", Range("C2").End(xlDown))
If cell.Value = Prod.Value Then
Range(cell.Offset(0, -2), cell.Offset(0, 2)).Interior.Color = 65535
End If
Next cell
End If
Next Prod
Range("AA:AA").ClearContents
End Sub
Or if you choose without VBA, use Conditional Formatting.
Select the range you want to be highlighted
Click Conditional Formatting ---> New Rule
Choose "Use a formula to determine which cells to format"
Type =SUMIF($C:$C,$C2,$D:$D)>VLOOKUP($C2,$C:$E,3,FALSE) in the formula box
Click Format button, then do the formatting as you want
Click OK, Click OK.

[Good day Kyle,
Is this what you are trying to do. This is only simple solution but can help you.
I used a helping column for your criteria you mentioned. I used sumifs. You can modify it to your style and needs. I used conditional formatting to highlight cells.]1

First, streamline your table, divide into two tables:
line, customer, product, OrderQty in one table.
product, TotalQty other table.
Assuming {product,TotalQty} in 'Sheet 2', and the other table in 'Sheet 1', then add a column in second table (C column in sheet 2) 'OrderQty>TotalQty' and insert formula in the cell below it, as follows:
=IF(SUMIF(Sheet1!$C:$C,$A2,Sheet1!$D:$D)>$B2,"Yes","No")
Drag or copy-paste the formula to remaining product rows.
Screenshots:

Related

Sum values in the column, if another column data are equal

I'm trying to analyze the data with >10K rows.
It contains 2 columns. 1st column has ID number. This number is repeating from row to row different times.
Columns 2 has just numbers, which I want to sum up if value in column 1 is the same.
For example: Image showing example attached.
First, what I did - is filtered from A/Z and trying with cycle find out qty of repeating items:
Private Sub CommandButton1_Click()
Dim row, B, i, col As Long
Dim H As Worksheet
Set H = Sheets("Sheet1")
H.Activate
row = H.Cells(Rows.Count, 1).End(xlUp).row
For B = 4 To row
i = 1
j = H.Cells(B, 2).Value
If H.Cells(B, 1).Value = H.Cells(B + 1, 1).Value Then
i = i + 1
.....
Result of sum I want to place in the last row where same IDs are matching. And after all, delete those not required ones.
Appreciate in advance any help on this.
If you want to delete rows after summing then VBA is best. Otherwise you can achieve out using excel formulas only. First you need to extract unique value from first column. Then you need to sum values based on unique values you extracted. So if you have Office365 then use UNIQUE() function to extract unique values like below.
=UNIQUE(A1:A14)
Then use SUMIF() to sum for these unique values.
=SUMIF($A$2:$A$14,D2,$B$2:$B$14)
You can just use a Pivot Table.
Since you will be updating/refreshing the data:
Select a cell in your data table and Insert/Table
Then, depending on your Excel version: Summarize with Pivot Table (or similar)
Note that there is no need to sort the data.
Then you merely drag
ID to the Rows area
Value to the Values area
If your values are numeric, it should default to Sum of Values
Rename / Format / decide on totals and subtotals, etc.
To refresh, the data after changing it, you merely need to select Data/Refresh, or you can create a command button to do the same.

Get average of cells conditionally in Excel 2010

I have a table in Excel with 2 columns. Column A (Owner) has a list of names. Column B (Duration) has a list of numbers. I need to get the average of numbers for each of the owner. So for James it would be (4 + 5 + 18) / 3. I know how to get the average in Excel but I don't know how to conditional say:
Use the value in Column B if value in A = "James"
A B
---------------
Owner Duration
James 4
Dan 67
Ger 3
James 5
Ger 75
James 18
The AVERAGEIF function will allow you to do this.
=AVERAGEIF(A2:A7,"James",B2:B7)
The first argument is the range which will be subject to the criteria
The second argument is the criteria.
The third argument is the range (corresponding to the first range) with values to be averaged.
There is also a SUMIF, COUNTIF, and others.
You will need to do this in a pivot table.
Select all your data.
Go to Insert >> pivot table >> click ok
Select both columns (COLA, COLB) from the list of fields
available on the right.
By default COLB will go under VALUES.
Click on the arrow that says Sum of COLB >> click on value field
settings >> Select Average function instead of sum.
Check this link https://support.office.com/en-us/article/Create-or-delete-a-PivotTable-or-PivotChart-report-d09e4d07-8cd6-4b60-afad-8fb67418800f?CorrelationId=78b545a8-649b-400a-9941-a23ef409c95b&ui=en-US&rs=en-US&ad=US#_Toc263767342 for more info on pivot tables

How do I display a number range when a number is entered?

I have a spreadsheet of clients and columns for their names, addresses, and ages. I'd like to display an age range in the column when their specific age is entered. For example, if Tina is 32, I'd like the "age" column to show "30-35" instead of 32.
Is there any way to do this?
The simplest way may be to create a lookup table in which 30-35 is in a cell immediately to the right of 30 (with the next row something like 36 and 36-40) and then rely on VLOOKUP's inexact matching, where if a value (eg 32) is not found the next lower is chosen, because of the ascending order of the table. So something like:
=VLOOKUP(32,table,2)
assuming you have the following three columns name, address, age
you could create a fourth column and copy + paste in the following formula:
=IF(ISNUMBER(C1),INT(C1 / 5) * 5 & "-" & INT(C1 / 5) * 5 + 5, "")
Once pasting the formula into the new column you can drag-down to extend it for all rows. You can then right click on the header of the age column and select Hide to be left with only the age range.
The above formula assumes column C is the age, and will display the age range only if a number is entered.

Format Cells from 2 sheets, based on 4 column comparison

I need to find changed values in dataSet.
So I have 2 sheets with, in general, same data.
I have two columns: docID(which is unique key) and rev(which is not unique).
I need to find(highlight/display only/...) those rows, where for same docID from first sheet rev is different in the second sheet.
UPDATE 1
Sheet 1:
docID rev
1 5
2 6
3 1
Sheet 2:
docID rev
1 6
2 6
3 1
Only the first row makes an interest to me, as others did not changed.
UPDATE 2
simple if statement which should give some understanding what I need. In general this is done on similar test data set, so cells are incorrect.
=IF(NOT(AND($B6=Sheet1!$B6;$C6=Sheet1!$C6));1;0)
So values are NOT equal.
You can do this with a custom formula in Conditional Highlighting:
Select the top cell in the column docID column, then select the whole column (in the example, I assume that this is column A and you select A1 and the rev is in column B, same structure in both sheets. Adjust as to your needs)
Goto home->Conditional Formatting->New rules->Use formula to determine which cells to format and enter the following formula:
=VLOKUP(A1,Sheet1!$A:$B,2,0)<>B1
Click on format and select the format you need.
Done!

Find values from one excel sheet in another

I have a column with the values in A excel sheet 1 1 1 2 2 2 3 3 3 4 4 4.... and i have in B excel sheet another column with values 1 2 4 ...., what i want is read values from B and see if they are in A sheet, for example if value 2 is in B sheet, then write true in a new column in sheet A in front of 2, and similarly false or nothing in front of value 3.
thanks
You can use a simple VLOOKUP - For example, assuming that the content of cell A1 of sheet B is 2, and that the sheet you call A is called SheetA, you can put the following formula in cell B1:
=IF(ISERROR(VLOOKUP(A1,SheetA!A:A,1,FALSE)),"",VLOOKUP(A1,SheetA!A:A,1,FALSE))
Use the approach described here:
http://spreadsheetpage.com/index.php/tip/comparing_two_lists_with_conditional_formatting/
Key formula is this: =COUNTIF(OldList,D2)=0, which you can use within the conditional formatting context as described, or to generate your true/false indicators as you mention in your question, i.e.:
=IF(COUNTIF(OldList,D2)=0,FALSE,TRUE)
OldList is just a range, and you don't need to use a named range. But if you don't name the range, just be sure to use absolute references for the range you're searching against.
Do you want a cool formula you can use to count the number of each matching value. Try this for your original post:
=IF(SUMPRODUCT(--($A1 =Sheet1!$A:$A) > 0), "True", "False")
And this to count the values: =SUMPRODUCT(--($A1 =Sheet1!$A:$A))

Resources