+ Reply to Thread
Results 1 to 8 of 8

VBA - color in cells

  1. #1
    Registered User
    Join Date
    12-06-2023
    Location
    Norway
    MS-Off Ver
    365
    Posts
    12

    VBA - color in cells

    Im setting up a risk assesment sheet, with a matrix of 25 (5x5) fields, having different colors green-yellow-red-white. The 5x5 matrix is labeled A-E on the X-axis and 1-5 on the Y-axis.
    Bellow there is a table describing new riskassesments, with two fields Safety (1-5) and consequence (A-E). Trying to make a VBA code changing the backgroundcolor of these cells to match the risk matrix color matching this value.
    Looking to make this better than alot of If statements checking out all combinations.

    Skjermbilde 2023-12-06 081021.png

    Skjermbilde 2023-12-06 081115.png

    My excel/VBA skills are somewhat limited, but Im a quick learner :D

    Lars

  2. #2
    Valued Forum Contributor
    Join Date
    08-31-2007
    Location
    SW Ireland
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2311 Build 16.0.17029.20068) 32-bit
    Posts
    536
    My initial thoughts are to use Conditional Formatting rather than VBA.
    Are you familiar with Conditional formatting?
    Excel 365 user. To unblock a downloaded macro-enabled workbook, go to your "Downloads" folder > right click on the workbook name > click 'Properties' > check the 'Unblock' checkbox. You can now open the workbook.

  3. #3
    Registered User
    Join Date
    12-06-2023
    Location
    Norway
    MS-Off Ver
    365
    Posts
    12

    Re: VBA - color in cells

    Sorry for not posting my current code, but after my 100'th "If cell1 = 1 and cell2 = D then cellcolor = Green..." I understood my desperate need of help

  4. #4
    Registered User
    Join Date
    12-06-2023
    Location
    Norway
    MS-Off Ver
    365
    Posts
    12

    Re: VBA - color in cells

    Hi, thanks. Yes, have a little experience with cond.format, but only in tables with containing numbervalues. Not sure how to adress a field outside the table to get a color from another celllocation. Cell F37 and G37 = this color if CELLVALUE(F37) = X and CELLVALUE(G37) = this.

  5. #5
    Registered User
    Join Date
    12-06-2023
    Location
    Norway
    MS-Off Ver
    365
    Posts
    12

    Re: VBA - color in cells

    My best attempt so far is repeating the code bellow for each fielt i want to color (10 in total). Anyone have a way to repeat this Case for 10 different fields in the worksheet?
    Select Case (Cells(36, 7).Value & Cells(36, 6).Value)
    Case "A1", _
    "A2", _
    "A3", _
    "A4", _
    "A5", _
    "B1", _
    "B2", _
    "C1"
    Cells(36, 6).Interior.Color = RGB(0, 255, 0)
    Cells(36, 7).Interior.Color = RGB(0, 255, 0)

    Case "B3", _
    "B4", _
    "B5", _
    "C2", _
    "C3", _
    "D1", _
    "D2", _
    "E1"
    Cells(36, 6).Interior.Color = RGB(255, 255, 0)
    Cells(36, 7).Interior.Color = RGB(255, 255, 0)
    Case "C4", _
    "C5", _
    "D3", _
    "D4", _
    "E2", _
    "E3"
    Cells(36, 6).Interior.Color = RGB(255, 0, 0)
    Cells(36, 7).Interior.Color = RGB(255, 0, 0)

    Case Else
    Cells(36, 6).Interior.Color = RGB(255, 255, 255)
    Cells(36, 7).Interior.Color = RGB(255, 255, 255)

    End Select

  6. #6
    Valued Forum Contributor
    Join Date
    08-31-2007
    Location
    SW Ireland
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2311 Build 16.0.17029.20068) 32-bit
    Posts
    536

    Re: VBA - color in cells

    Try the following to give you a start.

    I used named ranges as follows: the coloured cells MyTable, the Y-axis "YAxis" and the XAxis "Xaxis"

    There's not much error checking and likely other checks and balances needed but it seems to give the required colour.

    Note that on the X-axis you'd need just to show the letters A to E (this is what you appear to be entering in column G of the pictured example). Any additional text description such as 'High risk' etc should be shown in another cell beneath the X-Axis.

    Please Login or Register  to view this content.
    Last edited by deadlyduck; 12-07-2023 at 07:57 AM.

  7. #7
    Valued Forum Contributor
    Join Date
    08-31-2007
    Location
    SW Ireland
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2311 Build 16.0.17029.20068) 32-bit
    Posts
    536

    Re: VBA - color in cells

    A more flexible alternative is as follows- this code assumes that the cell immediately above the one that you fill in contains either the letter 'S' or 'K', so doesn't rely on the column you're in:

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    12-06-2023
    Location
    Norway
    MS-Off Ver
    365
    Posts
    12

    Re: VBA - color in cells

    Thanks for all the help, got it to work.

+ 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. Replies: 3
    Last Post: 09-07-2022, 09:31 AM
  2. Replies: 2
    Last Post: 02-06-2015, 09:15 PM
  3. Replies: 3
    Last Post: 02-06-2015, 09:10 PM
  4. Replies: 3
    Last Post: 05-30-2014, 10:24 AM
  5. Replies: 3
    Last Post: 01-08-2014, 03:56 PM
  6. Replies: 12
    Last Post: 09-18-2012, 03:13 PM
  7. Replies: 3
    Last Post: 07-08-2010, 06:48 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