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
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi Jemma,

My attempt at a solution has four processes.
Code:
[COLOR=darkblue]Public[/COLOR] [COLOR=darkblue]Sub[/COLOR] Jemma()
   AddTempSheet
   ReOrganizeData
   ProcessReOrganizedData
   [COLOR=green]'DeleteTempSheet[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

First a temporary sheet is added.

Then ReOrganizeData pulls the relevant data into the temp sheet and sorts it on Currency, Broker and Method. I have commented out the DeleteTempSheet procedure to let you see this output.

Then ProcessReOrganisedData creates the worksheets and grops like brokers.

To test.
Copy your sample data into Sheet1 of a new workbook.
Press Alt+F11 to open the VBA Editor.
Double click on the ThisWorkbook Module.
Copy and paste the code.

You will get a better understanding of what the code does if you step through - F8 - the Jemma procedure.

Code:
[COLOR=darkblue]Public[/COLOR] [COLOR=darkblue]Sub[/COLOR] Jemma()
   AddTempSheet
   ReOrganizeData
   ProcessReOrganizedData
   [COLOR=green]'DeleteTempSheet[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] AddTempSheet()
 
   [COLOR=green]'delete any existing temp sheet[/COLOR]
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
      Application.DisplayAlerts = [COLOR=darkblue]False[/COLOR]
         Sheets("Temp").Delete
      Application.DisplayAlerts = [COLOR=darkblue]True[/COLOR]
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] 0
   [COLOR=green]'add a temp sheet with headers[/COLOR]
   Worksheets.Add After:=Sheets(Sheets.Count)
   ActiveSheet.Name = "Temp"
   [COLOR=darkblue]With[/COLOR] Sheets("Temp")
      .Range("A1").Value = "Broker"
      .Range("B1").Value = "CCY"
      .Range("C1").Value = "Quantity"
      .Range("D1").Value = "Method"
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=green]'colour yellow[/COLOR]
   Sheets("Temp").Range("A1:D1").Interior.ColorIndex = 6
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] ReOrganizeData()
   [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("[COLOR=red]Sheet1[/COLOR]").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 = rng.Offset(, -10).Value [COLOR=green]'col 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]
 
   Sheets("Temp").Columns("A:D").EntireColumn.AutoFit
   [COLOR=darkblue]Set[/COLOR] rng = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[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=darkblue]Set[/COLOR] wsTemp = Sheets("Temp")
   [COLOR=darkblue]Set[/COLOR] rngTemp = wsTemp.Range("B2")
   sCurr = ""
   sBroker = ""
   rowNew = 1
 
   [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=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=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=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
 
      [COLOR=darkblue]Set[/COLOR] rngTemp = rngTemp.Offset(1, 0)
      [COLOR=green]'sCurr = rngTemp.Value[/COLOR]
   [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]
 
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] DeleteTempSheet()
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
 
   Application.DisplayAlerts = [COLOR=darkblue]False[/COLOR]
      Sheets("Temp").Delete
   Application.DisplayAlerts = [COLOR=darkblue]True[/COLOR]
 
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Last edited:
Upvote 0
Bertie, this is awesome, great work, this will save me 30mins, it was painful doing it manually.

Is it possible to add numberformating .NumberFormat = "#,##0.00_);[Red](#,##0.00)" to all the numbers and do Entirecolumn.Autofit for all currency tabs

Hi Jemma,

My attempt at a solution has four processes.
Code:
[COLOR=darkblue]Public[/COLOR] [COLOR=darkblue]Sub[/COLOR] Jemma()
   AddTempSheet
   ReOrganizeData
   ProcessReOrganizedData
   [COLOR=green]'DeleteTempSheet[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

First a temporary sheet is added.

Then ReOrganizeData pulls the relevant data into the temp sheet and sorts it on Currency, Broker and Method. I have commented out the DeleteTempSheet procedure to let you see this output.

Then ProcessReOrganisedData creates the worksheets and grops like brokers.

To test.
Copy your sample data into Sheet1 of a new workbook.
Press Alt+F11 to open the VBA Editor.
Double click on the ThisWorkbook Module.
Copy and paste the code.

You will get a better understanding of what the code does if you step through - F8 - the Jemma procedure.

Code:
[COLOR=darkblue]Public[/COLOR] [COLOR=darkblue]Sub[/COLOR] Jemma()
   AddTempSheet
   ReOrganizeData
   ProcessReOrganizedData
   [COLOR=green]'DeleteTempSheet[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] AddTempSheet()
 
   [COLOR=green]'delete any existing temp sheet[/COLOR]
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
      Application.DisplayAlerts = [COLOR=darkblue]False[/COLOR]
         Sheets("Temp").Delete
      Application.DisplayAlerts = [COLOR=darkblue]True[/COLOR]
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] 0
   [COLOR=green]'add a temp sheet with headers[/COLOR]
   Worksheets.Add After:=Sheets(Sheets.Count)
   ActiveSheet.Name = "Temp"
   [COLOR=darkblue]With[/COLOR] Sheets("Temp")
      .Range("A1").Value = "Broker"
      .Range("B1").Value = "CCY"
      .Range("C1").Value = "Quantity"
      .Range("D1").Value = "Method"
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=green]'colour yellow[/COLOR]
   Sheets("Temp").Range("A1:D1").Interior.ColorIndex = 6
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] ReOrganizeData()
   [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("[COLOR=red]Sheet1[/COLOR]").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 = rng.Offset(, -10).Value [COLOR=green]'col 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]
 
   Sheets("Temp").Columns("A:D").EntireColumn.AutoFit
   [COLOR=darkblue]Set[/COLOR] rng = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[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=darkblue]Set[/COLOR] wsTemp = Sheets("Temp")
   [COLOR=darkblue]Set[/COLOR] rngTemp = wsTemp.Range("B2")
   sCurr = ""
   sBroker = ""
   rowNew = 1
 
   [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=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=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=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
 
      [COLOR=darkblue]Set[/COLOR] rngTemp = rngTemp.Offset(1, 0)
      [COLOR=green]'sCurr = rngTemp.Value[/COLOR]
   [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]
 
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] DeleteTempSheet()
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
 
   Application.DisplayAlerts = [COLOR=darkblue]False[/COLOR]
      Sheets("Temp").Delete
   Application.DisplayAlerts = [COLOR=darkblue]True[/COLOR]
 
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
Hi Jemma,

Can you clarify the number formatting for me.

We can apply the number formatting at the end of the ReOrganizeData procedure:
Code:
   [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]

But all the numbers in the sample data are possitive so the [Red] part of the formatting has no effect.

On looking at your sample data I notice that some commodities are sold for less than the buying price. Is this what you are trying to highlight?

Sheet1

<TABLE style="BACKGROUND-COLOR: #ffffff; PADDING-LEFT: 2pt; PADDING-RIGHT: 2pt; FONT-FAMILY: Arial,Arial; FONT-SIZE: 10pt" border=1 cellSpacing=0 cellPadding=0><COLGROUP><COL style="WIDTH: 30px; FONT-WEIGHT: bold"><COL style="WIDTH: 87px"><COL style="WIDTH: 15px"><COL style="WIDTH: 13px"><COL style="WIDTH: 74px"><COL style="WIDTH: 87px"></COLGROUP><TBODY><TR style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt; FONT-WEIGHT: bold"><TD></TD><TD>F</TD><TD>G</TD><TD>H</TD><TD>I</TD><TD>J</TD></TR><TR style="HEIGHT: 35px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">1</TD><TD style="TEXT-ALIGN: center; FONT-FAMILY: Verdana">BUY_QUANTITY</TD><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana">g</TD><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana">h</TD><TD style="TEXT-ALIGN: center; FONT-FAMILY: Verdana">SELL_CURRENCY</TD><TD style="TEXT-ALIGN: center; FONT-FAMILY: Verdana">SELL_QUANTITY</TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">2</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana">1206800</TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="FONT-FAMILY: Verdana">USD</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana">1217781.9</TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">3</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana">719100</TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="FONT-FAMILY: Verdana">USD</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana">725697.02</TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">4</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana">1478500</TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="FONT-FAMILY: Verdana">USD</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana">1500677.5</TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">5</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana">734200</TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="FONT-FAMILY: Verdana">USD</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana; COLOR: #ff0000">438328.36</TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">6</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana">2792300</TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="FONT-FAMILY: Verdana">USD</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana">2813910.8</TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">7</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana">4122200</TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="FONT-FAMILY: Verdana">USD</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana">4153220.4</TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">8</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana">7746200</TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="FONT-FAMILY: Verdana">USD</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana; COLOR: #ff0000">443859.98</TD></TR><TR style="HEIGHT: 17px"><TD style="TEXT-ALIGN: center; BACKGROUND-COLOR: #cacaca; FONT-SIZE: 8pt">9</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana">7759200</TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="TEXT-ALIGN: right; BACKGROUND-COLOR: #c0c0c0; FONT-FAMILY: Verdana"></TD><TD style="FONT-FAMILY: Verdana">USD</TD><TD style="TEXT-ALIGN: right; FONT-FAMILY: Verdana; COLOR: #ff0000">444632.91</TD></TR></TBODY></TABLE>
 
Upvote 0
Hi Jemma,

Ignore my last post. I notice we are dealing with different currencies. No wonder I'm skint all the time.

Try this. I have highlighted the changes I have made.

Code:
[COLOR=darkblue]Public[/COLOR] [COLOR=darkblue]Sub[/COLOR] Jemma()
   AddTempSheet
   ReOrganizeData
   ProcessReOrganizedData
   [COLOR=green]'DeleteTempSheet[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] AddTempSheet()
 
   [COLOR=green]'delete any existing temp sheet[/COLOR]
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
      Application.DisplayAlerts = [COLOR=darkblue]False[/COLOR]
         Sheets("Temp").Delete
      Application.DisplayAlerts = [COLOR=darkblue]True[/COLOR]
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] 0
   [COLOR=green]'add a temp sheet with headers[/COLOR]
   Worksheets.Add After:=Sheets(Sheets.Count)
   ActiveSheet.Name = "Temp"
   [COLOR=darkblue]With[/COLOR] Sheets("Temp")
      .Range("A1").Value = "Broker"
      .Range("B1").Value = "CCY"
      .Range("C1").Value = "Quantity"
      .Range("D1").Value = "Method"
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=green]'colour yellow[/COLOR]
   Sheets("Temp").Range("A1:D1").Interior.ColorIndex = 6
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] ReOrganizeData()
   [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 = rng.Offset(, -10).Value [COLOR=green]'col 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=red] With Sheets("Temp").Range("C2:C" & rw)[/COLOR]
[COLOR=red]    .NumberFormat = "#,##0.00_ ;[Red]#,##0.00"[/COLOR]
[COLOR=red] End 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]
 
 
[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=darkblue]Set[/COLOR] wsTemp = Sheets("Temp")
   [COLOR=darkblue]Set[/COLOR] rngTemp = wsTemp.Range("B2")
   sCurr = ""
   sBroker = ""
   rowNew = 1
 
   [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=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=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=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
 
      [COLOR=darkblue]Set[/COLOR] rngTemp = rngTemp.Offset(1, 0)
      [COLOR=green]'sCurr = rngTemp.Value[/COLOR]
 
      [COLOR=green]'autofit the columns[/COLOR]
      [COLOR=red]wsNew.Cells.EntireColumn.AutoFit[/COLOR]
   [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]
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] DeleteTempSheet()
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
 
   Application.DisplayAlerts = [COLOR=darkblue]False[/COLOR]
      Sheets("Temp").Delete
   Application.DisplayAlerts = [COLOR=darkblue]True[/COLOR]
 
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
Top man Bertie, this is great! You have saved me so much time, thanks mate :)


