用excel VBA 檢查H-V欄的每個單元格是否包含B欄中的任何內容,如果包含,則將該單元格的文字和對應B欄的文字顏色設為紅色
- User
用excel VBA 檢查H-V欄的每個單元格是否包含B欄中的任何內容,如果包含,則將該單元格的文字和對應B欄的文字顏色設為紅色
Assistant
要實現這個功能,你可以使用 Excel VBA 的
For迴圈和If判斷式來檢查每個單元格的內容。以下是示例代碼:Sub CheckAndHighlight() Dim ws As Worksheet Set ws = ThisWorkbook.ActiveSheet Dim i As Long Dim j As Long Dim cell As Range Dim targetCell As Range ' 遍歷 H-V 欄的每個單元格 For i = 1 To ws.Cells(ws.Rows.Count, "H").End(xlUp).Row For j = 8 To 22 ' H-V 欄的欄號 Set cell = ws.Cells(i, j) ' 檢查 B 欄的每個單元格 For Each targetCell In ws.Range("B1:B" & ws.Cells(ws.Rows.Count, "B").End(xlUp).Row) If InStr(1, cell.Value, targetCell.Value) > 0 Then ' 如果包含,則將該單元格的文字和對應 B 欄的文字顏色設為紅色 cell.Font.Color = vbRed targetCell.Font.Color = vbRed End If Next targetCell Next j Next i End Sub
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.