How to randomize Excel rows - excel

How can I randomize lots of rows in Excel?
For example I have an excel sheet with data in 3 rows.
1 A dataA
2 B dataB
3 C dataC
I want to randomize the row order. For example
2 B dataB
1 A dataA
3 C dataC
I could make a new column and fill it with random numbers using =RAND() and sort based on that column.
But is this the best way to do it? The RAND equation will provide up to a million random numbers and I have a quarter of a million rows so it seems like it would work.
Thanks
I searched for a bit and while this answer about randomizing columns is close it seems like way overkill.

Perhaps the whole column full of random numbers is not the best way to do it, but it seems like probably the most practical as #mariusnn mentioned.
On that note, this stomped me for a while with Office 2010, and while generally answers like the one in lifehacker work,I just wanted to share an extra step required for the numbers to be unique:
Create a new column next to the list that you're going to randomize
Type in =rand() in the first cell of the new column - this will generate a random number between 0 and 1
Fill the column with that formula. The easiest way to do this may be to:
go down along the new column up until the last cell that you want to randomize
hold down Shift and click on the last cell
press Ctrl+D
Now you should have a column of identical numbers, even though they are all generated randomly.
The trick here is to recalculate them! Go to the Formulas tab and then click on Calculate Now (or press F9).
Now all the numbers in the column will be actually generated randomly.
Go to the Home tab and click on Sort & Filter. Choose whichever order you want (Smallest to Largest or Largest to Smallest) - whichever one will give you a random order with respect to the original order. Then click OK when the Sort Warning prompts you to Expand the selection.
Your list should be randomized now! You can get rid of the column of random numbers if you want.

I usually do as you describe:
Add a separate column with a random value (=RAND()) and then perform a sort on that column.
Might be more complex and prettyer ways (using macros etc), but this is fast enough and simple enough for me.

Here's a macro that allows you to shuffle selected cells in a column:
Option Explicit
Sub ShuffleSelectedCells()
'Do nothing if selecting only one cell
If Selection.Cells.Count = 1 Then Exit Sub
'Save selected cells to array
Dim CellData() As Variant
CellData = Selection.Value
'Shuffle the array
ShuffleArrayInPlace CellData
'Output array to spreadsheet
Selection.Value = CellData
End Sub
Sub ShuffleArrayInPlace(InArray() As Variant)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ShuffleArrayInPlace
' This shuffles InArray to random order, randomized in place.
' Source: http://www.cpearson.com/excel/ShuffleArray.aspx
' Modified by Tom Doan to work with Selection.Value two-dimensional arrays.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim J As Long, _
N As Long, _
Temp As Variant
'Randomize
For N = LBound(InArray) To UBound(InArray)
J = CLng(((UBound(InArray) - N) * Rnd) + N)
If J <> N Then
Temp = InArray(N, 1)
InArray(N, 1) = InArray(J, 1)
InArray(J, 1) = Temp
End If
Next N
End Sub
You can read the comments to see what the macro is doing. Here's how to install the macro:
Open the VBA editor (Alt + F11).
Right-click on "ThisWorkbook" under your currently open spreadsheet (listed in parentheses after "VBAProject") and select Insert / Module.
Paste the code above and save the spreadsheet.
Now you can assign the "ShuffleSelectedCells" macro to an icon or hotkey to quickly randomize your selected rows (keep in mind that you can only select one column of rows).

Use Excel Online (Google Sheets).. And install Power Tools for Google Sheets.. Then in Google Sheets go to Addons tab and start Power Tools. Then choose Randomize from Power Tools menu. Select Shuffle. Then select choices of your test in excel sheet. Then select Cells in each row and click Shuffle from Power Tools menu. This will shuffle each row's selected cells independently from one another.

For example this is our data set.
Then type this formular and add it B1 tO B9 cell.
Now you can go to Data tab, and select Sort smallest to largest or Sort largest to smallest as you need.
***Then there is a popped dialog, and check Expand the selection option. And click Sort.
Data range has been shuffled by rows randomly.Then you can remove the formula cells.(B column)

