Check value above to identify the same value as one item - vba

I have a dataset that looks like this:
ID Item No of items No of items ordered
1 Vauxhall
2 Vauxhall 40
3 Vauxhall 30
4 Vauxhall 60
6 Vauxhall
7 Vauxhall 40
8 Vauxhall 25
9 Vauxhall
10 BMW 30
11 BMW
12 BMW 25
13 BMW
14 BMW 55
15 BMW
16 BMW
17 BMW 20
18 BMW
Each item has nine rows. I would like VBA code or a formula to look at the 'Item' above active cell and if the 'Item' is the same and the 'No of Items' field is blank, I would like it to copy the next available value in 'No of Items' to the cells above which are empty. If there is no next available 'No of Items' for one item then put default value of 100.
I would like the result to look:
ID Item No of items No of Items ordered
1 Vauxhall 40
2 Vauxhall 40
3 Vauxhall 30
4 Vauxhall 60
6 Vauxhall 40
7 Vauxhall 40
8 Vauxhall 25
9 Vauxhall 100
10 BMW 30
11 BMW 25
12 BMW 25
13 BMW 55
14 BMW 55
15 BMW 20
16 BMW 20
17 BMW 20
18 BMW 100

If ID is in A1 then you might try in D2 copied down to suit:
=IF(B1=B2,"Do something","Do something else")
The general principle is the same for VBA (say If ... Else) but VBA has much more scope for what to do or what else to do.
Concerning your edit you'd need to adapt the above formula so it differentiates the last in each group, rather than the first, say in E2 copied down to suit:
=B2<>B3
Filter ColumnE to select TRUE and filter ColumnC to select (Blanks). Fill those with 100 and then fill ColumnC by selecting it, Go To Special..., Blanks (only), =, Down, Ctrl+Enter (see).

Related

How to group merge columns based on one row identifier with pandas?

I have a dataset, in which it has a lot of entries for a single location. I am trying to find a way to sum up all of those entries without affecting any of the other columns. So, just in case I'm not explaining it well enough, I want to use a dataset like this:
Locations Cyclists maleRunners femaleRunners maleCyclists femaleCyclists
Bedford 10 12 14 17 27
Bedford 11 40 34 9 1
Bedford 7 1 2 3 3
Leeds 1 1 2 0 0
Leeds 20 13 6 1 1
Bath 101 20 33 41 3
Bath 11 2 3 1 0
And turn it into something like this:
Locations Cyclists maleRunners femaleRunners maleCyclists femaleCyclists
Bedford 28 53 50 29 31
Leeds 21 33 39 1 1
Bath 111 22 36 42 3
Now, I have read up that a groupby should work in a way, but from my understanding a group by will change it into 2 columns and I don't particularly want to make hundreds of 2 columns and then merge it all. Surely there's a much simpler way to do this?
IIUC, groupby+sum will work for you:
df.groupby('Locations',as_index=False,sort=False).sum()
Output:
Locations Cyclists maleRunners femaleRunners maleCyclists femaleCyclists
0 Bedford 28 53 50 29 31
1 Leeds 21 14 8 1 1
2 Bath 112 22 36 42 3
Pivot table should work for you.
new_df = pd.pivot_table(df, values=['Cyclists', 'maleRunners', 'femalRunners',
'maleCyclists','femaleCyclists'],index='Locations', aggfunc=np.sum)

Need to Roll Parent Quantities to Calculate Total Quantity

