Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looping through range 1

Status
Not open for further replies.

MDA

Technical User
Joined
Jan 16, 2001
Messages
243
Location
US
I have two combo boxes that contain start and end "Periods." For example Start 0201 - End 0212 (YYMM).

I am having a problem in looping through the periods when I use 0001 - 0012. I am currently using the following syntax:

Dim i As Integer
For i = Val(cmbFromPE.Text) To Val(cmbtoPE.Text)
msgbox "0" & i
Next

So if I enter from: 0201 and to: 0203
It will return 0201,0202,0203 (THIS IS CORRECT)

BUT if I enter 0001 - 0003

I get back 01,02,03 (NOT CORRECT). This is because I am using type Interger for i. However, I don't know what other datatype I can use to loop through mutiple periods.

Any ideas?
Thanks in advance.
MDA
 

you should not be looping the way you are, by using the whole string as a number and incrementing by 1. what happens if your range is 0001 to 0101? it isn't going to magically go from 0012 to 0101 - is it?

you need to break the string into the year portion and the month portion and work on those individually.

but, besides that, you need to preserve the leading zeroes on the year/month pieces so they are always 2 digits.

the easiest way of handling this, for example is:

Format(year, "00")
Format(month, "00")

"00" will force the number to two digits with leading zeroes.
 
instead of this:
Dim i As Integer
For i = Val(cmbFromPE.Text) To Val(cmbtoPE.Text)
msgbox "0" & i
Next

try something like this:

dim iMonthYearIdx as integer
dim iMonthYearMin as integer
dim iMonthYearMax as integer

dim iYearMax as integer
dim iYearMin as integer
dim iMonthMax as integer
dim iMonthMin as integer

dim iMonth as integer
dim iYear as integer

iYearMin = val(left$(cmbFromPE.Text,2))
iYearMax = val(left$(cmbToPE.Text,2))
iMonthMin = val(right$(cmbFromPE,2))
iMonthMax = val((right$(cmbToPE.Text,2))

iMonthYearMin = 12 * iYearMin + iMonthMin
iMonthYearMax = 12 * iYearMax + iMonthMax

for iMonthYearIdx = iMonthYearMin to iMonthYearMax

iMonth = iMonthYearIdx mod 12

iYear = (iMonthYearIdx - iMonth) / 12

if (iMonth = 0) then
iMonth = 12
end if

msgbox format$(iYear,"00") & format$(iMonth,"00")

next iMonthYearIdx

 
Thanks rsinj,

That code worked like a charm. I just had to move the IF statement up a line.

Regards,
>M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top