Excel loop, run when cells value changes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Macolm64
    New Member
    • Sep 2006
    • 4

    Excel loop, run when cells value changes

    I am looking for some code that would loop until two cell values in excel equal the same. By cell value I mean the result of the formulas in the cells are the same.
    This procedure needs to be run when a cells value changes, again I mean result of the formula in that cell changes.

    For the loop part I have written the following code which works as if the loop didn't exist.

    Do
    Range("A1").Sel ect
    Selection.End(x lDown).Select
    Range(Selection , Selection.End(x lToRight)).Sele ct
    Selection.Copy
    Range("A1").End (xlDown).Offset (1, 0).Select
    Selection.Paste Special Paste:=xlPasteA ll, Operation:=xlNo ne, SkipBlanks:= _
    False, Transpose:=Fals e
    Application.Cut CopyMode = False
    Range("A1").Sel ect

    Exit Do

    Loop Until Range("L1").Val ue = Range("M1").Val ue


    Thankful for any help
  • CaptainD
    New Member
    • Mar 2006
    • 135

    #2
    Originally posted by Macolm64
    I am looking for some code that would loop until two cell values in excel equal the same. By cell value I mean the result of the formulas in the cells are the same.
    This procedure needs to be run when a cells value changes, again I mean result of the formula in that cell changes.

    For the loop part I have written the following code which works as if the loop didn't exist.

    Do
    Range("A1").Sel ect
    Selection.End(x lDown).Select
    Range(Selection , Selection.End(x lToRight)).Sele ct
    Selection.Copy
    Range("A1").End (xlDown).Offset (1, 0).Select
    Selection.Paste Special Paste:=xlPasteA ll, Operation:=xlNo ne, SkipBlanks:= _
    False, Transpose:=Fals e
    Application.Cut CopyMode = False
    Range("A1").Sel ect

    Exit Do

    Loop Until Range("L1").Val ue = Range("M1").Val ue


    Thankful for any help
    The worksheet_chang e event fires each time a cell value changes. Place code there to look at the values of your two cells

    Code:
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    
    If Sheet1.Cells(1, 12) <> Sheet1.Cells(1, 13) Then
        'Run your code
    End If
    End Sub

    Comment

    • Macolm64
      New Member
      • Sep 2006
      • 4

      #3
      Originally posted by CaptainD
      The worksheet_chang e event fires each time a cell value changes. Place code there to look at the values of your two cells

      Code:
      Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
      
      If Sheet1.Cells(1, 12) <> Sheet1.Cells(1, 13) Then
          'Run your code
      End If
      End Sub
      Thanks for the quick response.

      Where am I meant to put this new code? I tried it in "This Workbook" object and the specific sheet object for where I want the code to look for a change.
      Note: Im working with sheet 2 so I changed the two references in your code to sheet2.
      What do the cell numbers refer to in your code eg Cells(1, 12)

      Comment

      • CaptainD
        New Member
        • Mar 2006
        • 135

        #4
        Originally posted by Macolm64
        Thanks for the quick response.

        Where am I meant to put this new code? I tried it in "This Workbook" object and the specific sheet object for where I want the code to look for a change.
        Note: Im working with sheet 2 so I changed the two references in your code to sheet2.
        What do the cell numbers refer to in your code eg Cells(1, 12)
        Where it says "ThisWorkbo ok" (Double click to go to that area). That area catches all events in the intire workbook.

        Comment

        • Macolm64
          New Member
          • Sep 2006
          • 4

          #5
          Originally posted by CaptainD
          Where it says "ThisWorkbo ok" (Double click to go to that area). That area catches all events in the intire workbook.
          Ok thanks.

          Now for the loop problem. The code I have does not loop. I want the procedure to loop until cell L1 = M1

          Comment

          • CaptainD
            New Member
            • Mar 2006
            • 135

            #6
            Originally posted by Macolm64
            Ok thanks.

            Now for the loop problem. The code I have does not loop. I want the procedure to loop until cell L1 = M1
            Ok, I looked over you loop code and things are missing. What changes the cell values that you want to have equal each other?

            Comment

            • CaptainD
              New Member
              • Mar 2006
              • 135

              #7
              Originally posted by Macolm64
              Ok thanks.

              Now for the loop problem. The code I have does not loop. I want the procedure to loop until cell L1 = M1
              Here is a simple loop procedure to show you what is happening. Step through it and watch the values change

              Code:
              Private Sub Worksheet_Change(ByVal Target As Range)
              'Delete or change the value in A1
              'Then Delete the value in B3
              Do While Sheet1.Cells(3, 2) <= 100
                  If Sheet1.Cells(1, 1) = Blank Then
                      Sheet1.Cells(1, 1) = 2
                  End If
               
                  Sheet1.Cells(3, 2) = Sheet1.Cells(1, 1) * 2
                  Sheet1.Cells(1, 1) = Sheet1.Cells(1, 1) + 2
              
              Loop
              End Sub

              Comment

              • Macolm64
                New Member
                • Sep 2006
                • 4

                #8
                Originally posted by CaptainD
                Here is a simple loop procedure to show you what is happening. Step through it and watch the values change

                Code:
                Private Sub Worksheet_Change(ByVal Target As Range)
                'Delete or change the value in A1
                'Then Delete the value in B3
                Do While Sheet1.Cells(3, 2) <= 100
                    If Sheet1.Cells(1, 1) = Blank Then
                        Sheet1.Cells(1, 1) = 2
                    End If
                 
                    Sheet1.Cells(3, 2) = Sheet1.Cells(1, 1) * 2
                    Sheet1.Cells(1, 1) = Sheet1.Cells(1, 1) + 2
                
                Loop
                End Sub

                I have modified the code to suit my needs and it works just how I want it to.

                Thanks for the help.

                Comment

                Working...