gpt-3.5-turbo-0125· openai
Suzhen/CodeChat
write an vba code for this situatuation: i have an sheet named Licente with value DA or…
- 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
- 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 SubYou can run this code by pressing
Alt + F11to open the VBA editor, then go toInsert > Moduleto insert a new module and paste the code there. After that, you can run the macro by pressingF5or going toRun > Run Sub/UserForm. This code will color the cells in the specified ranges based on the conditions you've provided.