Do Loop InputBox into Array

m5u4r3p2h1y

New Member
Joined
May 8, 2013
Messages
14
Hi,

I,m trying to allow a user to input a year (between 2004-2012), each of which will be stored into an array. Since I don't know how many years the user will choose I'm required to use a dynamic array. For some reason when I run the code below it takes a valid input but then exits the loop instead of asking for another. I have the message boxes at the bottom to verify the array is getting the values, but they all return the value 0. If anyone could help I'd appreciate it.

Thanks.

Sub DataRange()


Dim Message, Title, DefaultValue As String
Dim RangeBoxArray() As Long
Dim i As Integer


Message = "Enter the years you would like to study (2004-2012 one year at a time). Click OK with value=0 to finish."
Title = "Historical Load Data Range"
DefaultValue = 0
i = 1
ReDim RangeBoxArray(i)


Do
RangeBoxArray(i) = InputBox(Message, Title, DefaultValue, 100, 100)
i = i + 1
ReDim RangeBoxArray(i)
Loop Until RangeBoxArray(i - 1) > 2013 Or RangeBoxArray(i - 1) < 2003


MsgBox RangeBoxArray(0)
MsgBox RangeBoxArray(1)
MsgBox RangeBoxArray(2)




End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
This will clear out the array as well as redimensioning it.
Code:
ReDim RangeBoxArray(i)

Try this.
Code:
ReDim Preserve RangeBoxArray(i)
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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