+ Reply to Thread
Results 1 to 4 of 4

Loop until activcell contains...

Hybrid View

  1. #1
    Registered User
    Join Date
    08-28-2012
    Location
    Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    11

    Loop until activcell contains...

    I have searched far and wide but maybe I'm just trying to use the wrong type of loop. I want my loop to apply conditional formatting to every line of an invoice spreadsheet until it gets to a line for the customer signature, but the text in this line is not the same in every spreadsheet, so I just want it to stop when it gets to a line containing the text "customer". Can anyone help me out?

    Do Until ActiveCell.FormulaR1C1 = "CUSTOMER SIGNATURE _______________________________"
        
            With Selection.Font
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = -0.349986266670736
        End With
    
        Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=RC3>0"
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With Selection.FormatConditions(1).Font
            .ThemeColor = xlThemeColorLight1
            .TintAndShade = 0
        End With
        Selection.FormatConditions(1).StopIfTrue = False
        ActiveCell.Offset(1, 0).Select
        Loop
    Thanks in advance.

  2. #2
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Loop until activcell contains...

    You can try this out:

        Dim rngFound        As Excel.Range
        Dim rngStart        As Excel.Range
        Dim fmtCurr         As FormatCondition
    
        Dim intCol          As Integer
        Dim lngFoundRow     As Long
    
        Set rngStart = Range("A2")
        intCol = rngStart.Column
        On Error Resume Next
        lngFoundRow = ActiveSheet.Columns(intCol).Find(What:="customer", After:=Cells(1, intCol), _
            SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            LookAt:=xlPart, LookIn:=xlValues).Row
        If Err <> 0 Then
            lngFoundRow = 0
        End If
        On Error GoTo 0
    
        If lngFoundRow > rngStart.Row Then
            Set rngFound = Cells(rngStart.Row, intCol).Resize(lngFoundRow - rngStart.Row + 1, 1)
            With rngFound.Font
                .ThemeColor = 2
                .TintAndShade = -0.349986266670736
            End With
    
            For Each fmtCurr In rngFound.FormatConditions
                fmtCurr.Delete
            Next fmtCurr
            With rngFound.FormatConditions.Add(Type:=xlExpression, Formula1:="=RC3>0")
                .SetFirstPriority
                With .Font
                    .ThemeColor = xlThemeColorLight1
                    .TintAndShade = 0
                End With
                .StopIfTrue = False
            End With
        End If
    
        Set rngStart = Nothing
        Set rngFound = Nothing
    It is a bit longer code, but does less processing. Change the Set rngStart line to point to the starting cell of the data on your worksheet. It first finds "Customer" in the given column, then assigns a range object to the cells from rngStart to the cell right above "Customer". It deletes any existing conditional formatting, then adds new conditional formatting as your code does.

  3. #3
    Registered User
    Join Date
    08-28-2012
    Location
    Minnesota
    MS-Off Ver
    Excel 2010
    Posts
    11

    Re: Loop until activcell contains...

    That looks awesome! It doesn't seem to be working though. My data starts is D19 and then the text to make it stop is also in that column; would that need to be in there?
    Thanks,
    Alex

  4. #4
    Forum Contributor wallyeye's Avatar
    Join Date
    05-06-2011
    Location
    Arizona
    MS-Off Ver
    Office 2010, 2007
    Posts
    308

    Re: Loop until activcell contains...

    Change the Set rngStart line to point to D19 instead of A2...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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