Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 3rd, 2007, 01:30 PM
Authorized User
 
Join Date: Jan 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Randomize a range

Hi
Hope one of you out there can help me.

I have a range of selected cells.
How can i rearrange (shuffle) the selected cells in random order?

Thanks
Frank

 
Old July 6th, 2007, 02:53 PM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

You'll have to swap content manually. This is a very simple function that randomizes a random number of times, swapping rows as it goes. To Grid, you could randomize a column number as well for both From and To.

Fill column A with several numbers leaving no blank rows in that column and Try this:
-------------------------------------------------------------------------
Private Sub CommandButton1_Click()

'There are many different ways you could do this.
'Below is a basic routine for randomizing

  Dim vHold As Variant, iFrom As Long, iTo As Long, iRows As Long, iTimes As Long, iCnt As Long
  If ActiveSheet.Cells(2, 1).Value = "" Then Exit Sub

  With ActiveSheet
    iRows = .Range("A1").End(xlDown).Row
    iTimes = Int(Rnd(1) * iRows * 1.5) + 5
' Swaps a random number of items anywhere from 5 to 1.5 * rows with data times
    For iCnt = 1 To iTimes
      iFrom = Int(Rnd(1) * iRows) + 1
      iTo = Int(Rnd(1) * iRows) + 1
      vHold = .Cells(iFrom, 1).Value
      .Cells(iFrom, 1).Value = .Cells(iTo, 1).Value
      .Cells(iTo, 1).Value = vHold
    Next
  End With

End Sub
-------------------------------------------------------------------------

Hope this points you in the right direction.

 
Old July 18th, 2007, 04:01 AM
Authorized User
 
Join Date: Jan 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you.
This will get me further.
Frank

 
Old July 18th, 2007, 04:12 AM
Authorized User
 
Join Date: Jan 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Works fine
Thanks again, Allen

 
Old February 23rd, 2016, 06:24 PM
Registered User
 
Join Date: Feb 2016
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ramdomize a range in two coulmns

code is Great! but how can it be modified to randomize a range in two columns? But still keep the rows together:

for example:

1 1
2 2
3 3

Randomized would appear:

2 2
3 3
1 1

Thank you,

-Justin
 
Old February 24th, 2016, 10:26 AM
Registered User
 
Join Date: Feb 2016
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by justair07 View Post
code is Great! but how can it be modified to randomize a range in two columns? But still keep the rows together:

for example:

1 1
2 2
3 3

Randomized would appear:

2 2
3 3
1 1

Thank you,

-Justin
Same question asked at: http://www.excelguru.ca/forums/showt...2803#post22803

Wasn't sure if op was still active(allenm) great code either way!!





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to loop through a range? chobo XSLT 3 May 24th, 2008 03:50 PM
Randomize value Lofa Dreamweaver (all versions) 1 September 28th, 2005 03:43 PM
Regular Expression for Randomize the sequence srinivasp20 Apache Tomcat 0 July 15th, 2005 02:20 AM
Getting chart range help munkiller Excel VBA 1 June 24th, 2005 05:37 AM
Need Help With Date Range rpainter Crystal Reports 0 June 20th, 2005 01:01 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.