+ Reply to Thread
Results 1 to 3 of 3

FindFormat ColorIndex Then Make A List

Hybrid View

  1. #1
    Registered User
    Join Date
    11-13-2015
    Location
    Titusville, PA
    MS-Off Ver
    2010
    Posts
    53

    FindFormat ColorIndex Then Make A List

    Thanks to all who have helped this little project of mine get this far!


    Next step -

    Generate a list of all the cells with a given interior color

    Two Lists are needed

    The first list will search for anything red on a single Sheet "INVMaster" and paste the cell contents in a list form

    The second will search for anything red on 17 other sheets (though in this example I only included 2) and make a single list of the red cells found.


    Right now i have been researching on it and I read that .Find won't work, that I need .FindFormat

    You guys have any ideas?
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: FindFormat ColorIndex Then Make A List

    Hi cwhite86,

    I tend to write code that is verbose. The following code works for me. There are working copies of the code in the attached file which I refer to quite often.

    Please note that .find is very particular about the inputs:
    a. ws.Cells.Find and ws.Range MUST refer to the same sheet.
    b. In the 'FindNext' example, ws.Columns("C") and After:=ws.Range("C1") must Match (e.g. Column 'C').


    Function LjmFindRedCells(ws As Worksheet, sFindString As String) As Long
      'This iterates through a worksheet finding all the occurences of a 'format'
      'NOTE: A format and a value can be found at the same time
      
      Dim r As Range
      Dim iCount As Long
      Dim bNeedMore As Boolean
      Dim sAddress As String
      Dim sFirstAddress As String
      
      'Set the 'Format' being searched for
      Application.FindFormat.Clear
      Application.FindFormat.Interior.Color = RGB(255, 0, 0)
    
      'Find the first occurence of the 'Format'
      Set r = Nothing
      Set r = ws.Cells.Find(What:=sFindString, _
                          After:=ws.Range("A1"), _
                          LookIn:=xlValues, _
                          LookAt:=xlWhole, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlNext, _
                          MatchCase:=False, _
                          SearchFormat:=True)
                          
                          
      If Not r Is Nothing Then
      
        'Save the found address as the 'First Address'
        'Save the row number in the array to be returned
        iCount = iCount + 1
        sFirstAddress = r.Address(False, False)
        'MsgBox "First occurrence of RED FORMAT is in cell '" & sFirstAddress & "'."
       
        'Search for additional occurences of the 'Format'
        'If found add them to the array to be returned
        bNeedMore = True
        While bNeedMore
          Set r = ws.Cells.Find(What:=sFindString, _
                     After:=r, _
                     LookIn:=xlValues, _
                     LookAt:=xlWhole, _
                     SearchOrder:=xlByRows, _
                     SearchDirection:=xlNext, _
                     MatchCase:=False, _
                     SearchFormat:=True)
                     
          'Keep on searching until the first 'Address' is found again
          sAddress = r.Address(False, False)
          If sAddress = sFirstAddress Then
            bNeedMore = False
          Else
            iCount = iCount + 1
            'MsgBox "Next occurrence of RED FORMAT is in cell '" & sAddress & "'."
          End If
        Wend
        
      End If
      
      'Set the return value
      LjmFindRedCells = iCount
      
      'Clear the object pointer
      Set r = Nothing
     
    
    End Function
    Similar code for your previous thread:
    Function LjmFindNext(ws As Worksheet, sFindString As String) As Long
      'This iterates through a worksheet finding all the occurences of a 'find string'
    
      Dim r As Range
      Dim iCount As Long
      Dim bNeedMore As Boolean
      Dim sAddress As String
      Dim sFirstAddress As String
      
      'Find the first occurence of the string
      Set r = Nothing
      Set r = ws.Columns("C").Find(What:=sFindString, _
                          After:=ws.Range("C1"), _
                          LookIn:=xlValues, _
                          LookAt:=xlWhole, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlNext, _
                          MatchCase:=False, _
                          SearchFormat:=False)
                          
                          
      If Not r Is Nothing Then
      
        'Save the found address as the 'First Address'
        'Save the value to be returned
        iCount = iCount + 1
        sFirstAddress = r.Address(False, False)
        MsgBox "First occurrence of '" & sFindString & "' is in cell '" & sFirstAddress & "'."
       
        'Search for additional values
        'If found add them to the array to be returned
        bNeedMore = True
        While bNeedMore
          Set r = ws.Columns("C").FindNext(After:=r)
          sAddress = r.Address(False, False)
          If sAddress = sFirstAddress Then
            bNeedMore = False
          Else
            iCount = iCount + 1
            MsgBox "Next occurrence of '" & sFindString & "' is in cell '" & sAddress & "'."
          End If
        Wend
     
      Else
        'MsgBox "Could not find '" & sFindString & "'"
      End If
     
      LjmFindNext = iCount
     
      'Clear the object pointer
      Set r = Nothing
     
    End Function
    Lewis
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    11-13-2015
    Location
    Titusville, PA
    MS-Off Ver
    2010
    Posts
    53

    Re: FindFormat ColorIndex Then Make A List

    Thanks for that, Haven't responded due to security issues on site.

    Thanks again.

+ 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. Using a 2nd Instance of FindFormat
    By stuartgb100 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-06-2015, 01:49 PM
  2. Using Findformat
    By stuartgb100 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-04-2015, 03:36 PM
  3. Use FindFormat on a Range
    By stuartgb100 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-30-2015, 02:01 PM
  4. [SOLVED] FindFormat Accounting Error
    By yusunghee in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-14-2013, 12:02 PM
  5. colorindex make a color a number
    By PanoIan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-04-2007, 09:05 PM
  6. [SOLVED] FindFormat with Borders not Working
    By xenovah@gmail.com in forum Excel General
    Replies: 0
    Last Post: 06-06-2006, 12:10 PM
  7. FindFormat w/ multiple spreadsheets
    By BarryQ in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-28-2005, 03:13 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