Excel table There is such a table excel. It is necessary to sort the data (white fields) in random order in unbound lines. How can this be done?

There is a sorting experience with

=CΠ›Π§Π˜Π‘() 

but how to be when the lines are not connected? Do you have to copy the data to a separate sheet, then add a column with

 =Π‘Π›Π§Π˜Π‘() 

and then how to sort them out? :]

Are there any better methods than this spherical horse in a vacuum?

Thank you, comrades, for instructing on the right path!

    1 answer 1

    Solution without macros , only using Excel formulas, sorting by several columns and dividing a sheet of the Π˜ΡΡ…ΠΎΠ΄Π½ΠΎΠΉ Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ into sheets:

    Π”Π°Π½Π½Ρ‹Π΅ - contains data with additional columns having a formula for sorting;

    Π€ΠΎΡ€ΠΌΡƒ - displays data in the required format randomly.

    In steps:

    1. We have a sheet Π˜ΡΡ…ΠΎΠ΄Π½Π°Ρ Ρ‚Π°Π±Π»ΠΈΡ†Π°

    one

    1. Create a Π”Π°Π½Π½Ρ‹Π΅ sheet, in front of the columns that need to be sorted in a random order, insert new columns with the functions Π‘Π›Π§Π˜Π‘Π›() .

    2

    For example, the added columns: Π‘Π» НаимСнованиС Ρ‚ΠΎΠ²Π°Ρ€Π° , Π‘Π» ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ 2 .

    1. To sort by multiple columns, use Π”Π°Π½Π½Ρ‹Π΅ / Π‘ΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²ΠΊΠ° . In the example columns: Π‘Π» НаимСнованиС Ρ‚ΠΎΠ²Π°Ρ€Π° , Π‘Π» ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ 2 .

    Note: when you select ОК , the sorting function will be sorted and immediately recalculated Π‘Π›Π§Π˜Π‘Π›() .

    3

    1. To display the data in the required format, create a Π€ΠΎΡ€ΠΌΠ° sheet.

    four

    1. To display the values ​​in the fields, we use the formula for the first row =Π˜ΠΠ”Π•ΠšΠ‘(Π”Π°Π½Π½Ρ‹Π΅!$A$1:$F$6;БВРОКА();3) , which refers to the second row of the third column of the Π”Π°Π½Π½Ρ‹Π΅ sheet. For correct display of the third and the following lines, the formula taking into account the offset of the lines - =Π˜ΠΠ”Π•ΠšΠ‘(Π”Π°Π½Π½Ρ‹Π΅!$A$1:$F$6;БВРОКА()/2+1;3)

    five

    1. It all works in the following way: the RINOUND () function assigns a random value to cells, when Π‘ΠΎΡ€Ρ‚ΠΈΡ€ΠΎΠ²ΠΊΠΈ applied on the Π”Π°Π½Π½Ρ‹Π΅ sheet, the order of values ​​in the cells is changed, the result of sorting by reference is displayed on the Π€ΠΎΡ€ΠΌΠ° sheet.

    2. If necessary, the cells from the Π€ΠΎΡ€ΠΌΠ° sheet are copied and the values ​​are inserted on another sheet.

    Solution using macros. The basic idea: the cell values ​​necessary for sorting are copied to the original array, which is sorted randomly (we get the resulting array) and then written to the table cells.

    In steps:

    1. Macro code

       Option Explicit ' Π²ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ явноС объявлСния всСх ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Π½Π½Ρ‹Ρ… Option Base 1 ' объявляСм ниТнюю Π³Ρ€Π°Π½ΠΈΡ†Ρƒ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ для индСксов массивов ' основная идСя: значСния ячССк Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΡ‹Π΅ для сортировки ΠΊΠΎΠΏΠΈΡ€ΡƒΠ΅ΠΌ Π² исходный массив, ' ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ сортируСтся случайным ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ (ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚ΠΈΡ€ΡƒΡŽΡ‰ΠΈΠΉ массив) ΠΈ Π΄Π°Π»Π΅Π΅ ' записываСм Π² ячСйки Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ Sub RandomOrderTable() Dim sSrtblClmn As String ' строка содСрТащая Π½ΠΎΠΌΠ΅Ρ€ столбцов, Π² ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΡ‚ΡŒΡΡ сортировка ячССк Dim aSrtblClmn() As String ' массив для хранСния Π½ΠΎΠΌΠ΅Ρ€ΠΎΠ² столбцов, Π² ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Π±ΡƒΠ΄Π΅Ρ‚ производится сортировка ячССк Dim aRw() As Variant ' исходный массив со значСниями ячССк Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ Dim aRndRw() As Variant ' Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚ΠΈΡ€ΡƒΡŽΡ‰ΠΈΠΉ массив со значСниями элСмСнтов aRw отсортированных случайным ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ Dim i As Integer Dim k As Integer Dim l As Integer ' индСкс исходного массива (количСства ячССк), ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠΎΠ΄Π²Π΅Ρ€Π³Π½ΡƒΡ‚ сортировкС Dim iLstRw As Integer ' количСство строк исходной Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ Dim iClrIndx As Integer ' Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ индСкса Ρ†Π²Π΅Ρ‚ Π·Π°Π»ΠΈΠ²ΠΊΠΈ для строк Π½Π΅ ΡƒΡ‡Π°ΡΡ‚Π²ΡƒΡŽΡ‰ΠΈΡ… Π² сортировкС iLstRw = Cells.SpecialCells(xlLastCell).Row ' ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠ»ΠΈ количСсвто строк исходной Ρ‚Π°Π±Π»ΠΈΡ†Ρ‹ sSrtblClmn = "2;4" aSrtblClmn = Split(sSrtblClmn, ";") iClrIndx = Cells(2, 1).Interior.ColorIndex ' ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠ»ΠΈ индСкс Ρ†Π²Π΅Ρ‚Π° ячСйки Π±Π΅Π· Π·Π°Π»ΠΈΠ²ΠΊΠΈ ' Ρ†ΠΈΠΊΠ» ΠΏΠ΅Ρ€Π΅Π±ΠΎΡ€Π° столбцов, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π±ΡƒΠ΄ΡƒΡ‚ ΠΊΠΎΠΏΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒΡΡ Π² массив For i = LBound(aSrtblClmn) To UBound(aSrtblClmn) l = 1 ' Ρ†ΠΈΠΊΠ» ΠΏΠ΅Ρ€Π΅Π±ΠΎΡ€Π° строк, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π±ΡƒΠ΄ΡƒΡ‚ ΠΊΠΎΠΏΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒΡΡ Π² массив For k = 1 To iLstRw ' провСряСм Ρ„ΠΎΠ½ ячСйки, Ссли Π·Π°Π»ΠΈΠ²ΠΊΠΈ Π½Π΅Ρ‚, Ρ‚ΠΎ ΠΊΠΎΠΏΠΈΡ€ΡƒΠ΅ΠΌ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ячСйки Π² исходный массив If Cells(k, Int(aSrtblClmn(i))).Interior.ColorIndex = iClrIndx Then ' пСрСопрСдСляСм массив ΠΈ заносим Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ячСйки Π² массив ReDim Preserve aRw(l) aRw(l) = Cells(k, Int(aSrtblClmn(i))).Value l = l + 1 End If Next k ' сортируСм массив случайным ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ ΠΈ сохраняСм Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ Π² Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚ΠΈΡ€ΡƒΡŽΡ‰Π΅ΠΌ массивС aRndRw = SortRndArray(aRw) l = 1 ' записываСм значСния Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚ΠΈΡ€ΡƒΡŽΡ‰Π΅Π³ΠΎ массива Π² ячСйки For k = 1 To iLstRw If Cells(k, Int(aSrtblClmn(i))).Interior.ColorIndex = iClrIndx Then Cells(k, Int(aSrtblClmn(i))).Value = aRndRw(l) l = l + 1 End If Next k Next i End Sub ' функция случайной сортировки взята ΠΏΠΎ ссылкС http://hav.su/index.php?option=com_content&view=article&id=64:-2&catid=35:calc Function SortRndArray(A As Variant) As Variant 'B - массив-Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ Dim B As Variant 'случайный индСкс элСмСнта массива Dim MyRndValue As Byte Dim i As Integer 'Max_i - максимальноС количСство элСмСнтов Dim Max_i As Integer Max_i = UBound(A, 1) ReDim B(LBound(A, 1) To UBound(A, 1)) As Variant 'Π·Π°ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ Π²Ρ‹Ρ…ΠΎΠ΄Π½ΠΎΠ³ΠΎ массива B 'Π² ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ»ΡŒΠ½ΠΎΠΌ порядкС выбирая ΠΈΡ… ΠΈΠ· A For i = LBound(A, 1) To UBound(A, 1) 'ΠΎΠΏΡ€Π΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ Π½ΠΎΠΌΠ΅Ρ€Π° элСмСнта ΠΈΠ· A MyRndValue = Int((Max_i * Rnd) + 1) 'занСсСниС этого элСмСнта Π² Π²Ρ‹Ρ…ΠΎΠ΄Π½ΠΎΠΉ массив B(i) = A(MyRndValue) 'Π·Π°ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ Π²Ρ‹Π±Ρ€Π°Π½Π½ΠΎΠ³ΠΎ элСмСнта ΠΈΠ· A послСдним элСмСнтом ΠΈΠ· A A(MyRndValue) = A(Max_i) 'ΠΏΠΎΠ½ΠΈΠΆΠ΅Π½ΠΈΠ΅ Π²Π΅Ρ€Ρ…Π½Π΅ΠΉ Π³Ρ€Π°Π½ΠΈΡ†Ρ‹ массива 'Ρ‚.ΠΎ. "ΡƒΠΌΠ΅Π½ΡŒΡˆΠ°Π΅ΠΌ" Ρ€Π°Π·ΠΌΠ΅Ρ€ массива Π½Π° ΠΎΠ΄ΠΈΠ½ элСмСнт Max_i = Max_i - 1 Next i 'Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ массив Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ B SortRndArray = B End Function 
    2. Copy the macro code into the Excel ( Π Π°Π·Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ - Vsual Basic );

    3. Source table:

    23

    1. Run the macro ( Alt+F8 )

    24

    1. We get the result:

    25