array / loop for 'x' amount of times

gordsky

Well-known Member
Joined
Jun 2, 2016
Messages
555
Office Version
  1. 2016
Platform
  1. Windows
The below code works fine for 3 loops/runs but would like it to run for any given number. Ive done lots of searching on google but cant figure out how to implement a variable number in MyDay(3). the required number will be generated by the user via an input box. Could someone assist please

VBA Code:
Dim MyDay(3) As Variant
   For i = 1 To UBound(MyDay)
       'run my code here
        MyDay(i) = 'outcome of code run
  
   Next i
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
How about
VBA Code:
Dim MyDay As Variant, x As Variant
x = InputBox("Enter a number")
If x = "" Then Exit Sub
ReDim MyDay(x)
 
Upvote 0
Solution
How about
VBA Code:
Dim MyDay As Variant, x As Variant
x = InputBox("Enter a number")
If x = "" Then Exit Sub
ReDim MyDay(x)
Fluff thanks so much, Ive been banging my head with something similar for ages. Kept getting either constant expression required or runtime9 Seems I was missing the ReDim.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
You're welcome & thanks for the feedback.
Fluff, really appreciated your help earlier, wonder if you would be able to assist with a step on. Assuming that the above code ran 5 times. Rather than looping through the result and writing each one to mastersheet individually (they would be in consecutive rows) is it possible to group them and paste in one go? (i know a 5 x loop isnt that bad but if the number were significantly more then obviously it would be slow going back and forth to sheet?)
 
Upvote 0
How about something like
VBA Code:
Dim MyDay As Variant, x As Variant
x = InputBox("Enter a number")
If x = "" Then Exit Sub
ReDim MyDay(1 To x, 1 To 1)
For i = 1 To UBound(MyDay)
   MyDay(i, 1) = i
Next i
Range("B2").Resize(x).Value = MyDay
 
Upvote 0
amazing, thankyou, hopefully Ill get there one day........
 
Upvote 0
Just noting that you can do this without using a loop or an array...
VBA Code:
  Dim x As Variant
  x = InputBox("Enter a number")
  If x = "" Then Exit Sub
  Range("B2").Resize(x) = Evaluate("ROW(1:" & x & ")")
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,185
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top