+ Reply to Thread
Results 1 to 6 of 6

VBA to change backgroundcolour in Freeform depending on text in cells

Hybrid View

  1. #1
    Registered User
    Join Date
    02-28-2014
    Location
    Europe
    MS-Off Ver
    Excel 2011
    Posts
    8

    VBA to change backgroundcolour in Freeform depending on text in cells

    Hello

    I'm new to VBA, so I need some help.
    I have a spreadsheet with several freeforms. I would like them to change backgroundcolour, if the content in a cell is equal to another cell.

    Basically I would like my shape "Freeform1" to change backgroundcolour to RGB (0, 180, 0) if Sheet1.Range ("A1") = Sheet2.Range("D3").
    If it's not the same content in both cells, I would like the freeform to remain unchanged, that's RGB (79, 129, 189).

    This should happend automaticly, so I don't have to click the shape to make it happen.

    Can anyone help me with this?

    Best regards
    Mixer

  2. #2
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Post Re: VBA to change backgroundcolour in Freeform depending on text in cells

    Here's your macro:
    Sub Auto_Color_FreeForm()
    With ThisWorkbook.Worksheets("Sheet1")
         If .Range("A1") = ThisWorkbook.Worksheets("Sheet2").Range("D3") Then _
         .Shapes("Freeform 1").Fill.ForeColor.RGB = RGB(0, 180, 0) Else _
         .Shapes("Freeform 1").Fill.ForeColor.RGB = RGB(79, 129, 189)
         End With
    End Sub
    to automate this, you will need to call this from the "Workbook_SheetChange" event of the workbook; like this
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Call Auto_Color_FreeForm
    End Sub
    so that the code runs everytime the user changes something and evaluates the condition.

  3. #3
    Registered User
    Join Date
    02-28-2014
    Location
    Europe
    MS-Off Ver
    Excel 2011
    Posts
    8

    Re: VBA to change backgroundcolour in Freeform depending on text in cells

    Thank you jewelsharma.

    I tried to use the macro you gave me, I've just changed the cells and sheets to fit my new document. The shapes are in sheet1 and the reference cells in sheet2 and 3.

    It looks like this now:

    Sub Auto_Color_FreeForm()
    With ThisWorkbook.Worksheets("Sheet1")
    If .ThisWorkbook.Worksheets("Sheet2").Range("A3") = ThisWorkbook.Worksheets("Sheet3").Range("D3") Then _
    .Shapes("Rounded Rectangle 4").Fill.ForeColor.RGB = RGB(0, 180, 0) Else _
    .Shapes("Rounded Rectangle 4").Fill.ForeColor.RGB = RGB(79, 129, 189)
    End With
    End Sub

    When I try to run it, with the second macro you wrote, I get an error message on line nr 3. Error code 383: Object doesn't support this property or method.
    Any suggestions on how to fix this?

  4. #4
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Re: VBA to change backgroundcolour in Freeform depending on text in cells

    I'll check & revert tomorrow.

  5. #5
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Re: VBA to change backgroundcolour in Freeform depending on text in cells

    remove the "." just before "ThisWorkbook" in line No. 3 of your code; and then run it again.
    Let me know if anything, or else mark the thread as [I]SOLVED[/]

    Note: Please use the code tags when sharing the code in future. It makes your post neater and easier for all to follow.

  6. #6
    Registered User
    Join Date
    02-28-2014
    Location
    Europe
    MS-Off Ver
    Excel 2011
    Posts
    8

    Re: VBA to change backgroundcolour in Freeform depending on text in cells

    Thanks, it runs perfectly now.

+ 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. Convert freeform user input and write to a fixed length text file
    By vijaymohan10 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-02-2013, 04:44 AM
  2. [SOLVED] listbox and freeform text in one cell?
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-18-2013, 02:05 PM
  3. [SOLVED] Cells to change colour depending on specific text
    By Fran Habbitts in forum Excel General
    Replies: 4
    Last Post: 12-11-2012, 10:50 AM
  4. Adding text to a freeform shape programmatically Excel VBA
    By waqas716 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-01-2011, 07:57 AM
  5. [SOLVED] Pick list or freeform text
    By PJH in forum Excel General
    Replies: 2
    Last Post: 03-30-2006, 12:15 AM

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