简体   繁体   中英

Cell Interior Color Index Excel VBA

Based on a language table, column A = Language, B = number, C = coloredcell

I would like to know what is the VBA so whenever I type a number on Column B (using Workbook_SheetChange), C is colored with the Colorindex equal to the number typed.

On the other hand, and I am sure is part of the solution to the previous question, on VBA how do I write cell.Interior.ColorIndex = (a specific cell value, If B2=4 -> for the row, whole or until last column has data, cell.Interior.ColorIndex = 4) and color the whole row.

Thank you

The sheetchange function has target as an argument, that's the cell that you changed. You can use it to change the relevant cell:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 2 Then
        Target.Offset(0,1).Interior.ColorIndex = Target.Value
        'and for the whole row
        Target.EntireRow.Interior.Color = Target.Offset(0,1).Interior.Color
    Endif 
End Sub

Right click on the sheet's name on which you want this functionality, and click on 'View Code'.

Now you need to write a VBA function that fires on any change to the worksheet. This is an inbuilt function called Worksheet_Change(Range) . The range object (it's argument) is the range that had changed when this function fired.

Private Sub Worksheet_Change(ByVal Target As Range)
End Sub

Inside the function you need to check whether the changed cell was in column B. This is done by the Column property of the Target range.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 2 Then
        ' The changed cell was in column B
    End If
End Sub

Now you need to get the cell's value and put it as the row's ColorIndex.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Column = 2 Then
        ColorValue = Target.Value
        Target.EntireRow.Interior.ColorIndex = ColorValue
    End If
End Sub

Edit: To color the cells only till the last value in the row, you need to count the number of filled cells in the row. The following code does that (see the comments to understand it)

Private Sub Worksheet_Change(ByVal Target As Range)

    ' Check if the edited cell is in column B
    If Target.Column = 2 Then

        ' Get the value of the edited cell
        ColorValue = Target.Value

        ' Get the row number of the edited cell
        RowNumber = Target.Row

        ' Get the number of filled cells in this row
        CellsCount = Application.WorksheetFunction.CountA(Range(RowNumber & ":" & RowNumber))

        ' Apply the color formatting till the last column in this row
        Range(Cells(RowNumber, 1), Cells(RowNumber, CellsCount)).Interior.ColorIndex = ColorValue

    End If
End Sub

The code of Nick Dewitt is OK, but it color only the column C.

If you want to color the entire row, starting from C depending of how much columns are in the row :

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lastcol As Integer, i As Integer

    If Target.Column = 2 Then
        lastcol = Cells(Target.Row, Columns.Count).End(xlToLeft).Column
        For i = 3 To lastcol
            Target.Offset(0, i - 2).Interior.ColorIndex = Target.Value
        Next i
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM