การเปลี่ยนสีตัวอักษร (Text Color) ใน Excel ช่วยเน้นและวิเคราะห์ข้อมูล แม้ไม่มีฟังก์ชันโดยตรง แต่ทำได้ 2 วิธี: Conditional Formatting และ VBA User-Defined Function
ดาวน์โหลดไฟล์ฝึกฝน
ดาวน์โหลดเทมเพลต Excel ฟรีเพื่อฝึกเอง
2 วิธีเปลี่ยนสีตัวอักษรด้วยสูตร
ข้อมูลตัวอย่าง: ชื่อหนังสือและราคาออนไลน์ 2 ปี ต้องการเปลี่ยนสีราคาตามเงื่อนไข

วิธีที่ 1: Conditional Formatting (แนะนำ - ไม่ต้องเขียนโค้ด)
#1 Highlight Cells Rules (กฎสำเร็จรูป)
มี 4 ตัวเลือก: Greater Than, Less Than, Between, Equal To

Greater Than: เลือกช่วง C5:D12 → Home → Conditional Formatting → Highlight Cells Rules → Greater Than → ใส่ 120 → เลือกสี → OK
ผล: ค่า > 120 เปลี่ยนสี


Less Than: ใส่ 110 → ค่า < 110 เปลี่ยนสี


Between: ใส่ 105 ถึง 135 → ค่าระหว่างช่วงเปลี่ยนสี


Equal To: ใส่ 150 → ค่า = 150 เปลี่ยนสี


#2 New Rule (สูตรกำหนดเอง)
เช่น เปลี่ยนสีแถวทั้งแถว หากค่า Column C > Column D
- เลือก C5:D12 → Home → Conditional Formatting → New Rule
- เลือก Use a formula to determine which cells to format
- สูตร:
=$C5>$D5(ใช้ $ ล็อกคอลัมน์) - Format → Font → Color: ส้ม → OK → OK





วิธีที่ 2: VBA User-Defined Function (UDF)
สร้างฟังก์ชัน TextColor เอง
- Alt+F11 → Insert → Module
- วางโค้ด:
Function TextColor(n1 As Double, n2 As Double) As Boolean
If n1 <= n2 Then
Application.Caller.Font.ColorIndex = 3 ' Red
Else
Application.Caller.Font.ColorIndex = xlAutomatic ' Normal
End If
End Function
Sub Tcolor() End Sub
- กด Run (F5) → ใส่ชื่อ Macro → Create → Run อีกครั้ง
- ใช้ในเซลล์:
=IF(C5>D5,1,0)+TextColor(C5,D5)






ผล: เงื่อนไขไม่ตรง → คืน 0 สีแดง | เงื่อนไขตรง → คืน 1 สีปกติ
สรุป
วิธี 1: Conditional Formatting - ใช้ง่าย ไม่ต้องโค้ด รองรับ Highlight Cells Rules และ Custom Formula วิธี 2: VBA UDF - ยืดหยุ่นกว่า แต่ซับซ้อนกว่า และ UDF มีข้อจำกัด (ไม่สามารถเปลี่ยนโครงสร้างชีทได้) แนะนำให้ใช้ Conditional Formatting สำหรับงานทั่วไป
บทความที่เกี่ยวข้อง
- How to Change a Row Color Based on a Text Value in a Cell in Excel
- Change the color of text and background of cells – An Excel Font and Fill Color Complete Guide
- 3 Methods to Convert Scientific Notation to Text in Excel
- Excel VBA to Highlight Specific Text in a Cell (6 Ways)
- Conditional Formatting Text Color (3 Easy Ways)