Loop inside a loop or Nested Loop in VBA

2655
28
Jump to solution
04-05-2013 05:07 AM
MichelleCouden1
Occasional Contributor III
I need help with a loop. I need it to do 2 things and I'm not sure how to write  a double loop. Would my code look like this :

If cboStations.Text = "Urban" then
   If cboYears.Text = "2010" then
    cboDistricts.AddItem "Abilene"
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
Try this. This will change the values of your districts if you select a new year or a new station.

Private Sub UserForm_Initialize()      Dim x As Integer      For x = 2010 To 2015         cboYear.AddItem x     Next         cboStations.AddItem "Annual"     cboStations.AddItem "Urban"      End Sub  Private Sub cboStations_Change()      If cboYear.Value <> "" Then         UpdateDistrict     End If      End Sub  Private Sub cboYear_Change()          If cboStations.Value <> "" Then         UpdateDistrict     End If      End Sub  Private Sub UpdateDistrict()      Dim myArray()     Dim myElement As Variant     cboDistrict.Clear          myArray = Array()          If cboStations.Text = "Annual" Then       If cboYear.Text = "2010" Or cboYear.Text = "2011" Or cboYear.Text = "2012" Then          myArray = Array("Abilene", "Amarillo", "Atlanta", "Austin", "Beaumont", "Brownwood", "Bryan", "Childress", "Corpus_Christi", "Dallas", "El_Paso", "Fort_Worth", "Houston", "Laredo", "Lubbock", "Lufkin", "Odessa", "Paris", "Pharr", "San Angelo", "San_Antonio", "Tyler", "Waco", "Wichita_Falls", "Yoakum") '      ElseIf cboYear.Text = "2011" Then '         myArray = Array("Abilene", "Amarillo", "Atlanta", "Austin", "Beaumont", "Brownwood", "Bryan", "Childress", "Corpus_Christi", "Dallas", "El_Paso", "Fort_Worth", "Houston", "Laredo", "Lubbock", "Lufkin", "Odessa", "Paris", "Pharr", "San Angelo", "San_Antonio", "Tyler", "Waco", "Wichita_Falls", "Yoakum") '      ElseIf cboYear.Text = "2012" Then '          myArray = Array("Abilene", "Amarillo", "Atlanta", "Austin", "Beaumont", "Brownwood", "Bryan", "Childress", "Corpus_Christi", "Dallas", "El_Paso", "Fort_Worth", "Houston", "Laredo", "Lubbock", "Lufkin", "Odessa", "Paris", "Pharr", "San Angelo", "San_Antonio", "Tyler", "Waco", "Wichita_Falls", "Yoakum") '      ElseIf cboYear.Text = "2012" Then '           myArray = Array("Abilene", "Amarillo", "Atlanta", "Austin", "Beaumont", "Brownwood", "Bryan", "Childress", "Corpus_Christi", "Dallas", "El_Paso", "Fort_Worth", "Houston", "Laredo", "Lubbock", "Lufkin", "Odessa", "Paris", "Pharr", "San Angelo", "San_Antonio", "Tyler", "Waco", "Wichita_Falls", "Yoakum") '      ElseIf cboYear.Text = "2013" Then '           myArray = Array() '      ElseIf cboYear.Text = "2014" Then '           myArray = Array()       End If          ElseIf cboStations.Text = "Urban" Then         If cboYear.Text = "2010" Then             myArray = Array("Abilene", "Amarillo", "Austin", "San_Antonio", "Waco", "Wichita_Falls")         ElseIf cboYear.Text = "2011" Then             myArray = Array("Beaumont", "Houston")         ElseIf cboYear.Text = "2012" Then 'or just Else             myArray = Array("Brownwood", "Bryan", "Childress", "Corpus_Christi", "El_Paso", "Lubbock", "Odessa", "Yoakum") '        ElseIf cboYear.Text = "2013" Then '            myArray = Array() '        ElseIf cboYear.Text = "2014" Then '            myArray = Array()         End If     End If          For Each myElement In myArray         cboDistrict.AddItem myElement     Next  End Sub 

View solution in original post

28 Replies
KenBuja
MVP Esteemed Contributor
Based on your question over on GIS.StackExchange and some of your previous questions, you could do something like this