Hi Jemma,

Ignore my last post. I notice we are dealing with different currencies. No wonder I'm skint all the time.

Try this. I have highlighted the changes I have made.

Code:
[COLOR=darkblue]Public[/COLOR] [COLOR=darkblue]Sub[/COLOR] Jemma()
   AddTempSheet
   ReOrganizeData
   ProcessReOrganizedData
   [COLOR=green]'DeleteTempSheet[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] AddTempSheet()
 
   [COLOR=green]'delete any existing temp sheet[/COLOR]
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
      Application.DisplayAlerts = [COLOR=darkblue]False[/COLOR]
         Sheets("Temp").Delete
      Application.DisplayAlerts = [COLOR=darkblue]True[/COLOR]
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] 0
   [COLOR=green]'add a temp sheet with headers[/COLOR]
   Worksheets.Add After:=Sheets(Sheets.Count)
   ActiveSheet.Name = "Temp"
   [COLOR=darkblue]With[/COLOR] Sheets("Temp")
      .Range("A1").Value = "Broker"
      .Range("B1").Value = "CCY"
      .Range("C1").Value = "Quantity"
      .Range("D1").Value = "Method"
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=green]'colour yellow[/COLOR]
   Sheets("Temp").Range("A1:D1").Interior.ColorIndex = 6
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] ReOrganizeData()
   [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 = rng.Offset(, -10).Value [COLOR=green]'col 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=red] With Sheets("Temp").Range("C2:C" & rw)[/COLOR]
[COLOR=red]    .NumberFormat = "#,##0.00_ ;[Red]#,##0.00"[/COLOR]
[COLOR=red] End 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]
 
 
[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=darkblue]Set[/COLOR] wsTemp = Sheets("Temp")
   [COLOR=darkblue]Set[/COLOR] rngTemp = wsTemp.Range("B2")
   sCurr = ""
   sBroker = ""
   rowNew = 1
 
   [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=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=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=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
 
      [COLOR=darkblue]Set[/COLOR] rngTemp = rngTemp.Offset(1, 0)
      [COLOR=green]'sCurr = rngTemp.Value[/COLOR]
 
      [COLOR=green]'autofit the columns[/COLOR]
      [COLOR=red]wsNew.Cells.EntireColumn.AutoFit[/COLOR]
   [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]
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] DeleteTempSheet()
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
 
   Application.DisplayAlerts = [COLOR=darkblue]False[/COLOR]
      Sheets("Temp").Delete
   Application.DisplayAlerts = [COLOR=darkblue]True[/COLOR]
 
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
Hi Bertie, is it possible to add Total underneath each broker quantity, something like this

