Results 1 to 3 of 3

Thread: Dual Loop in the same procedure

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2020
    Posts
    3

    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.

    Thanks in advance
    Last edited by Shaggy Hiker; Feb 23rd, 2020 at 08:48 PM. Reason: Added CODE tags.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,373

    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
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2020
    Posts
    3

    Re: Dual Loop in the same procedure

    Quote Originally Posted by westconn1 View Post
    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
  •  



Click Here to Expand Forum to Full Width