Private Sub cboYear_Change()
    dim myArray() as String
    cboDistrict.Clear

    If cboStations.Text = "Urban" Then
        If cboYear.Text = "2010" Then
           myArray = Array ("Abilene", "Amarillo", "Austin", "San_Antonio", "Waco", "Wichita_Falls" )
        Elseif cboYear.Text = "2011" Then
           myArray = Array ("Beaumont", "Houston")
        Elseif cboYear.Text = "2012" Then 'or just Else
            myArray = Array ("Brownwood", "Bryan", "Childress", "Corpus_Christi", "El_Paso", Lubbock, "Odessa", "Yoakum")
        End If
    Elseif cboStations.Text = "Rural"
         If cboYear.Text = "2010" Then
           myArray = Array ("Abilene", "Waco", "Wichita_Falls" )
        Elseif cboYear.Text = "2011" Then
           myArray = Array ("Beaumont")
        Elseif cboYear.Text = "2012" Then 'or just Else
            myArray = Array ("Brownwood", Lubbock, "Odessa", "Yoakum")
        End If

    End If
     
    For Each myElement in myArray
        cboDistrict.AddItem myElement
    Next
    
End Sub
MichelleCouden1
Occasional Contributor III
OK, Ken before I write in you code and delete mine. I have a question about my code. Why would the district combobox not be clearing in this section?


If cboStations.Text = "Urban" Then
         cboYear.Clear
      cboYear.AddItem "2010"
      cboYear.AddItem "2011"
      cboYear.AddItem "2012"
        ElseIf cboYear.Text = "2010" Then
           cboDistrict.Clear
        cboDistrict.AddItem "Abilene"
        cboDistrict.AddItem "Amarillo"
        cboDistrict.AddItem "Austin"
        cboDistrict.AddItem "San_Antonio"
        cboDistrict.AddItem "Waco"
        cboDistrict.AddItem "Wichita_Falls"
           ElseIf cboYear.Text = "2011" Then
              cboDistrict.Clear
           cboDistrict.AddItem "Beaumont"
           cboDistrict.AddItem "Houston"
             ElseIf cboYear.Text = "2012" Then
                cboDistrict.Clear
             cboDistrict.AddItem "Brownwood"
             cboDistrict.AddItem "Bryan"
             cboDistrict.AddItem "Childress"
             cboDistrict.AddItem "Corpus_Christi"
             cboDistrict.AddItem "El_Paso"
             cboDistrict.AddItem "Lubbock"
             cboDistrict.AddItem "Odessa"
             cboDistrict.AddItem "Yoakum"
       End If
      
0 Kudos
KenBuja
MVP Esteemed Contributor
The easiest thing would be to move cboDistrict.Clear out of the loop altogether.

However, your syntax is incorrect for the decisions. The If loop will first evaluate what is selected in the cboStations control. If that is "Urban" it will add in the years then exit from the loop. It will never try to evaluate what has been selected in the cboYear control (and you don't have the line cboDistrict.Clear in that first evaluation).

You mistakenly put a / in the first CODE block. If you remove that, it will make it easier to read.
0 Kudos
MichelleCouden1
Occasional Contributor III
OK, one more question. Where do I put the code? In cboStations_Change() or cboYear_Change().
0 Kudos
KenBuja
MVP Esteemed Contributor
This code would go into the cboYear_Change sub. In the cboStations_Change sub, I would image you'd have this code

cboYear.Clear

If cboStations.Text = "Urban" Then
           cboYear.AddItem "2010"
           cboYear.AddItem "2011"
           cboYear.AddItem "2012"
Elseif cboStations.Text = "Rural" Then
           cboYear.AddItem "2011"
End If

0 Kudos
MichelleCouden1
Occasional Contributor III
OK, I am getting a type mismatch???? I will attach my whole code. See Attached. It's the way I have the Annual statement worded.
0 Kudos
KenBuja
MVP Esteemed Contributor
What line are you getting this on?

Also it looks like you want to combine the two If statements. If you are using both arrays to populate cboDistrict (you left off that code) then only use one array.

If cboStations.Text = "Annual" Then
   myArray = Array("Abilene", ...
Else If cboStations.Text = "Urban" Then  ' or just Else If if you won't have additional stations
   If cboYear.Text = "2010" Then
      myArray = Array("Abilene", ...
      etc
End If

For Each myElement in myArray
    cboDistrict.AddItem myElement
    Next
0 Kudos
MichelleCouden1
Occasional Contributor III
I'm sorry Ken, I thought I included that. I'm getting this error on the Stations.Text = "Annual" etc....
Is there maybe a chance I can get the other loop to work? Because everything fills in correctly but the urban district names. It would be easier to fix. OR is it just better to change it all out.
0 Kudos