+ Reply to Thread
Results 1 to 19 of 19

VBA script to color cells

  1. #1
    Registered User
    Join Date
    09-23-2014
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    31

    VBA script to color cells

    Hi and thank you in advance. I am looking for a VBA code where I can accomplish the following:

    - Check for column A if column A has the text "abc" and "def" // column A can only have abc or def cant have both
    - If found look at the row where the text "abc" or "def" exist and check the corresponding cell on column C (which is date) for the row abc or def
    - If date exist on the corresponding cell on column C then gray out all cells above column C all the way to C2 as C1 is the heading
    - if no value then ignore.

    thank you!

  2. #2
    Valued Forum Contributor
    Join Date
    11-29-2022
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    435

    Re: VBA script to color cells

    So if your data went to row 500, and on row 500 Column A is abc and there is a date in C500, you want C2:C499 greyed out? I think I am missing something here.

  3. #3
    Forum Contributor
    Join Date
    11-23-2022
    Location
    Amsterdam
    MS-Off Ver
    2021
    Posts
    193

    Re: VBA script to color cells

    Try this

    Please Login or Register  to view this content.
    Last edited by kaasplankje; 12-02-2022 at 03:50 AM.

  4. #4
    Registered User
    Join Date
    09-23-2014
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    31

    Re: VBA script to color cells

    You are correct that is exactly what I am looking for

  5. #5
    Registered User
    Join Date
    09-23-2014
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    31

    Re: VBA script to color cells

    Quote Originally Posted by kaasplankje View Post
    Try this

    Please Login or Register  to view this content.
    Unfortunately the macro above did not work , I am pasting the image of sample data: Attachment 807654Attachment 807654

  6. #6
    Forum Contributor
    Join Date
    11-23-2022
    Location
    Amsterdam
    MS-Off Ver
    2021
    Posts
    193

    Re: VBA script to color cells

    Can't view your attachments for some reason but I am guessing that, as you are using excel 2010 (upgrading costs virtually nothing nowadays), some instructions are perhaps not understood.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: VBA script to color cells

    If there are multiple rows that matches the condition, this will get the last matched.

    If you want the first match, change max to min

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    09-23-2014
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    31

    Re: VBA script to color cells

    Quote Originally Posted by jindon View Post
    If there are multiple rows that matches the condition, this will get the last matched.

    If you want the first match, change max to min

    Please Login or Register  to view this content.
    There is only one row matching the results, however when I ran the script it did not do anything no grayout no errors which was weird. in column A I have the task where I am looking for either abc or def and my date column is column D. I tried to update your VBA but no luck I also updated the spreadsheet to macth your VBA to show data only on column A and B but no luck either.

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: VBA script to color cells

    Quote Originally Posted by Tescatlipoca View Post
    - Check for column A if column A has the text "abc" and "def" // column A can only have abc or def cant have both
    - If found look at the row where the text "abc" or "def" exist and check the corresponding cell on column C (which is date) for the row abc or def
    Quote Originally Posted by Tescatlipoca View Post
    in column A I have the task where I am looking for either abc or def and my date column is column D. I tried to update your VBA
    How did you update/adjust?

    Upload sample workbook.

  10. #10
    Registered User
    Join Date
    09-23-2014
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    31

    Re: VBA script to color cells

    test.xlsx

    see attached. The only change I made to your VBA was to update the index 0,2 to 0,3.

  11. #11
    Registered User
    Join Date
    09-23-2014
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    31

    Re: VBA script to color cells

    to clarify abc in this case is PSA/Lease Executed line 23

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: VBA script to color cells

    Your sample workbook is not like what you said in your opening post.
    So, you need to explain what exactly you want in words, or upload your workbook with before/after sheet clearly showing what you are trying to do.

  13. #13
    Registered User
    Join Date
    09-23-2014
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    31

    Re: VBA script to color cells

    Quote Originally Posted by jindon View Post
    Your sample workbook is not like what you said in your opening post.
    So, you need to explain what exactly you want in words, or upload your workbook with before/after sheet clearly showing what you are trying to do.
    Apologies, I summitted the wrong file on the previous message. Here is the correct before and after files.

    before.xlsx
    after.xlsx

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: VBA script to color cells

    See if this is how you wanted
    Please Login or Register  to view this content.
    Last edited by jindon; 12-02-2022 at 01:14 PM.

  15. #15
    Registered User
    Join Date
    09-23-2014
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    31

    Re: VBA script to color cells

    Quote Originally Posted by jindon View Post
    See if this is how you wanted
    Please Login or Register  to view this content.
    This macro changes the colors on the schedule, all I need is to search and find "PSA / LEASE EXECUTED" then color the corresponding date, in this case D23 to D5. Hope that make sense and thank you for all your effort...

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: VBA script to color cells

    change to
    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    09-23-2014
    Location
    Chicago
    MS-Off Ver
    2010
    Posts
    31

    Re: VBA script to color cells

    Quote Originally Posted by jindon View Post
    change to
    Please Login or Register  to view this content.
    I do appreciate the effort but no luck again

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: VBA script to color cells

    Since it is working as you explained here, your excel is something wrong or upload the workbook that is not working with the code you are currently using.

  19. #19
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,676

    Re: VBA script to color cells

    Try:
    PHP Code: 
    Sub test()
    Dim txt1 As Stringtxt2 As Stringf
    txt1 
    "PSA / LEASE EXECUTED"
    txt2 "What is text 2?" ' apply 2nd text criteria here
    Set f = Columns(1).Find(txt1)
    If f Is Nothing Then Set f = Columns(1).Find(txt2)
    If f Is Nothing Or Not IsDate(f.Offset(, 3)) Then MsgBox "No such data", vbCritical: Exit Sub
    Range("D5", f.Offset(, 3)).Interior.Color = 13553360
    End Sub 
    Quang PT

+ 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. Need A Fast Script To Copy 80000 Cells But My Current Script Is Too Slow
    By Genus Max in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-20-2019, 09:54 AM
  2. [SOLVED] VBA: Script that changes new text to another color
    By hector.febus in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-24-2014, 08:20 AM
  3. VBA: Script to change text color if a change is made
    By hector.febus in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-05-2014, 11:06 AM
  4. [SOLVED] Script for filling cells with specific color
    By adygelber in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-11-2012, 05:01 AM
  5. script to write 1's and zero's for cell color
    By NPeters in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-07-2010, 01:52 PM
  6. VBA Script for Onclick Color Formatting
    By Solar in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-02-2009, 01:46 PM
  7. Replies: 1
    Last Post: 06-27-2006, 10:00 AM

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.6.0 RC 1