hello
I need assistance from the experts to adjusting this code . the code creates report by summing the data and the values based on some headers , what I want inserting the last three columns (SALE ,RET, BALANCE) when every time run the macro with considering the formula in column BALANCE should change like this =G2+H2-I2 after first time run the macro as in the last picture .
the formula should be G2+H2 as in original code when run first time .
this is the code .
to understand how the code works
result based on code
what I want insert columns SALE,RET,BALANCE every time run the macro like this
with the same format ,borders ,formulas (formulas as explained above)
I need assistance from the experts to adjusting this code . the code creates report by summing the data and the values based on some headers , what I want inserting the last three columns (SALE ,RET, BALANCE) when every time run the macro with considering the formula in column BALANCE should change like this =G2+H2-I2 after first time run the macro as in the last picture .
the formula should be G2+H2 as in original code when run first time .
this is the code .
VBA Code:
Sub test()
Dim ws As Worksheet, a, Dic As Object, j$, k$, SALE As Double, RET As Double
Set Dic = CreateObject("scripting.dictionary")
For Each ws In Sheets
If ws.Name <> "COLLECTION" Then
a = ws.[A1].CurrentRegion
For x = 2 To UBound(a)
j = Join(Array(a(x, 2), a(x, 3), a(x, 4)), ";")
If UBound(a, 2) = 5 Then
If Not IsNumeric(a(x, 5)) Then a(x, 5) = 0
If a(1, 5) = "SALE" Then SALE = a(x, 5) Else RET = a(x, 5)
Else
If Not IsNumeric(a(x, 5)) Then a(x, 5) = 0
If Not IsNumeric(a(x, 6)) Then a(x, 6) = 0
SALE = IIf(a(1, 5) = "SALE", a(x, 5), a(x, 6))
RET = IIf(a(1, 5) = "SALE", a(x, 6), a(x, 5))
End If
k = Join(Array(SALE, RET), ";")
If Not Dic.exists(j) Then Dic.Add j, k Else _
Dic(j) = Split(Dic(j), ";")(0) + SALE & ";" & Split(Dic(j), ";")(1) + RET
SALE = 0: RET = 0
Next
End If
Next
With Sheets("COLLECTION").[A1].Resize(Dic.Count)
.Parent.UsedRange.Clear
.Resize(1, 7) = [{"ITEM","BR","TY","OR","SALE","RET","BALANCE"}]
.Offset(1, 1) = Application.Transpose(Dic.keys)
.Offset(1, 4) = Application.Transpose(Dic.items)
.Offset(1, 1).TextToColumns .Offset(1, 1), semicolon:=True
.Offset(1, 4).TextToColumns .Offset(1, 4), semicolon:=True
.Offset(1, 0) = Evaluate("row(1:" & Dic.Count & ")")
.Offset(1, 6) = "=E2-F2"
.Resize(Dic.Count + 1, 7).Borders.LineStyle = 1
End With
End Sub
COLLECTION.xlsm | ||||||||
---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | |||
1 | ITEM | BR | TY | OR | SALE | RET | ||
2 | 1 | FR | BANANA | TT | 200 | 10 | ||
3 | 2 | FR | APPLE | LL | 100 | 20 | ||
4 | 3 | FR | PEAR | NN | 10 | - | ||
5 | 4 | FR | BANANA | 20 | - | |||
6 | 5 | VEG | TOMATO | SS | 12 | - | ||
7 | 6 | VEG | TOMATO | AA | 12 | 12 | ||
STA |
COLLECTION.xlsm | ||||||||
---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | |||
1 | ITEM | BR | TY | OR | SALE | RET | ||
2 | 1 | FR | BANANA | TT | 100 | 5 | ||
3 | 2 | FR | APPLE | LL | 50 | 5 | ||
4 | 3 | FR | PEAR | NN | 20 | - | ||
5 | 4 | FR | BANANA | 10 | - | |||
6 | 5 | VEG | TOMATO | SS | 10 | 5 | ||
7 | 6 | VEG | TOMATO | AA | 5 | |||
RPA |
COLLECTION.xlsm | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | ITEM | BR | TY | OR | SALE | ||
2 | 1 | FR | APPLE | LL | 120 | ||
3 | 2 | FR | PEAR | NN | 30 | ||
4 | 3 | FR | BANANA | 40 | |||
5 | 4 | VEG | TOMATO | SS | 50 | ||
6 | 5 | VEG | TOMATO | AA | 5 | ||
7 | 6 | VEG | ONION | AA1 | 6 | ||
SR |
COLLECTION.xlsm | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | ITEM | BR | TY | OR | RET | ||
2 | 1 | FR | BANANA | TT | 5 | ||
3 | 2 | FR | APPLE | LL | 2 | ||
4 | 3 | FR | PEAR | NN | 3 | ||
5 | 4 | FR | BANANA | - | |||
6 | 5 | VEG | TOMATO | SS | 5 | ||
7 | 6 | VEG | ONION | AA1 | - | ||
8 | 7 | VEG | POTATO | AA2 | 5 | ||
SS |
result based on code
COLLECTION.xlsm | |||||||||
---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | |||
1 | ITEM | BR | TY | OR | SALE | RET | BALANCE | ||
2 | 1 | FR | BANANA | TT | 300 | 20 | 280 | ||
3 | 2 | FR | APPLE | LL | 270 | 27 | 243 | ||
4 | 3 | FR | PEAR | NN | 60 | 3 | 57 | ||
5 | 4 | FR | BANANA | 70 | - | 70 | |||
6 | 5 | VEG | TOMATO | SS | 72 | 10 | 62 | ||
7 | 6 | VEG | TOMATO | AA | 22 | 12 | 10 | ||
8 | 7 | VEG | ONION | AA1 | 6 | - | 6 | ||
COLLECTION |
Cell Formulas | ||
---|---|---|
Range | Formula | |
G2:G8 | G2 | =E2-F2 |
what I want insert columns SALE,RET,BALANCE every time run the macro like this
COLLECTION.xlsm | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | |||
1 | ITEM | BR | TY | OR | SALE | RET | BALANCE | SALE | RET | BALANCE | ||
2 | 1 | FR | BANANA | TT | 300 | 20 | 280 | 300 | 20 | 560 | ||
3 | 2 | FR | APPLE | LL | 270 | 27 | 243 | 270 | 27 | 486 | ||
4 | 3 | FR | PEAR | NN | 60 | 3 | 57 | 60 | 3 | 114 | ||
5 | 4 | FR | BANANA | 70 | - | 70 | 70 | - | 140 | |||
6 | 5 | VEG | TOMATO | SS | 72 | 10 | 62 | 72 | 10 | 124 | ||
7 | 6 | VEG | TOMATO | AA | 22 | 12 | 10 | 22 | 12 | 20 | ||
8 | 7 | VEG | ONION | AA1 | 6 | - | 6 | 6 | - | 12 | ||
COLLECTION |
Cell Formulas | ||
---|---|---|
Range | Formula | |
G2:G8 | G2 | =E2-F2 |
J2:J8 | J2 | =G2+H2-I2 |
with the same format ,borders ,formulas (formulas as explained above)