Challenging Macro help

Jemma Atkinson

Well-known Member
Joined
Jul 7, 2008
Messages
509
Hi, i have a work task which is very manual, i was hoping a macro could automate this. I have a list of brokers which buys and sells FX, i need a macro to create a tab for each currency and put the Buy and sells in one column for each broker, i am after a layout as shown below for the USD.

Please take note of Rawdata columns, i have hidden the columns which are not in use. Below is just an example, just to show you what i want the macro to do.

Excel Workbook
EFIJT
1BUY_CURRENCYBUY_QUANTITYSELL_CURRENCYSELL_QUANTITYEXEC_BROKER
2AUD1478500USD1500677.5DEUTSCHE BANK AG CORP.
3USD668304.76KRW744491500BARCLAYS BANK PLC WHOLESA
4TWD13753200USD479372.6STATE STREET BANK AND TRU
5KRW654212700USD589593.28BARCLAYS BANK PLC WHOLESA
6USD446627.37HUF88214600UBS SECURITIES L.L.C.
7CZK7746200USD443859.98BARCLAYS BANK PLC WHOLESA
8USD439083.93CLP212143400THE ROYAL BANK OF SCOTLAN
9BRL734200USD438328.36BARCLAYS BANK PLC WHOLESA
10MXN5336600USD443158.23BARCLAYS BANK PLC WHOLESA
11CZK7759200USD444632.91UBS SECURITIES L.L.C.
12EUR1066800USD1470669.14CREDIT SUISSE
13USD1528258.82AUD1514300STATE STREET BANK AND TRU
14EUR4078200USD5622320.71STATE STREET BANK AND TRU
15CAD4122200USD4153220.4STATE STREET BANK AND TRU
16AUD719100USD725697.02CITIBANK, N.A.
17EUR3117700USD4298694.11CITIBANK, N.A.
18CAD2792300USD2813910.84CITIBANK, N.A.
19AUD1206800USD1217781.88BARCLAYS BANK PLC WHOLESA
Rawdata


Excel Workbook
ABCDEFGHI
1BrokerCCYQuantityMethodBrokerCCYQuantityMethod
2BANC OF AMERICA SECURITIEUSD23,829.88BUYBARCLAYS BANK PLC WHOLESAUSD668,304.76BUY
3BANC OF AMERICA SECURITIEUSD116,330.39BUYBARCLAYS BANK PLC WHOLESAUSD4,071,417.78BUY
4BANC OF AMERICA SECURITIEUSD6,651,688.08BUYBARCLAYS BANK PLC WHOLESAUSD201,583.80BUY
5BANC OF AMERICA SECURITIEUSD1,477,594.34BUYBARCLAYS BANK PLC WHOLESAUSD812,395.09BUY
6BANC OF AMERICA SECURITIEUSD179,715.67BUYBARCLAYS BANK PLC WHOLESAUSD818,319.84BUY
7BANC OF AMERICA SECURITIEUSD206,333.42BUYBARCLAYS BANK PLC WHOLESAUSD1,614,417.63BUY
8BANC OF AMERICA SECURITIEUSD1,459,231.67BUYBARCLAYS BANK PLC WHOLESAUSD491,060.89BUY
9BANC OF AMERICA SECURITIEUSD862,380.85BUYBARCLAYS BANK PLC WHOLESAUSD436,926.61BUY
10BANC OF AMERICA SECURITIEUSD436,319.31BUYBARCLAYS BANK PLC WHOLESAUSD435,050.89BUY
11BANC OF AMERICA SECURITIEUSD440,696.11BUYBARCLAYS BANK PLC WHOLESAUSD434,180.39BUY
12BANC OF AMERICA SECURITIEUSD1,502,044.36BUYBARCLAYS BANK PLC WHOLESAUSD434,059.85BUY
13BANC OF AMERICA SECURITIEUSD1,822,838.50BUYBARCLAYS BANK PLC WHOLESAUSD1,459,465.03BUY
14BANC OF AMERICA SECURITIEUSD334,199.37SELLBARCLAYS BANK PLC WHOLESAUSD589593.28SELL
15BANC OF AMERICA SECURITIEUSD441,367.24SELLBARCLAYS BANK PLC WHOLESAUSD443859.98SELL
16BANC OF AMERICA SECURITIEUSD435,944.41SELLBARCLAYS BANK PLC WHOLESAUSD438328.36SELL
17BANC OF AMERICA SECURITIEUSD1,446,390.60SELLBARCLAYS BANK PLC WHOLESAUSD443158.23SELL
18BANC OF AMERICA SECURITIEUSD442,413.33SELLBARCLAYS BANK PLC WHOLESAUSD1217781.88SELL
19BANC OF AMERICA SECURITIEUSD733,387.80SELLBARCLAYS BANK PLC WHOLESAUSD289598.4SELL
20BANC OF AMERICA SECURITIEUSD444,025.92SELLBARCLAYS BANK PLC WHOLESAUSD11798943.9SELL
21BANC OF AMERICA SECURITIEUSD235,913.98SELLBARCLAYS BANK PLC WHOLESAUSD1341943.93SELL
22BANC OF AMERICA SECURITIEUSD986,223.99SELLBARCLAYS BANK PLC WHOLESAUSD46593.17SELL
23BANC OF AMERICA SECURITIEUSD123,505.75SELLBARCLAYS BANK PLC WHOLESAUSD460306.13SELL
24BANC OF AMERICA SECURITIEUSD6,746,424.24SELLBARCLAYS BANK PLC WHOLESAUSD687199.77SELL
25BANC OF AMERICA SECURITIEUSD746,351.08SELLBARCLAYS BANK PLC WHOLESAUSD28067.7SELL
USD
 
