Consulting

Results 1 to 2 of 2

Thread: How to randomise looping of variables

  1. #1
    VBAX Regular
    Joined
    Jan 2011
    Posts
    18
    Location

    How to randomise looping of variables

    Hi,

    I have the code below that takes a column of data from a range and pastes it elsewhere . Currently it does this in order of column, ie C, D, E, F etc

    What I would like to do is randomise the order of column extraction. So it may extract column D first and then column K and then column C instead of C, D, E, F etc.

    Is this posibble to do?

    Thanks

    XL


    [VBA]

    z = Sheets("X Variables").Cells(1, 200).End(xlToLeft).Column
    For i = 3 To z
    Range(Cells(1, i), Cells(1000, i)).Copy

    Sheets("Search").Select
    Range("B1").PasteSpecial Paste:=xlPasteValues
    [/VBA]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim vecCols As Variant
    Dim col As Long
    Dim idx As Long
    Dim tmp As Long
    Dim i As Long

    ReDim vecCols(1 To 26)
    Do

    col = Int(Rnd() * 26 + 1)
    tmp = 0
    On Error Resume Next
    tmp = Application.Match(col, vecCols, 0)
    On Error GoTo 0
    If tmp = 0 Then

    idx = idx + 1
    vecCols(idx) = col
    End If
    Loop Until idx >= 26

    For i = 1 To 26

    If vecCols(i) > 2 Then

    Cells(1, vecCols(i)).Resize(1000).Copy
    Worksheets("Search").Range("B1").PasteSpecial Paste:=xlPasteValues
    End If
    Next i[/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •