how adjusting code to insert three columns every time run the macro

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
343
Office Version
  1. 2016
Platform
  1. Windows
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 .
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
to understand how the code works
COLLECTION.xlsm
ABCDEF
1ITEMBRTYORSALERET
21FRBANANATT20010
32FRAPPLELL10020
43FRPEARNN10-
54FRBANANAQQ20-
65VEGTOMATOSS12-
76VEGTOMATOAA1212
STA


COLLECTION.xlsm
ABCDEF
1ITEMBRTYORSALERET
21FRBANANATT1005
32FRAPPLELL505
43FRPEARNN20-
54FRBANANAQQ10-
65VEGTOMATOSS105
76VEGTOMATOAA5
RPA


COLLECTION.xlsm
ABCDE
1ITEMBRTYORSALE
21FRAPPLELL120
32FRPEARNN30
43FRBANANAQQ40
54VEGTOMATOSS50
65VEGTOMATOAA5
76VEGONIONAA16
SR


COLLECTION.xlsm
ABCDE
1ITEMBRTYORRET
21FRBANANATT5
32FRAPPLELL2
43FRPEARNN3
54FRBANANAQQ-
65VEGTOMATOSS5
76VEGONIONAA1-
87VEGPOTATOAA25
SS

result based on code
COLLECTION.xlsm
ABCDEFG
1ITEMBRTYORSALERETBALANCE
21FRBANANATT30020280
32FRAPPLELL27027243
43FRPEARNN60357
54FRBANANAQQ70-70
65VEGTOMATOSS721062
76VEGTOMATOAA221210
87VEGONIONAA16-6
COLLECTION
Cell Formulas
RangeFormula
G2:G8G2=E2-F2


what I want insert columns SALE,RET,BALANCE every time run the macro like this

COLLECTION.xlsm
ABCDEFGHIJ
1ITEMBRTYORSALERETBALANCESALERETBALANCE
21FRBANANATT3002028030020560
32FRAPPLELL2702724327027486
43FRPEARNN60357603114
54FRBANANAQQ70-7070-140
65VEGTOMATOSS7210627210124
76VEGTOMATOAA221210221220
87VEGONIONAA16-66-12
COLLECTION
Cell Formulas
RangeFormula
G2:G8G2=E2-F2
J2:J8J2=G2+H2-I2


with the same format ,borders ,formulas (formulas as explained above)
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top