When we reorganise the data into the temp sheet just enter a negative value. See highlighted line below.

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] [COLOR=red]ReOrganizeData[/COLOR]()
   [COLOR=darkblue]Dim[/COLOR] rng [COLOR=darkblue]As[/COLOR] Range
   [COLOR=darkblue]Dim[/COLOR] rw [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
 
   [COLOR=darkblue]Set[/COLOR] rng = Sheets("Sheet1").Range("T2")
 
   [COLOR=green]'reorganize the data[/COLOR]
 
   [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] rng = ""
      rw = rw + 2
      [COLOR=darkblue]With[/COLOR] Sheets("Temp")
         [COLOR=green]'buy[/COLOR]
         .Range("A" & rw).Value = rng.Value                   'col T Broker
         .Range("B" & rw).Value = rng.Offset(, -15).Value     [COLOR=green]'col E buy Curr[/COLOR]
         .Range("C" & rw).Value = rng.Offset(, -14).Value     [COLOR=green]'col F buy qty[/COLOR]
         .Range("D" & rw).Value = "BUY"                       'buy
 
         [COLOR=green]'sell[/COLOR]
         .Range("A" & rw + 1).Value = rng.Value               'col T Broker
         .Range("B" & rw + 1).Value = rng.Offset(, -11).Value  [COLOR=green]'col I sell Curr[/COLOR]
         .Range("C" & rw + 1).Value = [COLOR=red]-(rng.Offset(, -10).Value) [/COLOR][COLOR=seagreen]'col [/COLOR][COLOR=green]J sell qty[/COLOR]
         .Range("D" & rw + 1).Value = "SELL"
 
         [COLOR=darkblue]Set[/COLOR] rng = rng.Offset(1, 0)
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
   [COLOR=darkblue]Loop[/COLOR]
 
   [COLOR=green]'sort the re-organized data[/COLOR]
   [COLOR=darkblue]With[/COLOR] Sheets("Temp")
         .Range("A1:D" & rw + 1).Sort _
         Key1:=Range("B2"), Order1:=xlAscending, _
         Key2:=Range("A2"), Order2:=xlAscending, _
         Key3:=Range("D2"), Order2:=xlAscending, _
         Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
         Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
         DataOption2:=xlSortNormal
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=darkblue]With[/COLOR] Sheets("Temp").Range("C2:C" & rw)
      .NumberFormat = "#,##0.00_ ;[Red]#,##0.00"
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   Sheets("Temp").Columns("A:D").EntireColumn.AutoFit
   [COLOR=darkblue]Set[/COLOR] rng = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi Bertie, after the code has summed up the totals i need the SELL to be Absolute values again this is because when i upload the figures into our reconciliation system it will only accept absolute values