Related

Find currency and amount, also ID store in array and paste in Output sheet

I would like to find the text "Currency" in columns A or B, store all the currencies listed under Currency. Same process for Amount (Can be in an column)
Store values in an array. Then paste in Output Sheet. The currencies will already be listed in Output sheet in 1st row of the sheet. However if it is a new currency then the code should find last used cell in row 1 and add it. The value of Amount should be added to Output sheet against the currency and ID number also copied from the Source sheet.
I have some code.
Public Sub loopRow()
Dim curArray As Variant
Dim listarray As Variant
Dim cnt As Long
'Find Currency
Dim rgFound As Range
Set rgFound = Range("A:B").Find("Currency")
'Find last used row
curArray = Cells(rgFound.Address).End(xlUp).Row
'Transpose list of currecny from the row down from the word Currency that it has found
listarray = Application.Transpose(Cells(Rows, curArray).End(xlUp)).Row
For cnt = LBound(curArray) To UBound(curArray)
curArray(cnt) = curArray(cnt)
Next cnt
For cnt = LBound(curArray) To UBound(curArray)
'Debug.Print curArray(cnt)
'Copy and paste into Sheet under the correct curreny, if new currency then add this in row A
Next cnt
End Sub
Whilst you need to understand your question is unanswerable as is, I'll do my best to help.
The problem we have is not seeing the source sheet the way you do, as we can't see it at all. You say you have the word Currency in columns A or B or both, and an ID column somewhere, and Amount values everywhere. That's tricky source data. If as is more likely, the ID is in a specific column and the amounts are in a set of columns, then we'd have a chance.
Your question outlines the basic steps you'd want to take pretty well, so you're off to a good start.
However you can do all of the work without VBA, certainly if I'm right about the Source data. Create yourself a working sheet, or multiple working sheets. Definitely one to sort out the full list of currencies. Grab a copy of columns A and B (by formulae) and then have the working sheet go through line by line and use logic to build the list. Spreadsheets are great at this.
Once you have the list, use it as row headers on your Output sheet and use sumifs to get the values. I am not sure how the IDs would fit in, but if they were to be your row headings, then do the same as the above to get the list of unique ids and link them into your Output page in column A. Your sumifs can handle that.
That will hardly tell you all you need to know, but if you work it out you'll have learned a lot about Excel and when you need to go into VBA.
If you'd rather do it with VBA, break down each step until it works, and then go onto the next one.
And if you want more help, paste your data in here. Anonymise it first if you need to.

Select all rows where all cells equal one another

I have a large table that only shows a single type of information: whether or not a species of plant was present at a particular study site. I have 500+ species listed in the first column, and 30 sites as column names. The table is populated with a simple "Y" or "N" to show presence. Example:
Scientific Name Old Wives Beach Dadi Orote N Airstrip
Abelmoschus moschatus N N N
Abrus precatorius Y N Y
Abutilon indicum N N N
However, the species list contains some species that do not occur at any sites, rendering a row full of "N"s, like the 1st and 3rd rows in the example above. I need to delete those rows in order to make the table more manageable.
Is there any way to achieve this without a long IF AND statement?
Inspired by pnuts' comment, in a new column, use the a COUNTIF() formula. For example, =COUNTIF(B2:AE2,"Y"), assuming the row/column headers are in row 1 and column A and the data is in the range B2:AE501+.
If you then select the entire range, including the headers and the new formula column and add filters, then you can select only the rows where the count of Y's is 0. Once you have only the 0's showing, you can select the entire rows and delete them (using Right-Click, Delete) without effecting the non-zero rows.
At this point, if you no longer need the count column, you can turn off the filter and delete the column but I wouldn't be surprised if you find the count comes in handy for some other reason.
Alternately, you could just use the filter to HIDE the 0 rows rather than delete them and that way to don't remove the data altogether but it's no longer in your way.
The code below is one way to do this, assuming there are no gaps in the data. The animated gif steps through to demonstrate how it works. You should remove the .select statements once you understand it.
Sub deleteIfAllN()
Dim plantR As Range, cell As Range, allN As Boolean
Set plantR = Range("A2")
While plantR <> ""
plantR.Select
Set r = plantR.Offset(0, 1)
allN = True
Do
r.Select
If r <> "N" Then
allN = False
Exit Do
End If
Set r = r.Offset(0, 1)
Loop Until r = ""
Set plantR = plantR.Offset(1, 0)
Rows(plantR.row - 1).Select
If allN Then Rows(plantR.row - 1).Delete
Wend
End Sub
You can use the Advanced Filter
Set up your data and criterion area as below
For the example you posted, the formula would be:
=COUNTIF($B8:$D8,"N")<>3
For 30 columns, just modify the range and the count.
Before
After
I chose to filter in place
Note that there is also an option to Copy to another location which would place the results of the filter in another location.

