VBA to transfor set of data from column to rows

danielalvz

New Member
Joined
Jan 29, 2022
Messages
9
Office Version
  1. 2011
Platform
  1. Windows
Hello I have a complex task that I was told that required VBA, i started to attempt it using formulas but did not get very far, there is a detailed the description of what i need accomplish in the mini sheet below, it is challanging but if any one could get it done it will be greatly appreaciated



working copy.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAE
1Date (wrong format)Date Symbol BiasQuantityPrice BOTPrice SLDRealized P&LEntry TimeExit TimeCommissionTrade ID
2BOTVERU5015.79513:39:162022041411short2022041414-Apr-22VERUshort5015.79515.715-6.0113:39:1611
3SLDVERU5015.715-6.0113:39:49202204141.011short2022041414-Apr-22VERUShort5013.5913.42-10.5113:51:241.012
4SLDVERU5013.4213:51:24202204141.012Short2022041414-Apr-22VERULong5014.04514.163.7413:55:2413
5BOTVERU5013.59-10.5113:53:292022041412Short2022041414-Apr-22VERUShort5014.5514.57-1.0114:10:5014
6BOTVERU5014.04513:55:242022041413Long2022041414-Apr-22VERUShort5014.99515.6731.7414:21:0615
7SLDVERU5014.163.7414:00:31202204141.013Long2022041414-Apr-22VERUShort5017.0617.3914.4914:27:3116
8BOTVERU5014.5514:10:502022041414Short
9SLDVERU5014.57-1.0114:18:24202204141.014Short
10BOTVERU5014.99514:21:062022041415ShortExtract the information from table A to H into L to V information to extract for every trade: Symbol, QTY, Price Bought or Sold, P&L, Time in and out, Date and commission - a trade is defined by the P&L: for example the first trade in the table was Veru bought at 15.795 and sold at 15.715, the trade is completed as the P&L of -6.01 appears on the 3 row next to SLD and the next trade starts ( this trade resulted in a loss this is why the P&L is negative) -the trade could be long or long defined by whether under the Action column BOT appears first, for example in the first trade BOT appears first meaning a LONG, the second trade after this one which in this case was using the same Symbol was short, define as SLD appears first ( currently the column N bias is wrong is should read Long then Short.... - BOT could also appear multiple time meaning that the symbol was bought more than one time before exiting the trade ( selling SLD the trade a hypothetical sample is presented in rows 26 through 28. In this case the symbol was BOT twice 50 shares each and then sold once for the total amount of shares bought initially of 100. This is case of Long ( as bought appears first). This only show two but it could be more - the same will be true for short as exemplified in rows 30 through 32. Please note there will not be space between rows is it only to be able to better show these cases. - as well there will be the time that an a postion was exit in more than one SLD ( in the case of a long) exemplified in rows 34 through 37, if this a case the P&L will show for each SLD row - same will be true in the case of a short exemplified in rows 40-43 Columns paratemters, need to be adjusted to accommodate 300 rows of data (initial formula set for only 13 row) Date, correct as it it now with the correct format adjusted in the next column Symbol, correct as it is now Bias, incorrectly set up does not display the information as defined previously Quantity: it needs to be able to accommodate in the case there is more than one BOT in the case of a long or more that one SLD in case of a short. in the case of the first example, it will be the sum of the qty in row 26 and 27. Please do not get confused the SLD qty in row 28 is the same qty so it does not need to be counted. In case of short, it will be the same the sum of the qty in row 30 and 31 Price BOT or sold (depending on whether is short or long) this is the price at which the trade was bough or the entry in case of long or sold in case of short, for example, the first trade row 2 through 3, the BOT price ( or entry) was 15.795. the same will be true for short for example the rows 4 trough 5 are short because SLD comes first, the entry price in this case is 13.42. -please note if there is more than one BOT (or sold if comes first) as it is the case for example in row 26 -27 ( or 30-31 in case of a short) the entry price will need to be the an the sum product of the quatity and the price BOT divided by the total number of shares BOT Price SLD or BOT (depending on whether is short or long) this is the price at which the trade was exit, for example the row 2-3 the exit price was 15.715. the same will whole true for short for example rows 4 trough 5 the exit price was 13.59 please note if there is more than one exit the final exit price shoul be the sum product of the exit price and the quatity divided by the total tumber of shares. Realized P&L, is correct although needs to accomodate if the trade there is more that one P&L which are the case for the sample trade in rows 34-37 and 40-43, it will be the some of these values Entry time: the time at which the trade was taken, if there is more that one BOT in the case of a long or SLD in the case of a short is simply the first BOT or SLD Exit time: the time at which the trade was exit, if there is more than one BOT in the case of a long or SLD in the case of a short is simply the last time that appears BOT or SLD Commission: it is the sum of all BOT and SLD in each trade
11SLDVERU5015.6731.7414:21:29202204141.015Short
12BOTVERU5017.0614:27:312022041416Short
13SLDVERU5017.3914.4914:27:46202204141.016Short
14BOTPLUG1027.036413:56:28202204191
15SLDPLUG1026.885-3.5214:05:10202204191
16BOTAXSM1040.5814:15:55202204191
17SLDAXSM1039.91-8.714:20:49202204191
18BOTPLUG1027.214:23:07202204191
19SLDPLUG1027.3-114:26:58202204191
20SLDAXSM1039.2514:31:36202204191
21BOTPLUG5027.4114:46:51202204191
22SLDPLUG5027.26-9.5114:52:31202204191.01
23BOTAXSM1038.960.914:52:48202204191
24
25example only
26BOTVERU5015.79513:39:16202204141
27BOTVERU5015.79514:39:16202204141Long
28SLDVERU10015.715-6.0113:39:49202204141.01
29
30SLDVERU5015.79513:39:16202204141
31SLDVERU5015.79514:39:16202204141short
32BOTVERU10015.715-6.0113:39:49202204141.01
33
34BOTVERU5015.79513:39:16202204141
35BOTVERU5015.79514:39:16202204141long
36SLDVERU5015.715-6.0113:39:49202204141.01
37SLDVERU5015.715-6.0114:39:49202204141.01
38
39
40SLDVERU5015.79513:39:16202204141
41SLDVERU5015.79514:39:16202204141short
42BOTVERU5015.715-6.0113:39:49202204141.01
43BOTVERU5015.715-6.0114:39:49202204141.01
44
45
46
47
48
Sheet1
Cell Formulas
RangeFormula
L2:L7,O2:O7L2=INDEX(G$2:G$13,MATCH($W2,$I$2:$I$13,0))
M2:M7M2=DATE(LEFT(L2,4),MID(L2,5,2),RIGHT(L2,2))
N2:N7N2=INDEX(B$2:B$13,MATCH($W2,$I$2:$I$13,0))
P2:P7P2=SUMIFS($C$2:$C$13,$I$2:$I$13,W2,$A$2:$A$13,"BOT")
Q2:Q7Q2=AVERAGEIFS($D$2:$D$13,$I$2:$I$13,W2,$A$2:$A$13,"BOT")
R2:R7R2=AVERAGEIFS($D$2:$D$13,$I$2:$I$13,W2,$A$2:$A$13,"SLD")
S2:S7S2=INDEX(E$2:E$13,MATCH($W2,$I$2:$I$13,0)+1)
T2:T7,V2:V7T2=INDEX(F$2:F$13,MATCH($W2,$I$2:$I$13,0))
W2W2=IFERROR(__xludf.DUMMYFUNCTION("unique(I2:I13)"),1)
W3W3=IFERROR(__xludf.DUMMYFUNCTION("""COMPUTED_VALUE"""),2)
W4W4=IFERROR(__xludf.DUMMYFUNCTION("""COMPUTED_VALUE"""),3)
W5W5=IFERROR(__xludf.DUMMYFUNCTION("""COMPUTED_VALUE"""),4)
W6W6=IFERROR(__xludf.DUMMYFUNCTION("""COMPUTED_VALUE"""),5)
W7W7=IFERROR(__xludf.DUMMYFUNCTION("""COMPUTED_VALUE"""),6)
I2:I13I2=COUNT(E$2:E2)+IF(E2="",1,0)
J4:J13J4=IF(ISNUMBER(E3),IF(A3="SLD","Short","Long"),J3)
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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