When we reorganise the data into the temp sheet just enter a negative value. See highlighted line below.

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] [COLOR=red]ReOrganizeData[/COLOR]()
   [COLOR=darkblue]Dim[/COLOR] rng [COLOR=darkblue]As[/COLOR] Range
   [COLOR=darkblue]Dim[/COLOR] rw [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
 
   [COLOR=darkblue]Set[/COLOR] rng = Sheets("Sheet1").Range("T2")
 
   [COLOR=green]'reorganize the data[/COLOR]
 
   [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] rng = ""
      rw = rw + 2
      [COLOR=darkblue]With[/COLOR] Sheets("Temp")
         [COLOR=green]'buy[/COLOR]
         .Range("A" & rw).Value = rng.Value                   'col T Broker
         .Range("B" & rw).Value = rng.Offset(, -15).Value     [COLOR=green]'col E buy Curr[/COLOR]
         .Range("C" & rw).Value = rng.Offset(, -14).Value     [COLOR=green]'col F buy qty[/COLOR]
         .Range("D" & rw).Value = "BUY"                       'buy
 
         [COLOR=green]'sell[/COLOR]
         .Range("A" & rw + 1).Value = rng.Value               'col T Broker
         .Range("B" & rw + 1).Value = rng.Offset(, -11).Value  [COLOR=green]'col I sell Curr[/COLOR]
         .Range("C" & rw + 1).Value = [COLOR=red]-(rng.Offset(, -10).Value) [/COLOR][COLOR=seagreen]'col [/COLOR][COLOR=green]J sell qty[/COLOR]
         .Range("D" & rw + 1).Value = "SELL"
 
         [COLOR=darkblue]Set[/COLOR] rng = rng.Offset(1, 0)
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
   [COLOR=darkblue]Loop[/COLOR]
 
   [COLOR=green]'sort the re-organized data[/COLOR]
   [COLOR=darkblue]With[/COLOR] Sheets("Temp")
         .Range("A1:D" & rw + 1).Sort _
         Key1:=Range("B2"), Order1:=xlAscending, _
         Key2:=Range("A2"), Order2:=xlAscending, _
         Key3:=Range("D2"), Order2:=xlAscending, _
         Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
         Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
         DataOption2:=xlSortNormal
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=darkblue]With[/COLOR] Sheets("Temp").Range("C2:C" & rw)
      .NumberFormat = "#,##0.00_ ;[Red]#,##0.00"
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   Sheets("Temp").Columns("A:D").EntireColumn.AutoFit
   [COLOR=darkblue]Set[/COLOR] rng = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
Hi Jemma,

Here are the amendments I have made. I have created a function to calculate a RunningTotal.