Excel Workbook
ABCD
1BrokerCCYQuantityMethod
2BANC OF AMERICA SECURITIEUSD23,829.88BUY
3BANC OF AMERICA SECURITIEUSD116,330.39BUY
4BANC OF AMERICA SECURITIEUSD6,651,688.08BUY
5BANC OF AMERICA SECURITIEUSD1,477,594.34BUY
6BANC OF AMERICA SECURITIEUSD179,715.67BUY
7BANC OF AMERICA SECURITIEUSD206,333.42BUY
8BANC OF AMERICA SECURITIEUSD1,459,231.67BUY
9BANC OF AMERICA SECURITIEUSD862,380.85BUY
10BANC OF AMERICA SECURITIEUSD436,319.31BUY
11BANC OF AMERICA SECURITIEUSD440,696.11BUY
12BANC OF AMERICA SECURITIEUSD1,502,044.36BUY
13BANC OF AMERICA SECURITIEUSD1,822,838.50BUY
14BANC OF AMERICA SECURITIEUSD1,172,102.56BUY
15BANC OF AMERICA SECURITIEUSD11,187,641.75BUY
16BANC OF AMERICA SECURITIEUSD1,594,789.25BUY
17BANC OF AMERICA SECURITIEUSD1,292,244.17BUY
18BANC OF AMERICA SECURITIEUSD1,701,123.42BUY
43BANC OF AMERICA SECURITIEUSD123,505.75SELL
44BANC OF AMERICA SECURITIEUSD6,746,424.24SELL
45BANC OF AMERICA SECURITIEUSD746,351.08SELL
46
47Total66,856,484.37
48
USD






