简体   繁体   中英

Conditional Formatting - Changing cell color based on another cells text

Currently trying to debug my code. I want the first cell of the row to change a specific color based on the text in a cell in the same row. The text that is conditional is based in a drop-down list that the user manually inputs when the macro gets done running.

    For polerow = 14 To lastrow + 12
        If Cells(polerow, 27).Value = "Simple" Then
            Cells(polerow, 1).Interior.ColorIndex = 5
            '.Cells(PoleRow,1).Interior.Colorindex = 5
        End If
    Next

I basically am trying to write something in formula format. The text that is conditional is "Simple". Thanks!

I think that your issue is you have not set the lastrow , you also need to identify the workbook and sheet references.

Dim cel As Range, lastrow As Long

lastrow = ThisWorkbook.Sheets("Sheet1").UsedRange.Rows.Count 'sets the number of used rows

    'loop through each cell in Col27 from row 14 to the lastrow + 12 
    For Each cel In ThisWorkbook.Sheets("Sheet1").Range("AA14:AA" & lastrow + 12)             
        If cel.Value = "Simple" Then 'test cell value for condition
            cel.Offset(, -26).Interior.ColorIndex = 5 'if condition is met then offset to first cell in row and color
        End If
    Next cel 'loop to next cell

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