+ Reply to Thread
Results 1 to 4 of 4

Loop through rows (Dynamic number)

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-06-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    180

    Loop through rows (Dynamic number)

    Hi All,

    I have created the code below (Spreadsheet Attached) which works for the first row but I want the code to loop through each row. I know there is a way aroud this by using count and then the rows will increment by 1 every time it loops through the code. Unfortunately I do not know how to do this.

    P.s. i do not want to specify how many rows and the number of rows is dynamic.

    Any ideas? Any help would be greatly appreciated.

    Option Explicit
    Sub RuleTesting()
    
    Dim QRule As String
    Dim RTRule As String
    Dim CurrentResult As Integer
    Dim Red As String
    Dim Amber As String
    Dim Green As String
    Dim Status As Range
    Dim RedNo As String
    Dim GreenNo As String
    Dim Symbol As String
    
    Set Status = ThisWorkbook.Sheets("Results_Q").Range("F2")
    
    ' Variables
    QRule = ThisWorkbook.Sheets("Results_Q").Range("E2").Value
    RTRule = ThisWorkbook.Sheets("RuleTable_RT").Range("A2").Value
    CurrentResult = ThisWorkbook.Sheets("Results_Q").Range("D2").Value
    RedNo = ThisWorkbook.Sheets("RuleTable_RT").Range("C2").Value
    GreenNo = ThisWorkbook.Sheets("RuleTable_RT").Range("G2").Value
    Symbol = ThisWorkbook.Sheets("RuleTable_RT").Range("B2").Value
    
    
    ' Selects first 'Status' cell & Loop Through rows
    ThisWorkbook.Sheets("Results_Q").Range("F2").Select
    Do Until ActiveCell.Offset(1, -1) = ""
    ActiveCell.Offset(1, 0).Select
    ' Calculate the RAG
    If Symbol = ">" Then
    
    If CurrentResult > RedNo Then
    Status = "Red"
    ElseIf CurrentResult < GreenNo Then Status = "Green"
    ElseIf Symbol = ">" Then Status = "Amber"
    
    ElseIf Symbol = "<" Then
    
    If CurrentResult < RedNo Then
    Status = "Red"
    ElseIf CurrentResult > GreenNo Then Status = "Green"
    Else: Status = "Amber"
    
    
    
    End If
    End If
    End If
    Loop
    End Sub
    Kind regards,

    James Elwell
    Attached Files Attached Files
    Last edited by forrestgump1980; 12-18-2014 at 12:02 PM.

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through rows (Dynamic number)

    Hi James

    Try this
    Option Explicit
    Sub RuleTesting()
    
      Dim QRule As String
      Dim RTRule As String
      Dim CurrentResult As Double
      Dim Status As Range
      Dim RedNo As String
      Dim GreenNo As String
      Dim Symbol As String
      Dim ws1 As Worksheet, ws2 As Worksheet
      Dim LR As Long, cel As Range
    
      Set ws1 = Sheets("Results_Q")
      Set ws2 = Sheets("RuleTable_RT")
    
      With ws1
        LR = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious).Row
    
        For Each cel In Range("E2:E" & LR)
          Set Status = .Range("F" & cel.Row)
          ' Variables
          QRule = .Range("E" & cel.Row).Value
          RTRule = ws2.Range("A" & cel.Row).Value
          CurrentResult = .Range("D" & cel.Row).Value
          RedNo = ws2.Range("C" & cel.Row).Value
          GreenNo = ws2.Range("G" & cel.Row).Value
          Symbol = ws2.Range("B" & cel.Row).Value
          If Symbol = ">" Then
    
            If CurrentResult > RedNo Then
              Status = "Red"
            ElseIf CurrentResult < GreenNo Then Status = "Green"
            ElseIf Symbol = ">" Then Status = "Amber"
            Else
            End If
            
            ElseIf Symbol = "<" Then
              If CurrentResult < RedNo Then
                Status = "Red"
              ElseIf CurrentResult > GreenNo Then Status = "Green"
              Else: Status = "Amber"
              End If
            End If
        Next cel
      End With
    End Sub
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Forum Contributor
    Join Date
    06-06-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    180

    Re: Loop through rows (Dynamic number)

    Hi John,

    That works fantastically. I just need to work out how you did it now.

    Regards,

    forrestgump1980

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Loop through rows (Dynamic number)

    You're welcome...glad I could help. Thanks for the Rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to loop through dynamic ranges using columns rather than rows
    By xcelnovice101 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-24-2014, 06:28 PM
  2. [SOLVED] Macro to loop through dynamic number of rows checking adjacent for certain criteria
    By ellismith in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-13-2014, 04:12 PM
  3. [SOLVED] Rename Sheets by using Dynamic Loop for the rows
    By desolatori in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-30-2014, 09:36 AM
  4. Do-While loop within a do-while loop? Dealing with changing number of rows
    By Motox in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-02-2012, 12:59 AM
  5. Dynamic Columns and Rows Selecting all Loop
    By excel0831 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-22-2012, 05:41 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1