Hi All,
**Newbie VBA/Macro user*** - I use forums and Youtube to learn how to code but in there somewhere are probably some bad habits.
I have a form button on a project i'm working on which has now gone a bit slow to calculate.
The button picks a random number between 1 and 10 in the selected cells. I have 10 other buttons on the same page doing roughly the same calculations plus a master reset button that clears all cells. All of which do their job but at a slower pace with the more buttons/code I added.
My question is - is there a cleaner/better way to write this code to speed it up, as it has got bogged down and is running slow.(which is to be expected I guess)
my code for one of the buttons is:
Sub Button4_Click()
Range("C3") = WorksheetFunction.RandBetween(1, 5)
Range("C5") = WorksheetFunction.RandBetween(1, 10)
Range("C6") = WorksheetFunction.RandBetween(1, 10)
Range("C8") = WorksheetFunction.RandBetween(1, 10)
Range("C9") = WorksheetFunction.RandBetween(1, 10)
Range("C11") = WorksheetFunction.RandBetween(1, 10)
Range("C12") = WorksheetFunction.RandBetween(1, 10)
Range("C14") = WorksheetFunction.RandBetween(1, 10)
Range("C15") = WorksheetFunction.RandBetween(1, 10)
Range("C17") = WorksheetFunction.RandBetween(1, 10)
Range("C18") = WorksheetFunction.RandBetween(1, 10)
Range("C20") = WorksheetFunction.RandBetween(1, 10)
Range("C21") = WorksheetFunction.RandBetween(1, 10)
Range("C23") = WorksheetFunction.RandBetween(1, 10)
Range("C24") = WorksheetFunction.RandBetween(1, 10)
Range("C26") = WorksheetFunction.RandBetween(1, 10)
Range("C27") = WorksheetFunction.RandBetween(1, 10)
Range("C29") = WorksheetFunction.RandBetween(1, 10)
Range("C30") = WorksheetFunction.RandBetween(1, 10)
Range("C32") = WorksheetFunction.RandBetween(1, 10)
Range("C33") = WorksheetFunction.RandBetween(1, 10)
Range("C35") = WorksheetFunction.RandBetween(1, 10)
Range("C36") = WorksheetFunction.RandBetween(1, 10)
Range("C38") = WorksheetFunction.RandBetween(1, 10)
Range("C39") = WorksheetFunction.RandBetween(1, 10)
End Sub
Thanks All
**Newbie VBA/Macro user*** - I use forums and Youtube to learn how to code but in there somewhere are probably some bad habits.
I have a form button on a project i'm working on which has now gone a bit slow to calculate.
The button picks a random number between 1 and 10 in the selected cells. I have 10 other buttons on the same page doing roughly the same calculations plus a master reset button that clears all cells. All of which do their job but at a slower pace with the more buttons/code I added.
My question is - is there a cleaner/better way to write this code to speed it up, as it has got bogged down and is running slow.(which is to be expected I guess)
my code for one of the buttons is:
Sub Button4_Click()
Range("C3") = WorksheetFunction.RandBetween(1, 5)
Range("C5") = WorksheetFunction.RandBetween(1, 10)
Range("C6") = WorksheetFunction.RandBetween(1, 10)
Range("C8") = WorksheetFunction.RandBetween(1, 10)
Range("C9") = WorksheetFunction.RandBetween(1, 10)
Range("C11") = WorksheetFunction.RandBetween(1, 10)
Range("C12") = WorksheetFunction.RandBetween(1, 10)
Range("C14") = WorksheetFunction.RandBetween(1, 10)
Range("C15") = WorksheetFunction.RandBetween(1, 10)
Range("C17") = WorksheetFunction.RandBetween(1, 10)
Range("C18") = WorksheetFunction.RandBetween(1, 10)
Range("C20") = WorksheetFunction.RandBetween(1, 10)
Range("C21") = WorksheetFunction.RandBetween(1, 10)
Range("C23") = WorksheetFunction.RandBetween(1, 10)
Range("C24") = WorksheetFunction.RandBetween(1, 10)
Range("C26") = WorksheetFunction.RandBetween(1, 10)
Range("C27") = WorksheetFunction.RandBetween(1, 10)
Range("C29") = WorksheetFunction.RandBetween(1, 10)
Range("C30") = WorksheetFunction.RandBetween(1, 10)
Range("C32") = WorksheetFunction.RandBetween(1, 10)
Range("C33") = WorksheetFunction.RandBetween(1, 10)
Range("C35") = WorksheetFunction.RandBetween(1, 10)
Range("C36") = WorksheetFunction.RandBetween(1, 10)
Range("C38") = WorksheetFunction.RandBetween(1, 10)
Range("C39") = WorksheetFunction.RandBetween(1, 10)
End Sub
Thanks All