Randomly fill in a table of 25 values, that come from a set of 30 values?

I want to make a series of tables that each contain 25 values, that come from a set of 30 values. How can I quickly, and randomly, produce these tables? I'm wondering if there is a way in excel, or will I need to program something, myself? If so, which language would be the easiest (Python, C, Java)?
Edit: The 25 values would include no repeats. In other words, I'm looking for random combinations (30C25) of the values.
You can eventually add the following custom User-Defined function and then use it as an array formula. Add the code to code module Module1:
Public Function RandUnique(src As Range) As Variant
Dim v As Variant: v = src
Randomize
Dim i As Integer, j As Integer, temp As Variant
For i = 1 To src.Rows.count
j = 1 + Int(Rnd * src.Rows.count)
temp = v(i, 1): v(i, 1) = v(j, 1): v(j, 1) = temp
Next
RandUnique = v
End Function
Once you have added this UDF,
Select the destination range, enter in the formula bar the following formula
=RandUnique($A$1:$A$30) ' <~~ set it to your source range of 30 values
Then press Ctrl+Shift+Enter
Please note that the randomization procedure used is rather basic, so that not all the combinations have really equal probability, but it is fair enough, unless you are using it for some deep statistical analysis, in which case you might need a perfect randomizer.
Name a list of your thirty values in Excel in rows greater than 25 (say List30), then in A1 copied down to A25 and all copied across to suit:
=INDEX(List30,RANDBETWEEN(1,30))
To exclude repetitions (so not random choices) you might enter your list in A1:A30 and in B1 copied down to suit:
=RAND()
then sort A:B on ColumnB and copy A1:A25 to paste say to D1. This way only one set is generated at a time (the sort/copy/paste would have to be repeated after each paste).
Here is an interesting way that requires no VBA nor any manual sorting.
Enter your source list in the range A1:A30.
In cell B1 enter this formula:
=CHOOSE(RANDBETWEEN(1,7),7,11,13,17,19,23,29)
In the range C1:C30 enter this formula:
=INDEX(A$1:A$30,MOD(ROWS(A$1:J1)*B$1,30))
In cell D1 enter this formula:
=RANDBETWEEN(1,30)
Now select any 25 contiguous vertical and empty cells and enter this formula:
=INDEX($C$1:$C$30,MOD(D$1+ROWS($A$1:$J1),30)+1)
Now copy the 25 cells and paste as values somewhere for your 1st table. Press F9 on the keyboard to get a fresh 25; copy and paste as values somewhere for your 2nd table. Press F9 on the keyboard to get a fresh 25; copy and paste as values somewhere for your 3rd table. Keep repeating for as many tables as you need.
.
Please note that while this will look very random (with no repeats) it is not random at all. It's a complex interference pattern that will appear completely random unless you are the Rain Man.

I have filtered my Excel data and now I want to number the rows. How do I do that?

