+ Reply to Thread
Results 1 to 4 of 4

script to write 1's and zero's for cell color

Hybrid View

  1. #1
    Registered User
    Join Date
    05-07-2010
    Location
    Cayman Islands
    MS-Off Ver
    Excel 2007
    Posts
    2

    Question script to write 1's and zero's for cell color

    I have a very large excel workbook that has a sheet off almost 600,000 lines, column B has duplicates. i have done a conditional format to highlight all unique and first occurrences.
    i am trying to write a VB script that will go down every row and if the cell is colored that write a 1 in cell z for that row if it is not colored then write a 0 in cell z for the row.

    or a program that will in one sweep find the unique & first occurrences and mark the cell with 1 or 0

    ANY help is appreciated.

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: script to write 1's and zero's for cell color

    Hello & Welcome to the forum,

    I would suggest just using the formula =COUNTIF($B$2:B2,B2) and copy down.

    If the value in B is unique you will get a 1 or if a duplicate you will get a 2, if a 3rd duplicate then a 3 and so on.

    You can now build conditional formatting off of the results in Z.

    If you want to return a zero, then incorporate an If statement =IF(COUNTIF($B$2:B2,B2)=1,0,1)

    You could even do away with column Z and put this formula directly into conditional formatting.

  3. #3
    Forum Expert Palmetto's Avatar
    Join Date
    04-04-2007
    Location
    South Eastern, USA
    MS-Off Ver
    XP, 2007, 2010
    Posts
    3,978

    Re: script to write 1's and zero's for cell color

    Seeing as you are using Excel 2007 and it allows filtering by color, you can script this.
    Here is an example you can build on:

    Note: you will need to determine the RGB value of the cell color, which can be done by simply activating the cell and calling up the color pallet via Format Cells. If you have multiple cell colors then you will need multiple passes to accomplish.

    Using the specialcells method will be much faster than looping through 600K cells.

    Option Explicit
    
    Sub Check_Cell_Color()
    
        Application.ScreenUpdating = False
        
        With Sheet1
            If .AutoFilterMode = True Then .AutoFilterMode = False
            .Range("B1:B600000").AutoFilter Field:=1, Criteria1:=RGB(242, 242, 242), Operator:=xlFilterCellColor
            .Range("Z1:Z600000").SpecialCells(xlCellTypeVisible).Value = 1
            .AutoFilterMode = False
            .Range("B1:A600000").AutoFilter Field:=1, Operator:=xlFilterNoFill
            .Range("Z1:Z600000").SpecialCells(xlCellTypeVisible).Value = 0
            .AutoFilterMode = False
        End With
        
        Application.ScreenUpdating = False
    
    End Sub
    Palmetto

    Do you know . . . ?

    You can leave feedback and add to the reputation of all who contributed a helpful response to your solution by clicking the star icon located at the left in one of their post in this thread.

  4. #4
    Registered User
    Join Date
    05-07-2010
    Location
    Cayman Islands
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: script to write 1's and zero's for cell color

    Thank you, you guys are great.
    I have to know is there any way to do this with one pass?

    Find the unique and first instance and then place the ones for those and 0’s for the duplicates?

+ 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