VBA to capture values between two numbers

Chazzo

New Member
Joined
Dec 28, 2014
Messages
24
Office Version
  1. 365
Hi Everyone,

If I have a named range with values 1 thru 12 named "Months", and two separate Cells that ask for user input "Beg Month" and "End Month".....

How can I use VBA to capture those values and the values between these two numbers?

For example, if a user input the number 5 for the "Beg Month" and the number 7 for the "End Month", how can I use a variable to capture the numbers 5,6, and 7?

The "Months" named range is on sheet 2 B5:B16.

The "Beg Month" entry cell is on Sheet1 in cell C11, and the "End Month" is on Sheet1 in Cell C12.

Any suggestions on VBA methods would be much appreciated.

Thank you very much in advance.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Maybe something like this:
Code:
Sub Chazzo()
'assumes the named ranges "Beg_Month" and "End_Month" are scoped at workbook level
Dim ValuesBetween()
ReDim ValuesBetween(1 To Range("End_Month") - Range("Beg_Month") + 1)
For i = 1 To Range("End_Month") - Range("Beg_Month") + 1
    ValuesBetween(i) = Range("Beg_Month") - 1 + i
Next i
MsgBox Join(ValuesBetween, ", ")

End Sub
 
Upvote 0
Awesome, thanks JoeMo!

I will give this a shot and report back the outcome....
 
Upvote 0
Looks like this will work for me, thanks again JoeMo.

This newbie will now "do something" with each value which I think I can manage....

I appreciate your level of skill.

Cheers.
 
Last edited:
Upvote 0
Looks like this will work for me, thanks again JoeMo.

This newbie will now "do something" with each value which I think I can manage....

I appreciate your level of skill.

Cheers.
You are welcome - thanks for the reply.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

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