Basically all I want to do is to insert a new column after having filtered my data by a certain criterion, and then insert consecutive numbers into that column, one for each row. I.e., I have data like this in one column:
Armstrong, John
Beattie, Jane
Coombs, John
And I want a new column running next to it so it looks like:
1 Armstrong, John
2 Beattie, Jane
3 Coombs, John
I have tried inputting the first few numbers and then dragging down to fill the rest of the column but when I do that all of the numbers turn to 1 for some reason.
Okay I found the correct answer to this issue here
Here are the steps:
Filter your data.
Select the cells you want to add the numbering to.
Press F5.
Select Special.
Choose "Visible Cells Only" and press OK.
Now in the top row of your filtered data (just below the header) enter the following code:
=MAX($"Your Column Letter"$1:"Your Column Letter"$"The current row for the filter - 1") + 1
Ex:
=MAX($A$1:A26)+1
Which would be applied starting at cell A27.
Hold Ctrl and press enter.
Note this only works in a range, not in a table!
Try this function:
=SUBTOTAL(3, B$2:B2)
You can find more details in this blog entry.
I had the same problem. I'm no expert but this is the solution we used:
Before you filter your data, first create a temporary column to populate your entire data set with your original sort order. Auto number the temporary "original sort order" column. Now filter your data. Copy and paste the filtered data into a new worksheet. This will move only the filtered data to the new sheet so that your row numbers will become consecutive. Now auto number your desired field. Go back to your original worksheet and delete the filtered rows. Copy and paste the newly numbered data from the secondary sheet onto the bottom of your original worksheet. Then clear your filter and sort the worksheet by the temporary "original sort order" column. This will put your newly numbered data back into its original order and you can then delete the temporary column.
Add a column for example 'Selected' First.
Then Filter your data.
Go to the column 'Selected'. Provide any proxy text or number to all rows. like '1' or 'A' - now your hidden Rows are Blank
Now, Clear Filter and Use Sorting - two levels
Sort by - 'Selected' Ascending - this leaves blank cells at bottom
Add Sort Level - 'Any column you Desire' your order
Now, Why dont you drag the autofill yourself.
Oops, I have no reputation here.
I had the same need to fill up a column with a sequence series for each value on another column. I tried all the answers above and could not fix the problem. I solved it with a simple VBA macro.
My data have the same structure (but with 3000 rows):
N2 is the column on which the table is filtered;
N3 is the column where I wanted to fill a series;
A | B
N2 | N3
1 | 1
2 | 1
3 | 1
1 | 2
6 | 1
4 | 1
2 | 2
1 | 3
5 | 1
Here below the code:
> Sub Seq_N3() ' ' Seq_N3 Macro ' Sequence numbering of N3 based on N2 value
> do N2
> Dim N2 As Integer
> Dim seq As Integer
>
> With ActiveSheet
>
> For N2 = 1 To 7 Step 1
> seq = 1 '
> .Range("B2").Select '
>
> Do While ActiveCell.Offset(0, -1).Value2 <> 0
>
> If ActiveCell.Offset(0, -1).Value2 = N2 Then
> ActiveCell.Value2 = seq
> seq = seq + 1
> ActiveCell.Offset(1, 0).Select
> Else
> ActiveCell.Offset(1, 0).Select
> End If
>
> Loop
>
> Next N2
>
> End With End Sub
Hope it helps!
Step 1: Highlight the entire column (not including the header) of the column you wish to populate
Step 2: (Using Kutools) On the Insert dropdown, click "Fill Custom List"
Step 3: Click Edit
Step 4: Create your list (For Ex: 1, 2)
Step 5: Choose your new custom list and then click "Fill Range"
DONE!!!
Easiest way do this is to remove filter, fill series from top of total data. Filter your desired data back in, copy list of numbers into a new sheet (this should be only the total lines you want to add numbering to) paste into column A1. Add "1" into column B1, right click and hold then drag down to end of numbers and choose "fill series". Now return to your list with filters and in the next column to the right "VLOOKUP" the filtered number against the list you pasted into a new sheet and return the 2nd value.
The best option would be using formula =Row()-Row(ColumnHeaderCell) then Apply it to the whole Column [But this not works in filter row number]
Although the methods described in this thread allow sequential numbers to be added to visible cells in a filtered range, the formulas for doing so are likely to break if the cells currently hidden by the filter have existing values. Ideally, you would just copy the filtered range after adding formulas to number them--but you will get a "no can do" message because of the breaks in the selected range. The workaround is to use a currently blank auxiliary column.
The instructions that follow (taken from my answer in an Experts Exchange thread) assume that a filtered range (cells B5:B7147) needs new sequential numbers starting with cell B116.
The overall approach uses F5...Special Cells to select just the cells we care about at the moment. And we will be using an auxiliary column to store intermediate values.
The starting point is with filter applied, and you want to update the numbers in column B starting with cell B116. We will use column K as the auxiliary column. And we note that the data extends through row 7147.
Put 9918 (the next sequential number) in cell K116. Then select the range of cells from K117 to K7147. Now use F5...Special Cells...Visible cells ribbon item to select just the cells in column K that need new sequential numbers. Click in the formula bar and type the formula shown below. Hold the Control key down and hit Enter to put sequential numbers in the visible cells.
`=MAX(K$116:K116)+1`
Now clear the AutoFilter. In the address bar (just above the intersection of row numbers and column letters), type K5:K7147 and hit Enter. This will instantly select those cells without need to drag the cursor down through thousands of rows.
Next, use F5...Special Cells...Blanks to select the blank cells in column K. In cell K5 Control + Enter the formula shown below.
=IF(B5="","",B5)
Use the trick with the address bar to select K5:K7147. Copy those cells. Next, use the address bar trick to select B5:B7147. Now do a Paste Special...Values. You now have the desired numbers in column B without formulas and without any of the originally hidden values being overwritten.
Finally, you may clear (or delete) the auxiliary column K.
Try this:
On first row set value 1 (e.g cell A1)
on next row set: =A1+1
Finally autocomplete the remaining rows

