how to sort and then move data to new column?

livermore

New Member
Joined
Feb 23, 2011
Messages
39
Hi.

I've been debating on how to do this but so far couldn't find a way.
I have 3 columns, A, B and C.
Column A show my buy and sell orders.
Column B show how many products I bought and sold
Column C show the price at which I bought and sold

My goal is to sort columns A, B and C and transfer that sorted data into Column D and E.
My reading at this is as follow, if A2 = Buy or Sell then copy C2 to D2 or E2. 1 product sold, nothing more to do.
Now the problem comes in B2 where I have 2 products sold. I need to have 2 times the price which I sold that product into the list.
Also, the orders need to be aligned, meaning, first order was a buy then go to the first row on Buy column. Second order was a sell then goes to first row of Sell column.
How to do all this?


ABCDE
BUYSELL
Buy1280328032803.25
Sell22803.2528032803.25
Sell32804.528032804.50
Sell32804.252803.252804.50
Buy228032803.252804.50
Buy52803.252803.252804.25
Sell22803.752803.252804.25
Buy228052803.252804.25
28052803.75
28052804.75

<tbody>
</tbody>


Thank you for the help.
Regards
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
livermore,

If you're comfortable with a vba approach you might consider the following...

Code:
Sub SortMove()
Dim rng As Range, r As Range
Set rng = Range("A2", Cells(Rows.Count, "C").End(xlUp))
rng.Select

With ActiveSheet.Sort
    .SortFields.Clear
    .SortFields.Add Key:=Range("A2:A9"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SortFields.Add Key:=Range("B2:B9"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    .SortFields.Add Key:=Range("C2:C9"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        
    .SetRange rng
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

For Each r In rng.Columns(1).Rows
    If r.Value = "Buy" Then
        Cells(Rows.Count, "D").End(xlUp).Offset(1, 0).Resize(r.Offset(0, 1)).Value = r.Offset(0, 2)
    Else
        Cells(Rows.Count, "E").End(xlUp).Offset(1, 0).Resize(r.Offset(0, 1)).Value = r.Offset(0, 2)
    End If
Next r
End Sub

I did note the last entry in Column E of your sample data (2804.75) doesn't exist anywhere in Column C of your sample data; assuming the sample data is incomplete or in error.

Cheers,

tonyyy
 
Upvote 0
Not saying I can do this but the question is do you want this performed via VBA or formulas?

Just for anyone else who wants to tackle this it looks like you want the values in column C placed in D or E depending on column A being BUY or SELL but you also want the value to appear a set number of times indicated by column B.

UPDATE: Too late, nice one Tonyyy

I'd be interested in seeing a formulaic solution to this
 
Last edited:
Upvote 0
Many thanks Tonyyy

The 2804.75 in column E is in fact an error.

Unfortunately my VBA knowledge is... zero.
Will need to research on how to apply the formula on Excel.

Again I thank you for your help.
Regards.
 
Upvote 0
You're very welcome, livermore...

Just to be clear, vba is programming code, not formulas.

Contextures has easy-to-follow tutorials on how to add and run code. (The code provided should be placed into a Regular Module.)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,869
Messages
6,122,012
Members
449,060
Latest member
LinusJE

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