+ Reply to Thread
Results 1 to 2 of 2

Thread: Loop to check condition of cells

  1. #1
    Registered User
    Join Date
    12-28-2009
    Location
    Atlanta, Ga
    MS-Off Ver
    Excel 2007
    Posts
    16

    Smile Loop to check condition of cells

    Hi,
    I'm looking for assistance with some code that I've been working on but haven't been able to get it to work. Column R is a calculated cell that adds percentages across the worksheet. I would like to prevent the responders from having a percentage over 100% in column R. For example, the value in R6 is currently 100% but if the responder changed V6 to 10% then the "Grand Total" would be 105%, which I'd like to have an error message displayed when a cell R6 and below are over 100%.

    There may be some additional rows added and I'd like to make sure that the code contains a variable to check every row in column R starting at row 6.

    The code I've tried is below:

    
    Private Sub Worksheet_Change(ByVal Target As Range)
    ' This loop repeats for a fixed number of times determined by the number of rows in the range
        Dim i As Integer
        For i = 1 To Selection.CurrentRegion.Rows.Count - 1
        ActiveCell.FormulaR1C1 = "=Average(RC[-1],RC[-2])"
        ActiveCell.Offset(1, 0).Select
    
        If ActiveCell.Value > 1 Then
        MsgBox "The value you entered exceeds the 100% total for this process.  Please adjust."
        Range(Target.Address).Value = Null
        End If
    
        Next i
    End Sub
    Please let me know if there's any additional details that you'd need to know. Thank you in advance for taking a look at this.

  2. #2
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,300

    Re: Loop to check condition of cells

    Hi keith,
    Why not put a data validation in column R so values greater than 1 are not allowed? Look at Data Validation of Decimal and between 0 and 1.

    I may be off base on this answer but it was first to come to mind. I'd rather not have to write VBA if Excel can handle it in another way.
    One test is worth a thousand opinions.
    Click the * below to say thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0