How can I get the length of the longest string in a column (Excel)?

I have an Excel spreadsheet that I am writing out to a file. I am currently using the following method to reproduce the contents of each cell:
cell_contents = Right(Space(10) & Trim(Cells(iRow, iColumn)), 10)
However, if the contents of the cell are longer than 10 characters long (or however long I choose to specify), then I loose parts of the data. Is there a way to quickly and easily get the length of the longest string in a range of cells?
I have seen people suggest using the following loop, but I was hoping that I might be able to do it without having to loop over all the cells:
For Each c In SourceRange.Cells
if len(c) > b then b = len(c)
Next c
Record these steps as a macro in the cell you want to calculate the max length.
1) enter this formula
=MAX(LEN(A1:A4), 1) -- Edit for your range.
2) press control-shift enter (FormulaArray)
3) now stop recording macro
4) view the VBA. copy what you need to your VBA.
Should give you something like this:
Selection.FormulaArray = "=MAX(LEN(R[-10]C[1]:R[-1]C[1]))"
Press Alt-F11, insert new module, paste code bellow.
Public Function maxRangeLength(data As Range) As Integer
Dim ret As Integer
ret = 0
For Each cell In data
ret = Application.Max(ret, Len(cell))
Next cell
maxRangeLength = ret
End Function
Usage:
=maxRangeLength(A8:D11)
Could just use ... {=MAX(LEN(A2:A200))}
In the top cell put =MAX(LEN(A2:A200)) .. and then press CTRL-SHIFT-ENTER to get the curly braces.
Suppose that the data is in column A and there is a heading row for your spreadsheet.
Create a new column next to it and enter in the formula: =LEN(A2)
Autofill down that formula.
Turn on filtering for that column and the click the drop-down box on the column heading cell.
Scroll down to the bottom of the list where the largest summarized value is.
That will be the largest value in the column.

Resources