वीबीए: कोशिकाओं पर सशर्त स्वरूपण कैसे लागू करें
आप कक्षों पर सशर्त स्वरूपण लागू करने के लिए 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
निम्नलिखित उदाहरण दिखाते हैं कि एक्सेल में निम्नलिखित डेटा सेट के साथ व्यवहार में प्रत्येक विधि का उपयोग कैसे करें:
उदाहरण 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
जब हम इस मैक्रो को चलाते हैं, तो हमें निम्नलिखित आउटपुट प्राप्त होता है:
ध्यान दें कि टीम के नाम “मावेरिक्स”, “ब्लेज़र्स” और “सेल्टिक्स” वाले सभी सेल विशिष्ट सशर्त स्वरूपण के अधीन हैं।
“लेकर्स” नाम वाली एकमात्र टीम अकेली रह गई है क्योंकि हमने उस टीम नाम वाले सेल के लिए कोई सशर्त स्वरूपण नियम निर्दिष्ट नहीं किया है।
उदाहरण 3: कक्षों से सभी सशर्त स्वरूपण नियम हटाएँ
अंत में, हम वर्तमान शीट पर कोशिकाओं से सभी सशर्त स्वरूपण नियमों को हटाने के लिए निम्नलिखित मैक्रो का उपयोग कर सकते हैं:
Sub RemoveConditionalFormatting()
ActiveSheet.Cells.FormatConditions.Delete
End Sub
जब हम इस मैक्रो को चलाते हैं, तो हमें निम्नलिखित आउटपुट प्राप्त होता है:
ध्यान दें कि प्रत्येक कक्ष से सभी सशर्त स्वरूपण हटा दिए गए हैं।
अतिरिक्त संसाधन
निम्नलिखित ट्यूटोरियल बताते हैं कि वीबीए में अन्य सामान्य कार्य कैसे करें:
वीबीए: एक स्ट्रिंग में वर्णों की घटनाओं की गणना कैसे करें
वीबीए: कैसे जांचें कि एक स्ट्रिंग में दूसरी स्ट्रिंग है या नहीं
वीबीए: “यदि” सेल में शामिल है” के लिए एक सूत्र