I need a macro to evaluate data in excel while counting by 10 - excel

I have data in 3 columns lets call them A, B, and C. All three columns are exclusively numbers. I need to look at column A and find all numbers that are between X-Y. For example I need all numbers between 95-104, these numbers can all be grouped as 100, I then need the next 10 so 105-114 and all those numbers can be group 110. Once I have the numbers in the group I need to find the average of group 100 in columns B and C. My final chart will read column A group, then B will be average of that group and C will be the other column average.

You can use simple loop.
with TAB1
for i = 1 to .Cells(.Rows.Count, "A").End(xlUp).Row
if cells(i,1) > Y and cells(i,1) < X then _
.EntireRow.Hidden = True
Else .EntireRow.Hidden = False
next
Then you can use subtotal excel function for average. Not tested it is little help I cant run excel on my mobile =)

Related

VBA Create filter for multiple columns

good night!
I'm new in VBA and my english isn't that good so, please, be patient with me =D
Since I'm new, I always did simple loops, with one column. I never had to do a filter before. So I confess I have no idea where to begin. But I'll try my best to explain, so please, help me because it's really important to me.
I need to create a loop in the first 5 columns with this logic:
1 - Search all 5 columns and create a filter;
2 - Filter: Get all rows of column A where value == 1 And all rows of column B where value == 1 And all rows of Column C where variable == 'C' And all rows of column D where values == 1 And all rows of column E where value == 1 and create another sheet(1) with all rows of the filter;
3 - Do loop 2 again, but with "J" on column "C", and create another sheet (2);
The logic is: I need to create one sheet with all the rows that I set in the filter.
Example: Find all rows on the Columns where Column A = 1 & Column B = 1 & Column C = F & Column D = 1 & Column E = 1 -> Create one sheet with all rows with this values
Just like that:
Note this: because I have Column C with "F" in a row and "J" in another row, I need to create one sheet with all rows that contains each letter.
Could you help me, please?
Thanks and have a great night!

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.

Highlight exceeding limit in VBA

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:

Summing up a column based on multiple Criterion

Basically
Column 1 contains many names,
Column 2 contains the Unique version of those many names,
Column 3 contains either 1 or 0
I want to sum the values in Column 3 based on Column 1 matching Column 2
EG
A B C
VBA VBA 1
VBA XY 0
XY ZX 1
ZX 1
XY 1
VBA 0
XY 1
ZX 1
So I want it to produce a 4th column equal in length to Column B as follows
VBA 1
XY 3
ZX 2
I've tried a few different ways and I just cant seem to get it to work.
Any help would be much appreciated
You can use
=SUMPRODUCT(($C$1:$C$8)*($A$1:$A$8=B1))
Drag/Copy down as required. Change range as needed. See image for reference.
Anyone looking for another method aside from the above SUMIF method:
If its all on the Same sheet:
Cells(I, "C").Value = Application.CountIfs(Range("B:B"), Cells;
(I, "A").Value, Range("D:D"), "1")
If its on multiple sheets:
Sheets("SheetName").Cells(I, "C").Value =
Application.CountIfs(Sheets("SheetName").Range("B:B"), Sheets("SheetName");
.Cells(I, "A").Value, Sheets("SheetName").Range("D:D"), "1")
There is probably a neater way of doing it. You could set a variable for the sheet names rather than typing Sheets("Sheetname") over and over.

Any ideas on how to turn certain cells absolute?

Here is my spread sheet:
What I'm trying to do is break down my total weight into individual weights, to do this I have to make total weight absolute so I can multiply my % ratios. Only problem is some skus have 6 items some have 5 items so I can't just set every X rows make absolute. essentially I want to do this.
P.S. I have about 5000 rows so it'd take a really long time to manually do it. Wondering if there's any solutions thanks.
You could use nested if functions, it's dirty but it works:
The function goes in J column
Check if the associate H column is empty
If it is empty return empty to keep the spreadsheet clean
If it is not empty check the row above in I column for empty
If it is not empty that the total weight to multiply the ratio by the total weight
If it is empty check the row above that
And repeat as necessary.
I wrote this function that checks 5 deep I think? You could keep nesting it to check 10 deep, 20 deep, etc., until you reach the nested IF limit in excel which I think is 255 so that it adapts to your varying needs in the items list.
=IF(H7<>"",IF(I6<>"",H7*I6,IF(I5<>"",H7*I5,IF(I4<>"",H7*I4,IF(I3<>"",H7*I3,IF(I2<>"",H7*I2))))), "")
Not sure if you just want the J column filled based on the H column, but here's my suggestion. I tend to use hidden columns as they keep my formulae simple but you can incorporate the formula in my G column (the column I'ld hide) into my formula in the F column.
Results Shown
Formulae Shown
P.S. I need reputation as I need to comment on another post.
Sryn
You could use a hidden column with nested If statements, formula goes in Column K:
Check if Column F is blank
If it is not check if the row above is blank
Repeat
Once the row is blank you know you are on the row with the total weight
Set the Column K cell to that value (the I column)
For column J:
If Column K is 0 then return empty
If not multiply Column H by Column K
The formula for Column K is:
=IF(F22 <> "", IF(F21 <> "", IF(F20 <> "", "", I20), I21)) - just add the required amount of nesting, for your case 6 nests is enough
The formula for Column J is:
=IF(K22 = 0, "", K22 * H22)
And then just right click column K and choose to hide that column. Of if you use column K in another part of your sheet just select any unused column.

Resources