Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0125· openaiHugging Face Suzhen/CodeChat

write an vba code for this situatuation: i have an sheet named Licente with value DA or…

  1. User

    write an vba code for this situatuation: i have an sheet named Licente with value DA or NU on B column and i want cell colour with DA to be green , and with NO to be red, also all cells from all raw that contain an x mark inside coresponding to value DA OR NU same colour , if it contain nothing must be white

  2. Assistant

    You can use the following VBA code to achieve this:

    Sub ColorCells()
        Dim ws As Worksheet
        Dim rng As Range
        Dim cell As Range
        Dim lastRow As Long
        
        Set ws = ThisWorkbook.Sheets("Licente")
        lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
        Set rng = ws.Range("B2:B" & lastRow)
        
        For Each cell In rng
            If cell.Value = "DA" Or cell.Value = "NU" Then
                If InStr(cell.Value, "x") > 0 Then
                    cell.Interior.Color = RGB(0, 255, 0) 'Green
                Else
                    cell.Interior.Color = RGB(255, 0, 0) 'Red
                End If
            Else
                cell.Interior.Color = RGB(255, 255, 255) 'White
            End If
        Next cell
    
    End Sub
    

    You can run this code by pressing Alt + F11 to open the VBA editor, then go to Insert > Module to insert a new module and paste the code there. After that, you can run the macro by pressing F5 or going to Run > Run Sub/UserForm. This code will color the cells in the specified ranges based on the conditions you've provided.