Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ซอฟต์แวร์ >> Office

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

การใช้ VBA Macro เป็นวิธีที่มีประสิทธิภาพ เร็ว และปลอดภัยที่สุดสำหรับการดำเนินการต่างๆ ใน Excel บทความนี้จะแสดงวิธีตรวจสอบ ถ้าสีตัวอักษรเป็นสีแดง ให้คืนค่าผลลัพธ์ที่ต้องการ 6 วิธี ด้วย VBA

ดาวน์โหลดไฟล์ฝึกปฏิบัติ

ดาวน์โหลดไฟล์ Excel ฟรีได้ที่นี่

6 วิธี ตรวจสอบ Font สีแดง คืนค่าผลลัพธ์ ด้วย VBA

ชุดข้อมูลตัวอย่างมีค่าบางช่องเป็นตัวอักษรสีแดง

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

1. ถ้า Font สีแดง คืนคำว่า "Yes" ไม่ใช่ คืน "No"

สร้าง User Defined Function (UDF) ใช้ในเซลล์เหมือนฟังก์ชันปกติ

ขั้นตอน:

  1. กด Alt + F11 หรือแท็บ Developer > Visual Basic

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

  1. ในหน้าต่าง VBA: เมนู Insert > Module

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

  1. วางโค้ด:
Function FontColorRed(target As Range) 
    Application.Volatile 
    If target.Font.Color = 255 Then 
        FontColorRed = "Yes" 
    Else 
        FontColorRed = "No" 
    End If 
End Function

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

สำคัญ: นี้เป็น UDF ไม่ใช่ Sub → กด Save (ไม่ใช่ Run)

  1. กลับไป Worksheet ใช้สูตร: =FontColorRed(C5)
  2. กด Enter → ได้ Yes/No

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

  1. ลาก Fill Handle ลงไป → ได้ Yes ทุกเซลล์ที่เป็นสีแดง

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

2. คืนค่า Color Code (ColorIndex) ถ้า Font สีแดง

Function ColorCode(rng As Range) 
    ColorCode = rng.Font.ColorIndex 
End Function

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

  • ใช้สูตร: =ColorCode(C5) → ได้ ColorIndex (สีแดง = 3)
  • ลาก Fill Handle ได้รหัสสีทุกเซลล์

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

3. คืนค่า Index Number (RGB Value) ถ้า Font สีแดง

Function IndexColor(cell As Range) 
    IndexColor = cell.Font.Color 
End Function

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

  • ใช้สูตร: =IndexColor(C5) → ได้ค่า RGB (สีแดง = 255)
  • ลาก Fill Handle → เซลล์สีแดงจะคืนค่า 255

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

4. คืนค่า RGB Code (รูปแบบ 255,0,0) ถ้า Font สีแดง

Function FontRGB(cell As Range) 
    Dim iColorIndex As Long 
    Dim iColor As Variant 
    iColorIndex = cell.Font.Color 
    iColor = iColorIndex Mod 256 
    iColor = iColor & ", " 
    iColor = iColor & (iColorIndex \ 256) Mod 256 
    iColor = iColor & ", " 
    iColor = iColor & (iColorIndex \ 256 \ 256) Mod 256 
    FontRGB = iColor 
End Function

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

  • ใช้สูตร: =FontRGB(C5) → ได้ RGB string เช่น "255, 0, 0"
  • ลาก Fill Handle → เซลล์สีแดงแสดง 255,0,0

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

5. ถ้า Font สีแดง ให้ Highlight เซลล์ (Sub Procedure)

Sub HighlightCell() 
    Set ws = Sheets("Highlight") 
    For r = 1 To 104 
        For c = 1 To 36 
            If (ws.Cells(r, c).Font.Color = 255) Then 
                ws.Cells(r, c).Interior.ColorIndex = 34 
            End If 
        Next c 
    Next r 
End Sub

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

  • กด F5 หรือ Run Sub/UserForm → เซลล์ที่มี Font สีแดง จะถูก Highlight สีเขียวอ่อน (ColorIndex 34)

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

6. เปลี่ยนสี Font จากสีแดง กลับเป็นค่าเริ่มต้น (Automatic)

Sub ChangeFontColor() 
    With Application.FindFormat.Font 
        .Subscript = False 
        .Color = 255 
        .TintAndShade = 0 
    End With 
    With Application.ReplaceFormat.Font 
        .Subscript = False 
        .ColorIndex = xlAutomatic 
        .TintAndShade = 0 
    End With 
    Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder:= _ 
        xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True 
End Sub

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

  • Run Macro → Font สีแดงทั้งหมดกลายเป็นสีอัตโนมัติ (ดำ/ตามธีม)

ตรวจจับเซลล์ตัวอักษรสีแดงด้วย VBA ดึงผลลัพธ์ใน Excel

สรุป

บทความนี้ครอบคลุม 6 วิธี VBA สำหรับ Font สีแดง: 1) UDF คืน Yes/No 2) UDF คืน ColorIndex 3) UDF คืน RGB Value 4) UDF คืน RGB String (255,0,0) 5) Sub Highlight เซลล์ 6) Sub เปลี่ยน Font สีแดงเป็นค่าเริ่มต้น เลือกใช้ตามความต้องการครับ

บทความน่าสนใจเพิ่มเติม

  • VBA to Remove Characters from String in Excel (7 Methods)
  • How to Use VBA to Delete Empty Rows in Excel
  • VBA Macro to Insert Row in Excel Based on Criteria (4 Methods)
  • How to Count Rows with VBA in Excel (5 Approaches)
  • VBA to Delete Entire Row based on Cell Value in Excel (3 Methods)