# Thread: Dual Loop in the same procedure

1. ## Dual Loop in the same procedure

Hi there.

I'm a bit rusty at VB coding (its been a few years since I last did some).
I'm trying to create 2 loops using Counters within the same procedure but don't seem to be having too much luck.

This is my code.
Code:
Sub SET_Q1 ()
Dim CounterA As Integer
Dim CounterB As Integer

For CounterA = 12 To 16
For CounterB = 1 To 5

If Worksheets("Data").Range("U" & CounterA).Text = "G" Then
Worksheets("Investments").Shapes.Range(Array("AutoShape Q1NAP" & CounterB)).Visible = False
End if

If Worksheets("Data").Range("U" & CounterA).Text = "A" Then
Worksheets("Investments").Shapes.Range(Array("AutoShape Q1NAP" & CounterB)).Visible = False
End If

If Worksheets("Data").Range("U" & CounterA).Text = "R" Then
Worksheets("Investments").Shapes.Range(Array("AutoShape Q1NAP" & CounterB)).Visible = False
End If

If Worksheets("Data").Range("U" & CounterA).Text = "" Then
Worksheets("Investments").Shapes.Range(Array("AutoShape Q1NAP" & CounterB)).Visible = True
End If

Next
Next

End Sub

The code is supposed to cycle through a range of cells (on the data worksheet) and then make the visible property on certain shapes (in the investments worksheet) either true or false (depending on the value in each cell). I can get the code to loop through 1 of the variable but not both.

Any help would be really apperciated.

2. ## Re: Dual Loop in the same procedure

without being really sure what you want to do it looks like you should have a counterb loop for criteria of the countera

i believe i would use a select case rather than all the ifs as only one case can be true
Code:
For CounterA = 12 To 16
Select Case Worksheets("Data").Range("U" & CounterA).Text
Case "G", "A", "R": res = False
Case "U": res = True
End Select
For CounterB = 1 To 5
Worksheets("Investments").Shapes.Range(Array("AutoShape Q1NAP" & CounterB)).Visible = res
Next
Next
of course i may have completely misunderstood what you want to achieve

3. ## Re: Dual Loop in the same procedure

Originally Posted by westconn1
without being really sure what you want to do it looks like you should have a counterb loop for criteria of the countera

i believe i would use a select case rather than all the ifs as only one case can be true
Code:
For CounterA = 12 To 16
Select Case Worksheets("Data").Range("U" & CounterA).Text
Case "G", "A", "R": res = False
Case "U": res = True
End Select
For CounterB = 1 To 5
Worksheets("Investments").Shapes.Range(Array("AutoShape Q1NAP" & CounterB)).Visible = res
Next
Next
of course i may have completely misunderstood what you want to achieve

Thank you for taking the time to look at my query. This is greatly appreciated. I will take a look at your suggested solution later today.
For now at least, I managed to come up with a quick and dirty way around (by deducting 11 from the Value of CounterA i.e.

"AutoShape Q1NAP" & CounterA - 11"

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•