Move row data left right.

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Using Excel 2010
Hello,

I got some data filled in range C6:P10, example with 5 rows I wonder if data can move randomly in each row without changing any values.

For example row C6:P6 random move in range see the result after macro in H16:P16 vales are the same but has moved randomly. sum of the row has the same, but the sum of column has changed.

Is this possible, if yes it is, please I need only a VBA solution.

Here is below the example with before and after macro results.

ROW-LEFT-RIGHT.xlsx
ABCDEFGHIJKLMNOPQRS
1
2
3
4Before Macro
5R1R2R3R4R5R6R7R8R9R10R11R12R13R14Sum
62471W4TH57Q53139
771222YYX2732403512QWHVA187
827322Q5T59TYW4G757211098
91122235402232HHNTPQD5G7TK5L164
10QWEHJK525P44H6K9N12273314134
11Sum376051885062644040363440515
12
13
14After Macro
15R1R2R3R4R5R6R7R8R9R10R11R12R13R14Sum
16HWT2575174341Q39
1712VAQWYY271272240X32H35187
18G722Q151055T72732TY9W498
19TKHH40D5221325LPQ223512G7NT164
201QW279NJK331252P4EH6K544H134
21Sum13267332786660369370531435
22
23
Sheet1
Cell Formulas
RangeFormula
R16:R20,R6:R10R6=SUM(C6:P6)
C21:P21,C11:P11C11=SUM(C6:C10)


Regards,
Moti
 

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)
This solution requires using some available cells on your sheet, currently it uses cells "C23:P25", but that may change.

Try:
VBA Code:
Sub Move_Data()
  Dim i As Long
  
  For i = 6 To 10
    Range("C23:P23").FormulaR1C1 = "=RAND()"
    Range("C24:P24").FormulaR1C1 = "=RANK.EQ(R[-1]C,R23C3:R23C16)"
    Range("C25:P25").FormulaR1C1 = "=INDEX(R" & i & "C3:R" & i & "C16,0,R[-1]C)"
    
    Range("C" & i & ":P" & i).Value = Range("C25:P25").Value
  Next
  
  Range("C23:P25").ClearContents
End Sub
 
Upvote 1
This solution requires using some available cells on your sheet, currently it uses cells "C23:P25", but that may change.
DanteAmor, yes this worked with simple data I am thankful to you for it.

This has some inconvenient I need to change ranges accordingly data there will always 14 columns and rows can vary 100 to 80000+ could it be some better way regarding formula pure VBA.

Good Luck!

Regards,
Moti
 
Upvote 0
Another approach, using arrays.

VBA Code:
Public Sub Move_Row_Values()

    Dim rowCells As Range
    Dim rowVals As Variant
    
    Randomize
    For Each rowCells In Range("C6:P10").Rows
        rowVals = rowCells.Value
        RandomArrayVals rowVals
        rowCells.Value = rowVals
    Next
    
End Sub


Private Sub RandomArrayVals(vals As Variant)
    Dim i As Long, r As Long
    Dim swap As Variant
    For i = 1 To UBound(vals, 2)
        r = Rnd * (UBound(vals, 2) - 1) + 1
        swap = vals(1, i): vals(1, i) = vals(1, r): vals(1, r) = swap
    Next
End Sub
 
Upvote 1
This has some inconvenient I need to change ranges accordingly data there will always 14 columns and rows can vary 100 to 80000+ could it be some better way regarding formula pure VBA.

Assuming that the last row is the sum, then with the following macro you can have 6, 80000 or whatever, the macro will calculate it automatically.
It will put the entire data set in an array (only one read) and at the end it will put the result (only one write).

VBA Code:
Sub Random_Data()
  Dim a As Variant, b As Variant, arr As Variant
  Dim i&, k&, x&, y&, z&
 
  a = Range("C6:P" & Range("C" & Rows.Count).End(3).row - 1).Value   'Input
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
 
  Randomize
  For i = 1 To UBound(a, 1)
    arr = Evaluate("ROW(1:" & UBound(a, 2) & ")")
    k = UBound(arr)
    For z = 1 To k
      x = Int(Rnd * k + z)
      y = arr(z, 1)
      arr(z, 1) = arr(x, 1)
      arr(x, 1) = y
      k = k - 1
      b(i, z) = a(i, arr(z, 1))
    Next
  Next
  Range("C6").Resize(UBound(b, 1), UBound(b, 2)).Value = b  'output
End Sub

😇
 
Upvote 1
Solution
Another approach, using arrays.

VBA Code:
Public Sub Move_Row_Values()

    Dim rowCells As Range
    Dim rowVals As Variant
   
    Randomize
    For Each rowCells In Range("C6:P10").Rows
        rowVals = rowCells.Value
        RandomArrayVals rowVals
        rowCells.Value = rowVals
    Next
   
End Sub


Private Sub RandomArrayVals(vals As Variant)
    Dim i As Long, r As Long
    Dim swap As Variant
    For i = 1 To UBound(vals, 2)
        r = Rnd * (UBound(vals, 2) - 1) + 1
        swap = vals(1, i): vals(1, i) = vals(1, r): vals(1, r) = swap
    Next
End Sub
John_w, thank you for giving an appropriate solution, I like the method of sorting rows, one by one. (y)

Thank you for your time to help. Have a good day and Good Luck!

Kind Regards,
Moti :)
 
Upvote 0
Assuming that the last row is the sum, then with the following macro you can have 6, 80000 or whatever, the macro will calculate it automatically.
It will put the entire data set in an array (only one read) and at the end it will put the result (only one write).

VBA Code:
Sub Random_Data()
  Dim a As Variant, b As Variant, arr As Variant
  Dim i&, k&, x&, y&, z&
 
  a = Range("C6:P" & Range("C" & Rows.Count).End(3).row - 1).Value   'Input
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
 
  Randomize
  For i = 1 To UBound(a, 1)
    arr = Evaluate("ROW(1:" & UBound(a, 2) & ")")
    k = UBound(arr)
    For z = 1 To k
      x = Int(Rnd * k + z)
      y = arr(z, 1)
      arr(z, 1) = arr(x, 1)
      arr(x, 1) = y
      k = k - 1
      b(i, z) = a(i, arr(z, 1))
    Next
  Next
  Range("C6").Resize(UBound(b, 1), UBound(b, 2)).Value = b  'output
End Sub

😇
DanteAmor, thank you revising and giving a macro solution yes it worked as you described put the result (only one write). And it is fast too.

Thank you for your time to help. Have a good day and Good Luck!

Kind Regards,
Moti :)
 
Upvote 0
DanteAmor, thank you revising and giving a macro solution yes it worked as you described put the result (only one write). And it is fast too.

Thank you for your time to help. Have a good day and Good Luck!
Glad we could help. Thanks and have a good day too.

😇
 
Upvote 1

Forum statistics

Threads
1,224,823
Messages
6,181,177
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