Consulting

Results 1 to 7 of 7

Thread: Solved: Pls help clean up code

  1. #1
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location

    Solved: Pls help clean up code

    This example works but I hope there is a shorter way of writing it.
    Is there?

    Thank you
    Thank you for your help

  2. #2
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    What exactly do you want this to do?

    DO you want the random number (spins) to populate the cells in Column 1 for 48 rows?

    I am not sure what the code is intended to do, otherwise I could probably help.
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




  3. #3
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    [VBA]Sub spin()

    Application.ScreenUpdating = False
    Dim a As Variant, b As Integer, k As Single, j As Long, i As Long
    a = Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", _
    "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", _
    "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", _
    "31", "32", "33", "34", "35", "36", "37", "38", "39", _
    "40", "41", "42", "43", "44", "45", "46", "47", "48", "49")
    spins = 200
    For i = 1 To spins
    b = Rnd() * 49
    If b > 48 Then b = 0
    [D1].Value = a(b)
    k = 0
    'For j = 1 To 5000000 / (spins + 1 - i) ' less wait on the first spin

    'Next j
    Next i
    Application.ScreenUpdating = True

    End Sub[/VBA]


    This works pretty fast for me...not sure what you are looking for. The j loop slows it down..
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




  4. #4
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location
    Thank you XLGibbs.

    I do not want the code to do ANYTHING but spin randomly from 1 to 49 one hundred times and stop.
    This is just to give an optical illusion .... at the end of the run the code will pick up a value from a different range and paste it in the same cell the numbers have been spining

    Hope I explained it well
    Thanks again

    Sorry forgot the attachment (revised)
    Thank you for your help

  5. #5
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location
    Sorry I did not explained it well ... it's the range that I want to write differently if possible
    kind of like (Range(A1:A10) no need here to write Range(A1,A2,A3,A4 etc... ) is there a similar way in an array ? (if that is what it's call in this example)


    [VBA] "11", "12", "13", "14", "15", "16",[/VBA]
    Thank you for your help

  6. #6
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by ndendrinos
    ...I do not want the code to do ANYTHING but spin randomly from 1 to 49 one hundred times and stop...
    You mean to generate a random number between 1 and 49 - repeat this 100 times then stop?[VBA]
    Sub spin()

    Dim N As Long

    For N = 1 To 100
    Randomize
    [D1] = Int(Rnd() * 49 + 1)
    Next

    End Sub
    [/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  7. #7
    VBAX Mentor
    Joined
    Sep 2004
    Posts
    431
    Location
    Thank you both very much ... John your code is exactly what I was looking for .Much shorter... took it up to 1000 and I now get the visual I need.
    Thank you for your help

Posting Permissions

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