Thanks in advance for any assistance you can provide. I have spent hours on this with no luck.
I'm working with an indented bill of material table which has an end part id, a sequence number, a level and a quantity. The goal is to determine the total cumulative quantity of each row on this table.
What makes this difficult is that to determine the total quantity, the child quantity needs to be multiplied by it's parent quantity. The parent quantity of that parent needs to be multiplied by it's parent quantity and so on.
For example, a level 3 part has a component quantity of 5. It's parent the level 2 part has a component quantity of 2. It's parent the level 1 part has a component quantity of 3. This means the level 3 part total quantity is 30 (3 X 2 X 5).
With the assistance of SO (specifically #KKK) the parent quantity was able to be calculated in the below query. After this was resolved I realized I now need two additional columns, one that shows the Rolled/Cumulative quantities of the parent rows and one that shows the total quantity of the child.
The attached screenshot has the two columns that need to be added highlighted in yellow. Here is the current SQL (using Oracle 10.2) for the columns that are not in yellow:
select
end_part_id, sort_seq_no, indented_lvl, comp_qty,
(select distinct first_value(a.comp_qty) over (order by a.sort_seq_no desc, TRIM(a.indented_lvl) desc)
from
report_table a
where
a.end_part_id = b.end_part_id
and a.sort_seq_no < b.sort_seq_no
and TRIM(a.indented_lvl) < TRIM(b.indented_lvl)) as "PARENT_QTY"
from report_table b
Expected Results
END_PART_ID SORT_SEQ_NO INDENTED_LVL COMP_QTY PARENT_QTY ROLLED_PARENT QTY TOTAL_QTY
PARTX 1 1 2 1 1 2
PARTX 2 2 5 2 2 10
PARTX 3 3 2 5 10 20
PARTX 4 4 1 2 20 20
PARTX 5 5 1 1 20 20
PARTX 6 6 1 1 20 20
PARTX 7 5 4 1 20 80
PARTX 8 6 1 4 80 80
PARTX 9 2 7 2 2 14
PARTX 10 3 2 7 14 28
PARTX 11 3 2 7 14 28
PARTX 12 4 1 2 28 28
PARTX 13 4 1 2 28 28
PARTX 14 3 8 7 14 112
PARTX 15 1 1 1 1 1
PARTX 16 2 7 1 1 7
PARTX 17 3 2 7 7 14
PARTX 18 3 2 7 7 14
PARTX 19 4 1 2 14 14
PARTX 20 4 1 2 14 14

vba sort loop requires rank

I have a spreadsheet that has student test scoresĀ  from each week.It has 5 fields,name(column A),year(column B),level(column C),week(columnD) and score(ColE).Each student have their own block on the sheet and each block of rows is seperated by and empty row.The blocks of rows will vary in size.(please see below).
I have code that will order the score(column E) from highest to lowest,(see before sort and after sort)
What I would like to do is to insert another column between the week column and the score column that gives the position of each score after the sort,and it would appear as it is in Fig 3 below .I think it would require some sort of RANK procedure and a loop.Notice also that some times the scores by a student for certain weeks can be the same so then there will be a joint top(or second or third etc)as with John Ellis joint 4th with two sets of 54 and phil simm who has a joint 1st and 4th.
Hope this makes sense.Any help much appreciated
At the bottom the spreadshhet figures I have also placed the code that I used for looping and sorting column E the score column.
BEFORE SORT(Fig1)
name year level week score
jill evans 5 2 10 56
jill evans 5 2 11 49
jill evans 5 2 12 77
jill evans 5 2 13 84
empty empty empty empty empty
john ellis 3 4 10 45
john ellis 3 4 11 54
john ellis 3 4 12 54
john ellis 3 4 13 29
john ellis 3 4 14 66
empty empty empty empty empty
phil simm 4 6 10 89
phil simm 4 6 11 76
phil simm 4 6 12 41
phil simm 4 6 13 41
phil simm 4 6 14 56
phil simm 4 6 15 59
phil simm 4 6 16 61
phil simm 4 6 17 61
AFTER SORT(Fig2)
name year level week score
jill evans 5 2 11 49
jill evans 5 2 10 56
jill evans 5 2 12 77
jill evans 5 2 13 84
empty empty empty empty empty
john ellis 3 4 13 29
john ellis 3 4 10 45
john ellis 3 4 11 54
john ellis 3 4 12 54
john ellis 3 4 14 66
empty empty empty empty empty
phil simm 4 6 12 41
phil simm 4 6 13 41
phil simm 4 6 14 56
phil simm 4 6 15 59
phil simm 4 6 16 61
phil simm 4 6 17 61
phil simm 4 6 11 76
phil simm 4 6 10 89
FIG3 with the position row included between week col and score col
name year level week position score
jill evans 5 2 11 1 49
jill evans 5 2 10 2 56
jill evans 5 2 12 3 77
jill evans 5 2 13 4 84
empty empty empty empty empty empty
john ellis 3 4 13 1 29
john ellis 3 4 10 2 45
john ellis 3 4 11 3 54
john ellis 3 4 12 3 54
john ellis 3 4 14 4 66
empty empty empty empty empty empty
phil simm 4 6 12 1 41
phil simm 4 6 13 1 41
phil simm 4 6 14 2 56
phil simm 4 6 15 3 59
phil simm 4 6 16 4 61
phil simm 4 6 17 4 61
phil simm 4 6 11 5 76
phil simm 4 6 10 6 89
So the position column reflects the new position of the score after the sort.
If two scores are the same then that will be a joint position,as with John Ellis joint 4th with two sets of 54 and phil simm who has a joint 1st and 4th.
Hope this nakes sense.Any help much appreciated
Sub sortone()
Application.ScreenUpdating = False
Dim Area As Range, sr As Long, er As Long
For Each Area In Range("A2", Range("E" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeConstants).Areas
With Area
sr = .Row
er = sr + .Rows.Count - 1
Range("A" & sr & ":E" & er).Sort key1:=Range("E" & sr), order1:=1
End With
Next Area
Application.ScreenUpdating = True
End Sub
many thanks
"RankIf" conditional rank by Subsets using SUMPRODUCT:
An alternate way to rank, kind of like if there were a RANKIF function, uses SUMPRODUCT to do conditional ranks:
Formula in D5:
=1+SUMPRODUCT((A$4:A$21=A5)*($B$4:$B$21>B5))
...the absolute/relative cells references are setup to allow the formula to be copied or filled down & right.
More Information:
Office.com : SUMPRODUCT Function (Excel)
Office.com : RANK Function (Excel)
MSDN : WorksheetFunction.Rank Method (VBA/Excel)
Office.com : Excel statistical functions: Representing ties by using RANK
you could try this code
Option Explicit
Sub sortone()
Dim Area As Range
Application.ScreenUpdating = False
With Range("A1", Range("A" & Rows.Count).End(xlUp))
.Columns(5).Insert
.Cells(1, 5).Value = "position"
For Each Area In .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeConstants).Areas
With Area
.Resize(, 6).Sort key1:=.Range("F1"), order1:=1
.Offset(, 4).FormulaR1C1 = "=RANK(RC[1]," & .Offset(, 5).Resize(, 1).Address(, , xlR1C1) & ",1)"
End With
Next Area
End With
Application.ScreenUpdating = True
End Sub
name year level week position score
john ellis 3 4 13 1 29
phil simm 4 6 12 2 41
phil simm 4 6 13 2 41
john ellis 3 4 10 4 45
jill evans 5 2 11 5 49
john ellis 3 4 11 6 54
john ellis 3 4 12 6 54
jill evans 5 2 10 8 56
phil simm 4 6 14 8 56
phil simm 4 6 15 10 59
phil simm 4 6 16 11 61
phil simm 4 6 17 11 61
john ellis 3 4 14 13 66
phil simm 4 6 11 14 76
jill evans 5 2 12 15 77
jill evans 5 2 13 16 84
phil simm 4 6 10 17 89
empty empty empty empty #VALUE! empty
empty empty empty empty #VALUE! empty

How to calculate maximum of three in combined query

usage
The image shows works that I have done in access. The query "overall usage review" count the number of usages in each month by using combined query.
After this step, I want to show the maximum three of usage.quantity among 12months and calculate with the formula ( E.g., "Total MAX three months usage"/3)
E.g.,
Warehouse Part Number ........................................................
A X01 9 16 7 14 10 5 9 11 6 3 11 5
A X02 20 22 10 12 20 17 18 29 14 13 11 19
B X01 8 7 3 26 17 6 3 2 5 10 8 14
B X05 9 10 16 6 10 4 13 12 6 4 3 6
I want to result it as below...
Warehouse Part Number Maximum three usage quantity Results
A X01 41 41/3
A X02 71 71/3
B X01 57 19
B X05 39 13
Someone told me to use dynamic sql, but I dont know what it is... Pls tell me how to solve this problem in detail. The problem stuck in my mind for a very long time...

Create a Multi-Column Report Using DevEx Grid

I have a daily sales report query and it have 2 columns like
days sales
1 12
2 65
3 25
...
30 24
but when I want to print it there is a lots of free spaces on paper, so I want to seperate query with a percentage (like % 33)
and result will be like 3 x 2 columns for one paper. and it will be more comfortable for me.
days sales days sales days sales
1 12 11 21 21 5
2 65 12 53 22 18
3 25 13 0
...
10 45 20 12 30 55
Any way to do this with DevEx Grid?
this is the view which I get
and I dont want such kind of empty paper for couple of records..
You will not find an easy way to achieve the desired result using XtraGrid, because it is not intended for reporting. I suggest that you create reports using another product: XtraReports.