Code:
[COLOR=darkblue]Function[/COLOR] RunningTotal([COLOR=darkblue]ByRef[/COLOR] dTotal [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Double[/COLOR], _
                     [COLOR=darkblue]ByVal[/COLOR] rngTemp [COLOR=darkblue]As[/COLOR] Range) As [COLOR=darkblue]Double[/COLOR]
   [COLOR=green]'calculate running total[/COLOR]
   [COLOR=darkblue]If[/COLOR] rngTemp.Offset(, 2).Value = "BUY" [COLOR=darkblue]Then[/COLOR]
      RunningTotal = dTotal + rngTemp.Offset(, 1).Value
   [COLOR=darkblue]Else[/COLOR]
      RunningTotal = dTotal - rngTemp.Offset(, 1).Value
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
End [COLOR=darkblue]Function[/COLOR]

This function is called at various stages in the ProcessReOrganizedData procedure.

I have highlighted below the changes I have made to this procedure.

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] ProcessReOrganizedData()
   [COLOR=darkblue]Dim[/COLOR] wsNew [COLOR=darkblue]As[/COLOR] Worksheet
   [COLOR=darkblue]Dim[/COLOR] wsTemp [COLOR=darkblue]As[/COLOR] Worksheet
   [COLOR=darkblue]Dim[/COLOR] rngTemp [COLOR=darkblue]As[/COLOR] Range
   [COLOR=darkblue]Dim[/COLOR] sCurr [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]           [COLOR=green]'currency[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] sBroker [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]         'broker
   [COLOR=darkblue]Dim[/COLOR] rowNew [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] colNew [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
 [COLOR=red]  Dim dTotal As Double[/COLOR]
 
   [COLOR=darkblue]Set[/COLOR] wsTemp = Sheets("Temp")
   [COLOR=darkblue]Set[/COLOR] rngTemp = wsTemp.Range("B2")
   sCurr = ""
   sBroker = ""
   rowNew = 1
   dTotal = 0#    [COLOR=green]'0.00[/COLOR]
 
   [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] rngTemp = ""
      [COLOR=green]'do we need to cretae a new worksheet?[/COLOR]
      [COLOR=darkblue]If[/COLOR] sCurr <> rngTemp.Value [COLOR=darkblue]Then[/COLOR]
         sCurr = rngTemp.Value
         Worksheets.Add After:=Sheets("Temp")
         ActiveSheet.Name = sCurr
         [COLOR=darkblue]Set[/COLOR] wsNew = Sheets(sCurr)
         rowNew = 2
         colNew = 1
 
         [COLOR=green]'copy headers[/COLOR]
         wsTemp.Range("A1:D1").Copy Destination:=wsNew.Cells(1, colNew)
         [COLOR=green]'copy values[/COLOR]
         wsTemp.Range("A" & rngTemp.Row & ":D" & rngTemp.Row).Copy _
            Destination:=wsNew.Cells(rowNew, colNew)
 
         [COLOR=green]'reset the broker[/COLOR]
         sBroker = rngTemp.Offset(0, -1).Value
 
         [COLOR=green]'calculate running total[/COLOR]
[COLOR=red]     dTotal = RunningTotal(dTotal, rngTemp)[/COLOR]
 
      [COLOR=darkblue]Else[/COLOR]
         [COLOR=green]'check the broker[/COLOR]
         [COLOR=darkblue]If[/COLOR] sBroker = rngTemp.Offset(0, -1).Value [COLOR=darkblue]Then[/COLOR]
            rowNew = rowNew + 1
            [COLOR=green]'copy values[/COLOR]
            wsTemp.Range("A" & rngTemp.Row & ":D" & rngTemp.Row).Copy _
            Destination:=wsNew.Cells(rowNew, colNew)
 
            [COLOR=green]'calculate running total[/COLOR]
[COLOR=red]        dTotal = RunningTotal(dTotal, rngTemp)[/COLOR]
         [COLOR=darkblue]Else[/COLOR]
            [COLOR=green]'move output along five columns[/COLOR]
            rowNew = 2
            colNew = colNew + 5
 
            [COLOR=green]'copy headers[/COLOR]
            wsTemp.Range("A1:D1").Copy Destination:=wsNew.Cells(1, colNew)
            [COLOR=green]'copy values[/COLOR]
            wsTemp.Range("A" & rngTemp.Row & ":D" & rngTemp.Row).Copy _
               Destination:=wsNew.Cells(rowNew, colNew)
 
            [COLOR=green]'reset the broker[/COLOR]
            sBroker = rngTemp.Offset(0, -1).Value
 
            [COLOR=green]'calculate running total[/COLOR]
[COLOR=red]        dTotal = RunningTotal(dTotal, rngTemp)[/COLOR]
 
          [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
 
     [COLOR=green]'Do we need to add total? Check broker and currency.[/COLOR]
      [COLOR=darkblue]If[/COLOR] rngTemp.Offset(0, -1).Value <> rngTemp.Offset(1, -1).Value [COLOR=red]Or _[/COLOR]
[COLOR=red]     rngTemp.Value <> rngTemp.Offset(1, 0).Value[/COLOR] [COLOR=darkblue]Then[/COLOR]
 
         [COLOR=darkblue]With[/COLOR] wsNew
            .Cells(rowNew + 2, colNew + 1).Value = "Total"
            .Cells(rowNew + 2, colNew + 2).Value =[COLOR=red] dTotal[/COLOR]
            .Cells(rowNew + 2, colNew + 2).NumberFormat = "#,##0.00_ ;[Red]#,##0.00"
         [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
         [COLOR=green]'reset the running total[/COLOR]
[COLOR=red]     dTotal = 0#[/COLOR]
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
 
      [COLOR=darkblue]Set[/COLOR] rngTemp = rngTemp.Offset(1, 0)
 
      [COLOR=green]'autofit the columns[/COLOR]
      wsNew.Cells.EntireColumn.AutoFit
 
   [COLOR=darkblue]Loop[/COLOR]
 
   [COLOR=darkblue]Set[/COLOR] rngTemp = [COLOR=darkblue]Nothing[/COLOR]
   [COLOR=darkblue]Set[/COLOR] wsNew = [COLOR=darkblue]Nothing[/COLOR]
   [COLOR=darkblue]Set[/COLOR] wsTemp = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Replace the ProcessReOrganizedData procedure with the new version and add the function to the same module which contains the rest of the code.

Bertie.
 
Upvote 0
Fantastic Bertie, you are a champ! Thanks for all your help on this task.


Hi Jemma,

Here are the amendments I have made. I have created a function to calculate a RunningTotal.

Code:
[COLOR=darkblue]Function[/COLOR] RunningTotal([COLOR=darkblue]ByRef[/COLOR] dTotal [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Double[/COLOR], _
                     [COLOR=darkblue]ByVal[/COLOR] rngTemp [COLOR=darkblue]As[/COLOR] Range) As [COLOR=darkblue]Double[/COLOR]
   [COLOR=green]'calculate running total[/COLOR]
   [COLOR=darkblue]If[/COLOR] rngTemp.Offset(, 2).Value = "BUY" [COLOR=darkblue]Then[/COLOR]
      RunningTotal = dTotal + rngTemp.Offset(, 1).Value
   [COLOR=darkblue]Else[/COLOR]
      RunningTotal = dTotal - rngTemp.Offset(, 1).Value
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
End [COLOR=darkblue]Function[/COLOR]
This function is called at various stages in the ProcessReOrganizedData procedure.

I have highlighted below the changes I have made to this procedure.

Code:
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] ProcessReOrganizedData()
   [COLOR=darkblue]Dim[/COLOR] wsNew [COLOR=darkblue]As[/COLOR] Worksheet
   [COLOR=darkblue]Dim[/COLOR] wsTemp [COLOR=darkblue]As[/COLOR] Worksheet
   [COLOR=darkblue]Dim[/COLOR] rngTemp [COLOR=darkblue]As[/COLOR] Range
   [COLOR=darkblue]Dim[/COLOR] sCurr [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]           [COLOR=green]'currency[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] sBroker [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]         'broker
   [COLOR=darkblue]Dim[/COLOR] rowNew [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
   [COLOR=darkblue]Dim[/COLOR] colNew [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR]
 [COLOR=red]  Dim dTotal As Double[/COLOR]
 
   [COLOR=darkblue]Set[/COLOR] wsTemp = Sheets("Temp")
   [COLOR=darkblue]Set[/COLOR] rngTemp = wsTemp.Range("B2")
   sCurr = ""
   sBroker = ""
   rowNew = 1
   dTotal = 0#    [COLOR=green]'0.00[/COLOR]
 
   [COLOR=darkblue]Do[/COLOR] [COLOR=darkblue]Until[/COLOR] rngTemp = ""
      [COLOR=green]'do we need to cretae a new worksheet?[/COLOR]
      [COLOR=darkblue]If[/COLOR] sCurr <> rngTemp.Value [COLOR=darkblue]Then[/COLOR]
         sCurr = rngTemp.Value
         Worksheets.Add After:=Sheets("Temp")
         ActiveSheet.Name = sCurr
         [COLOR=darkblue]Set[/COLOR] wsNew = Sheets(sCurr)
         rowNew = 2
         colNew = 1
 
         [COLOR=green]'copy headers[/COLOR]
         wsTemp.Range("A1:D1").Copy Destination:=wsNew.Cells(1, colNew)
         [COLOR=green]'copy values[/COLOR]
         wsTemp.Range("A" & rngTemp.Row & ":D" & rngTemp.Row).Copy _
            Destination:=wsNew.Cells(rowNew, colNew)
 
         [COLOR=green]'reset the broker[/COLOR]
         sBroker = rngTemp.Offset(0, -1).Value
 
         [COLOR=green]'calculate running total[/COLOR]
[COLOR=red]     dTotal = RunningTotal(dTotal, rngTemp)[/COLOR]
 
      [COLOR=darkblue]Else[/COLOR]
         [COLOR=green]'check the broker[/COLOR]
         [COLOR=darkblue]If[/COLOR] sBroker = rngTemp.Offset(0, -1).Value [COLOR=darkblue]Then[/COLOR]
            rowNew = rowNew + 1
            [COLOR=green]'copy values[/COLOR]
            wsTemp.Range("A" & rngTemp.Row & ":D" & rngTemp.Row).Copy _
            Destination:=wsNew.Cells(rowNew, colNew)
 
            [COLOR=green]'calculate running total[/COLOR]
[COLOR=red]        dTotal = RunningTotal(dTotal, rngTemp)[/COLOR]
         [COLOR=darkblue]Else[/COLOR]
            [COLOR=green]'move output along five columns[/COLOR]
            rowNew = 2
            colNew = colNew + 5
 
            [COLOR=green]'copy headers[/COLOR]
            wsTemp.Range("A1:D1").Copy Destination:=wsNew.Cells(1, colNew)
            [COLOR=green]'copy values[/COLOR]
            wsTemp.Range("A" & rngTemp.Row & ":D" & rngTemp.Row).Copy _
               Destination:=wsNew.Cells(rowNew, colNew)
 
            [COLOR=green]'reset the broker[/COLOR]
            sBroker = rngTemp.Offset(0, -1).Value
 
            [COLOR=green]'calculate running total[/COLOR]
[COLOR=red]        dTotal = RunningTotal(dTotal, rngTemp)[/COLOR]
 
          [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
 
     [COLOR=green]'Do we need to add total? Check broker and currency.[/COLOR]
      [COLOR=darkblue]If[/COLOR] rngTemp.Offset(0, -1).Value <> rngTemp.Offset(1, -1).Value [COLOR=red]Or _[/COLOR]
[COLOR=red]     rngTemp.Value <> rngTemp.Offset(1, 0).Value[/COLOR] [COLOR=darkblue]Then[/COLOR]
 
         [COLOR=darkblue]With[/COLOR] wsNew
            .Cells(rowNew + 2, colNew + 1).Value = "Total"
            .Cells(rowNew + 2, colNew + 2).Value =[COLOR=red] dTotal[/COLOR]
            .Cells(rowNew + 2, colNew + 2).NumberFormat = "#,##0.00_ ;[Red]#,##0.00"
         [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
         [COLOR=green]'reset the running total[/COLOR]
[COLOR=red]     dTotal = 0#[/COLOR]
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
 
      [COLOR=darkblue]Set[/COLOR] rngTemp = rngTemp.Offset(1, 0)
 
      [COLOR=green]'autofit the columns[/COLOR]
      wsNew.Cells.EntireColumn.AutoFit
 
   [COLOR=darkblue]Loop[/COLOR]
 
   [COLOR=darkblue]Set[/COLOR] rngTemp = [COLOR=darkblue]Nothing[/COLOR]
   [COLOR=darkblue]Set[/COLOR] wsNew = [COLOR=darkblue]Nothing[/COLOR]
   [COLOR=darkblue]Set[/COLOR] wsTemp = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
Replace the ProcessReOrganizedData procedure with the new version and add the function to the same module which contains the rest of the code.

Bertie.
 
Upvote 0

Forum statistics

Threads
1,224,621
Messages
6,179,946
Members
452,949
Latest member
beartooth91

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