Vba: วิธีใช้การจัดรูปแบบตามเงื่อนไขกับเซลล์


คุณสามารถใช้วิธีการต่อไปนี้ใน VBA เพื่อใช้การจัดรูปแบบตามเงื่อนไขกับเซลล์:

วิธีที่ 1: ใช้การจัดรูปแบบตามเงื่อนไขตามเงื่อนไข

 Sub ConditionalFormatOne()
Dim rg As Range
Dim cond As FormatCondition

'specify range to apply conditional formatting
Set rg = Range(" B2:B11 ")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'apply conditional formatting to any cell in range B2:B11 with value greater than 30
Set cond = rg.FormatConditions.Add(xlCellValue, xlGreater, " =30 ")

'define conditional formatting to use
With cond
.Interior.Color = vbGreen
.Font.Color = vbBlack
.Font.Bold = True
End With

End Sub

วิธีที่ 2: ใช้การจัดรูปแบบตามเงื่อนไขตามเงื่อนไขต่างๆ

 Sub ConditionalFormatMultiple()
Dim rg As Range
Dim cond1 As FormatCondition, cond2 As FormatCondition, cond3 As FormatCondition

'specify range to apply conditional formatting
Set rg = Range(" A2:A11 ")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'specify rules for conditional formatting
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Mavericks ”)
Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Blazers ”)
Set cond3 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Celtics ”)

'define conditional formatting to use
With cond1
.Interior.Color = vbBlue
.Font.Color = vbWhite
.Font.Italic = True
End With

With cond2
.Interior.Color = vbRed
.Font.Color = vbWhite
.Font.Bold = True
End With

With cond3
.Interior.Color = vbGreen
.Font.Color = vbBlack
End With

End Sub

วิธีที่ 3: เอากฎการจัดรูปแบบตามเงื่อนไขทั้งหมดออกจากเซลล์

 Sub RemoveConditionalFormatting()
ActiveSheet.Cells.FormatConditions.Delete
End Sub

ตัวอย่างต่อไปนี้แสดงวิธีการใช้แต่ละวิธีในทางปฏิบัติกับชุดข้อมูลต่อไปนี้ใน Excel:

ตัวอย่างที่ 1: ใช้การจัดรูปแบบตามเงื่อนไขตามเงื่อนไข

เราสามารถใช้แมโครต่อไปนี้เพื่อเติมเซลล์ในช่วง B2:B11 ที่มีค่ามากกว่า 30 โดยมีพื้นหลังสีเขียว แบบอักษรสีดำ และรูปแบบข้อความตัวหนา:

 Sub ConditionalFormatOne()
Dim rg As Range
Dim cond As FormatCondition

'specify range to apply conditional formatting
Set rg = Range(" B2:B11 ")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'apply conditional formatting to any cell in range B2:B11 with value greater than 30
Set cond = rg.FormatConditions.Add(xlCellValue, xlGreater, " =30 ")

'define conditional formatting to use
With cond
.Interior.Color = vbGreen
.Font.Color = vbBlack
.Font.Bold = True
End With

End Sub

เมื่อเราเรียกใช้แมโครนี้ เราได้รับผลลัพธ์ต่อไปนี้:

โปรดทราบว่าแต่ละเซลล์ในช่วง B2:B11 ที่มีค่ามากกว่า 30 จะขึ้นอยู่กับการจัดรูปแบบตามเงื่อนไข

เซลล์ใดๆ ที่มีค่าเท่ากับหรือน้อยกว่า 30 จะถูกปล่อยให้อยู่ตามลำพัง

ตัวอย่างที่ 2: ใช้การจัดรูปแบบตามเงื่อนไขตามเงื่อนไขหลายข้อ

เราสามารถใช้แมโครต่อไปนี้เพื่อใช้การจัดรูปแบบตามเงื่อนไขกับเซลล์ในช่วง A2:A11 ตามชื่อทีม:

 Sub ConditionalFormatMultiple()
Dim rg As Range
Dim cond1 As FormatCondition, cond2 As FormatCondition, cond3 As FormatCondition

'specify range to apply conditional formatting
Set rg = Range(" A2:A11 ")

'clear any existing conditional formatting
rg.FormatConditions.Delete

'specify rules for conditional formatting
Set cond1 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Mavericks ”)
Set cond2 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Blazers ”)
Set cond3 = rg.FormatConditions.Add(xlCellValue, xlEqual, “ Celtics ”)

'define conditional formatting to use
With cond1
.Interior.Color = vbBlue
.Font.Color = vbWhite
.Font.Italic = True
End With

With cond2
.Interior.Color = vbRed
.Font.Color = vbWhite
.Font.Bold = True
End With

With cond3
.Interior.Color = vbGreen
.Font.Color = vbBlack
End With

End Sub

เมื่อเราเรียกใช้แมโครนี้ เราได้รับผลลัพธ์ต่อไปนี้:

โปรดทราบว่าเซลล์ที่มีชื่อทีม “Mavericks”, “Blazers” และ “Celtics” ล้วนอยู่ภายใต้การจัดรูปแบบตามเงื่อนไขเฉพาะ

มีเพียงทีมเดียวที่ชื่อ “Lakers” เท่านั้นที่ถูกทิ้งให้อยู่ตามลำพัง เนื่องจากเราไม่ได้ระบุกฎการจัดรูปแบบตามเงื่อนไขสำหรับเซลล์ที่มีชื่อทีมนั้น

ตัวอย่างที่ 3: ลบกฎการจัดรูปแบบตามเงื่อนไขทั้งหมดออกจากเซลล์

สุดท้าย เราสามารถใช้แมโครต่อไปนี้เพื่อลบกฎการจัดรูปแบบตามเงื่อนไขทั้งหมดออกจากเซลล์บนแผ่นงานปัจจุบัน:

 Sub RemoveConditionalFormatting()
ActiveSheet.Cells.FormatConditions.Delete
End Sub

เมื่อเราเรียกใช้แมโครนี้ เราได้รับผลลัพธ์ต่อไปนี้:

โปรดทราบว่าการจัดรูปแบบตามเงื่อนไขทั้งหมดได้ถูกลบออกจากแต่ละเซลล์แล้ว

แหล่งข้อมูลเพิ่มเติม

บทช่วยสอนต่อไปนี้จะอธิบายวิธีการทำงานทั่วไปอื่นๆ ใน VBA:

VBA: วิธีนับจำนวนอักขระในสตริง
VBA: วิธีตรวจสอบว่าสตริงมีสตริงอื่นหรือไม่
VBA: สูตรสำหรับ “ถ้า” เซลล์มี”

เพิ่มความคิดเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *