Array of MonthNames

  • Thread starter Thread starter JMay
  • Start date Start date
J

JMay

My current studybook just showed me how to create this array; and I'm able
to test it in the Immediate Window and it works; that is when I enter "?
Monthname(4)" (I get )April (Great!!)
so what else can I do with it; I'm clueless..

Option Explicit
Option Base 1

Sub MonthArray()
Dim MonthName(1 To 12) As String
MonthNames(1) = "Janaury"
MonthNames(2) = "February"
MonthNames(3) = "March"
MonthNames(4) = "April"
MonthNames(5) = "May"
MonthNames(6) = "June"
MonthNames(7) = "July"
MonthNames(8) = "August"
MonthNames(9) = "September"
MonthNames(10) = "October"
MonthNames(11) = "November"
MonthNames(12) = "December"
End Sub
 
JMay,

As the code is written, it is useless. You could modify it to be
a Function procedure that gives the month name given a month
number. For example,


Function MonthArray(MonthNumber As Integer) As String
Dim MonthName(1 To 12) As String
MonthNames(1) = "Janaury"
MonthNames(2) = "February"
MonthNames(3) = "March"
MonthNames(4) = "April"
MonthNames(5) = "May"
MonthNames(6) = "June"
MonthNames(7) = "July"
MonthNames(8) = "August"
MonthNames(9) = "September"
MonthNames(10) = "October"
MonthNames(11) = "November"
MonthNames(12) = "December"
MonthArray = MonthNames(MonthNumber)
End Function

You would then call this function with code like

MsgBox MonthArray(12)

Of course, there are many other and better ways to get the same
thing, but this shows how you would use such an array.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
All you can do is retrieve the month name based upon a month number. Unless
you have some code that somewhere has a month number that you need to
resolve to the appropriate month name, it is of no use at all.

Surely, the point is not what you can do with that particular piece of code,
but more what it is telling you about loading arrays, and retrieving from
them. There is an Option Base 1 statement, which means that your array is 1
based, rather than 0 based (as I tend to use), and is more useful for months
as January is month 1, a zero based array would either have January as item
0, or not load item 0.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Thanks both Chip and Bob;

Bob Phillips said:
All you can do is retrieve the month name based upon a month number. Unless
you have some code that somewhere has a month number that you need to
resolve to the appropriate month name, it is of no use at all.

Surely, the point is not what you can do with that particular piece of code,
but more what it is telling you about loading arrays, and retrieving from
them. There is an Option Base 1 statement, which means that your array is 1
based, rather than 0 based (as I tend to use), and is more useful for months
as January is month 1, a zero based array would either have January as item
0, or not load item 0.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
Just to add

Dim MonthName(1 To 12) As String


overrides any option base setting.

What do you think your study book was trying to teach you with the example?

As an analogy
In elementary school, we had races to see who could look up a word in the
dictionary the fastest. The point wasn't to develop speed at looking up the
words, but to repetitively practice the skills needed for looking up words
while removing the boredom of doing it over and over.
 
You want to see what you can do with arrays.

Try searching for "= array(" in the *excel* newsgroups in Google.

I got 68,000 hits.

There might be something useful in there <vbg>.
 
Back
Top