Hi Jemma,

Ignore my last post. I notice we are dealing with different currencies. No wonder I'm skint all the time.

Try this. I have highlighted the changes I have made.

Code:
[COLOR=darkblue]Public[/COLOR] [COLOR=darkblue]Sub[/COLOR] Jemma()
   AddTempSheet
   ReOrganizeData
   ProcessReOrganizedData
   [COLOR=green]'DeleteTempSheet[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] AddTempSheet()
 
   [COLOR=green]'delete any existing temp sheet[/COLOR]
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
      Application.DisplayAlerts = [COLOR=darkblue]False[/COLOR]
         Sheets("Temp").Delete
      Application.DisplayAlerts = [COLOR=darkblue]True[/COLOR]
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] 0
   [COLOR=green]'add a temp sheet with headers[/COLOR]
   Worksheets.Add After:=Sheets(Sheets.Count)
   ActiveSheet.Name = "Temp"
   [COLOR=darkblue]With[/COLOR] Sheets("Temp")
      .Range("A1").Value = "Broker"
      .Range("B1").Value = "CCY"
      .Range("C1").Value = "Quantity"
      .Range("D1").Value = "Method"
   [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
 
   [COLOR=green]'colour yellow[/COLOR]
   Sheets("Temp").Range("A1:D1").Interior.ColorIndex = 6
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] ReOrganizeData()
   [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 = rng.Offset(, -10).Value [COLOR=green]'col 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=red] With Sheets("Temp").Range("C2:C" & rw)[/COLOR]
[COLOR=red]    .NumberFormat = "#,##0.00_ ;[Red]#,##0.00"[/COLOR]
[COLOR=red] End 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]
 
 
[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=darkblue]Set[/COLOR] wsTemp = Sheets("Temp")
   [COLOR=darkblue]Set[/COLOR] rngTemp = wsTemp.Range("B2")
   sCurr = ""
   sBroker = ""
   rowNew = 1
 
   [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=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=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=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
 
      [COLOR=darkblue]Set[/COLOR] rngTemp = rngTemp.Offset(1, 0)
      [COLOR=green]'sCurr = rngTemp.Value[/COLOR]
 
      [COLOR=green]'autofit the columns[/COLOR]
      [COLOR=red]wsNew.Cells.EntireColumn.AutoFit[/COLOR]
   [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]
 
[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] DeleteTempSheet()
   [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
 
   Application.DisplayAlerts = [COLOR=darkblue]False[/COLOR]
      Sheets("Temp").Delete
   Application.DisplayAlerts = [COLOR=darkblue]True[/COLOR]
 
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0
Hi Jemma,

I have adjusted the ProcessReOrganizedData procedure as highlighted below:

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=darkblue]Dim[/COLOR] dTotal [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Double[/COLOR]
 
   [COLOR=darkblue]Set[/COLOR] wsTemp = Sheets("Temp")
   [COLOR=darkblue]Set[/COLOR] rngTemp = wsTemp.Range("B2")
   sCurr = ""
   sBroker = ""
   rowNew = 1
 
   [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=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=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=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
 
     [COLOR=green]'do we need to add total?[/COLOR]
[COLOR=red]     If rngTemp.Offset(0, -1).Value <> rngTemp.Offset(1, -1).Value Then[/COLOR]
 
[COLOR=red]        With wsNew[/COLOR]
[COLOR=red]           .Cells(rowNew + 2, colNew + 1).Value = "Total"[/COLOR]
[COLOR=red]           .Cells(rowNew + 2, colNew + 2).Value = _[/COLOR]
[COLOR=red]              .Application.WorksheetFunction.Sum _[/COLOR]
[COLOR=red]              (Range(.Cells(2, colNew + 2), Cells(rowNew + 1, colNew + 2)))[/COLOR]
[COLOR=red]           .Cells(rowNew + 2, colNew + 2).NumberFormat = "#,##0.00_ ;[Red]#,##0.00"[/COLOR]
[COLOR=red]        End With[/COLOR]
 
[COLOR=red]     End 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]
 
Upvote 0
Your too good Bertie, thanks heaps for this :)

Hi Jemma,

I have adjusted the ProcessReOrganizedData procedure as highlighted below:

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=darkblue]Dim[/COLOR] dTotal [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Double[/COLOR]
 
   [COLOR=darkblue]Set[/COLOR] wsTemp = Sheets("Temp")
   [COLOR=darkblue]Set[/COLOR] rngTemp = wsTemp.Range("B2")
   sCurr = ""
   sBroker = ""
   rowNew = 1
 
   [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=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=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=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
 
     [COLOR=green]'do we need to add total?[/COLOR]
[COLOR=red]     If rngTemp.Offset(0, -1).Value <> rngTemp.Offset(1, -1).Value Then[/COLOR]
 
[COLOR=red]        With wsNew[/COLOR]
[COLOR=red]           .Cells(rowNew + 2, colNew + 1).Value = "Total"[/COLOR]
[COLOR=red]           .Cells(rowNew + 2, colNew + 2).Value = _[/COLOR]
[COLOR=red]              .Application.WorksheetFunction.Sum _[/COLOR]
[COLOR=red]              (Range(.Cells(2, colNew + 2), Cells(rowNew + 1, colNew + 2)))[/COLOR]
[COLOR=red]           .Cells(rowNew + 2, colNew + 2).NumberFormat = "#,##0.00_ ;[Red]#,##0.00"[/COLOR]
[COLOR=red]        End With[/COLOR]
 
[COLOR=red]     End 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]
 
Upvote 0
Hi Bertie,

I realised that the totals must be a net position of buys and sells, where the sells are negatives, my bad. So when the code totals each quantity can it take into a/c if its a sell then its a negative, so as an example the below should total to 65,716.33 rather than 81,571,868.91.


Excel Workbook
BDBEBFBG
1BrokerCCYQuantityMethod
2WESTPAC BANKING CORPORATIUSD2,736,670.29BUY
3WESTPAC BANKING CORPORATIUSD1,296,126.32BUY
4WESTPAC BANKING CORPORATIUSD2,328,178.14BUY
5WESTPAC BANKING CORPORATIUSD652,722.56BUY
6WESTPAC BANKING CORPORATIUSD1,455,064.81BUY
7WESTPAC BANKING CORPORATIUSD1,454,781.05BUY
8WESTPAC BANKING CORPORATIUSD1,214,106.64BUY
9WESTPAC BANKING CORPORATIUSD1,482,392.79BUY
10WESTPAC BANKING CORPORATIUSD1,668,418.36BUY
11WESTPAC BANKING CORPORATIUSD13,031,544.96BUY
12WESTPAC BANKING CORPORATIUSD10,830,637.78BUY
13WESTPAC BANKING CORPORATIUSD1,919,720.49BUY
14WESTPAC BANKING CORPORATIUSD682,712.10BUY
15WESTPAC BANKING CORPORATIUSD4,614,606.33SELL
16WESTPAC BANKING CORPORATIUSD11,580,325.62SELL
17WESTPAC BANKING CORPORATIUSD12,125,374.93SELL
18WESTPAC BANKING CORPORATIUSD1,864,441.67SELL
19WESTPAC BANKING CORPORATIUSD2,124,989.32SELL
20WESTPAC BANKING CORPORATIUSD1,445,676.16SELL
21WESTPAC BANKING CORPORATIUSD2,787,275.53SELL
22WESTPAC BANKING CORPORATIUSD2,339,890.36SELL
23WESTPAC BANKING CORPORATIUSD1,310,897.50SELL
24WESTPAC BANKING CORPORATIUSD625,315.20SELL
25
26Total65,716.33
USD






Hi Jemma,

I have adjusted the ProcessReOrganizedData procedure as highlighted below:

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=darkblue]Dim[/COLOR] dTotal [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Double[/COLOR]
 
   [COLOR=darkblue]Set[/COLOR] wsTemp = Sheets("Temp")
   [COLOR=darkblue]Set[/COLOR] rngTemp = wsTemp.Range("B2")
   sCurr = ""
   sBroker = ""
   rowNew = 1
 
   [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=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=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=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
      [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
 
     [COLOR=green]'do we need to add total?[/COLOR]
[COLOR=red]     If rngTemp.Offset(0, -1).Value <> rngTemp.Offset(1, -1).Value Then[/COLOR]
 
[COLOR=red]        With wsNew[/COLOR]
[COLOR=red]           .Cells(rowNew + 2, colNew + 1).Value = "Total"[/COLOR]
[COLOR=red]           .Cells(rowNew + 2, colNew + 2).Value = _[/COLOR]
[COLOR=red]              .Application.WorksheetFunction.Sum _[/COLOR]
[COLOR=red]              (Range(.Cells(2, colNew + 2), Cells(rowNew + 1, colNew + 2)))[/COLOR]
[COLOR=red]           .Cells(rowNew + 2, colNew + 2).NumberFormat = "#,##0.00_ ;[Red]#,##0.00"[/COLOR]
[COLOR=red]        End With[/COLOR]
 
[COLOR=red]     End 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]
 
Upvote 0

Forum statistics

Threads
1,224,618
Messages
6,179,917
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