VBA स्ट्रिंग तुलना - दो स्ट्रिंग वैल्यू की तुलना कैसे करें?

एक्सेल VBA स्ट्रिंग तुलना

VBA में दो स्ट्रिंग्स की तुलना करने के लिए हमारे पास एक अंतर्निहित फ़ंक्शन है जिसका अर्थ है " स्ट्रॉमकॉम "। इसे हम " स्ट्रिंग तुलना " के रूप में पढ़ सकते हैं , यह फ़ंक्शन केवल VBA के साथ उपलब्ध है और वर्कशीट फ़ंक्शन के रूप में उपलब्ध नहीं है। यह किसी भी दो तारों की तुलना करता है और परिणामों को "शून्य (0)" के रूप में लौटाता है यदि दोनों तार मेल खा रहे हैं और यदि दोनों आपूर्ति तार मेल नहीं खा रहे हैं तो हमें परिणाम के रूप में "एक (1)" मिलेगा ।

VBA या एक्सेल में, हम विभिन्न परिदृश्यों का भरपूर सामना करते हैं। ऐसा ही एक परिदृश्य है "दो स्ट्रिंग मूल्यों की तुलना करना।" एक नियमित वर्कशीट में, हम इन कई तरीकों से कर सकते हैं, लेकिन VBA में, आप यह कैसे करते हैं?

नीचे "StrComp" फ़ंक्शन का सिंटैक्स है।

सबसे पहले, दो तर्क काफी सरल हैं,

  • के लिए स्ट्रिंग 1, हम क्या पहले मान हम तुलना कर रहे हैं की आपूर्ति करने की जरूरत है और
  • के लिए स्ट्रिंग 2, हम दूसरे मूल्य हम तुलना कर रहे हैं की आपूर्ति करने की जरूरत है।
  • (तुलना) यह StrComp फ़ंक्शन का वैकल्पिक तर्क है। यह तब सहायक होता है जब हम केस की संवेदनशील तुलना करना चाहते हैं। उदाहरण के लिए, इस तर्क में, "एक्सेल" "एक्सेल" के बराबर नहीं है क्योंकि ये दोनों शब्द संवेदनशील हैं।

हम यहां तीन मूल्यों की आपूर्ति कर सकते हैं।

  • शून्य (0) के लिए " बाइनरी तुलना, " अर्थात, "एक्सेल," "एक्सेल" के बराबर नहीं है। मामले की संवेदनशील तुलना के लिए, हम 0 की आपूर्ति कर सकते हैं।
  • " पाठ तुलना " के लिए एक (1) , यानी, "एक्सेल," "एक्सेल" के बराबर है। यह एक गैर-मामला संवेदनशील तुलना है।
  • दो (2) केवल डेटाबेस तुलना के लिए।

"StrComp" फ़ंक्शन के परिणाम TRUE या FALSE को डिफॉल्ट नहीं करते लेकिन अलग-अलग होते हैं। नीचे "StrComp" फ़ंक्शन के विभिन्न परिणाम हैं।

  • हम "0" परिणाम के रूप में अगर आपूर्ति तार मिलान कर रहे हैं मिल जाएगा ।
  • यदि आपूर्ति की गई तारें मेल नहीं खा रही हैं, तो हम "1" प्राप्त करेंगे , और संख्यात्मक मिलान के मामले में, हमें 1 प्राप्त होगा यदि स्ट्रिंग 1 स्ट्रिंग 2 से अधिक है।
  • अगर स्ट्रिंग 1 नंबर 2 नंबर से कम है तो हम "-1" प्राप्त करेंगे ।

VBA में स्ट्रिंग तुलना कैसे करें?

उदाहरण 1

हम "से मेल खाएगी बैंगलोर स्ट्रिंग के खिलाफ" " बेंगलुरु ।"

सबसे पहले, दो स्ट्रिंग मानों को संग्रहीत करने के लिए दो VBA चर को स्ट्रिंग घोषित करें।

कोड:

उप String_Comparison_Example1 () Dim Value1 स्ट्रिंग के रूप में मंद मान 2 स्ट्रिंग स्ट्रिंग उप के रूप में

इन दो चर के लिए, दो स्ट्रिंग मान संग्रहीत करें।

कोड:

उप String_Comparison_Example1 () मान मान 1 स्ट्रिंग मान मान 2 के रूप में स्ट्रिंग मान 1 = "बैंगलोर" मान 2 = "बंगाली" अंत उप

अब " StrComp " फ़ंक्शन के परिणाम को संग्रहीत करने के लिए एक और चर घोषित करें ।

कोड:

उप String_Comparison_Example1 () मान मान 1 स्ट्रिंग मान मान 2 के रूप में स्ट्रिंग मान 1 = "बंगलौर" मान 2 = "बंगाली" मंद अंतिम रूप में स्ट्रिंग अंत उप

इस चर के लिए, "StrComp" फ़ंक्शन खोलें।

कोड:

Sub String_Comparison_Example1() Dim Value1 As String Dim Value2 As String Value1 = "Bangalore" Value2 = "BANGALORE" Dim FinalResult As String FinalResult = StrComp( End Sub

For “String1” & “String2” we have already assigned values through variables, so enter variable names, respectively.

Code:

Sub String_Comparison_Example1() Dim Value1 As String Dim Value2 As String Value1 = "Bangalore" Value2 = "BANGALORE" Dim FinalResult As String FinalResult = StrComp(Value1, Value2, End Sub

The last part of the function is “Compare” for this choice “vbTextCompare.”

Code:

Sub String_Comparison_Example1() Dim Value1 As String Dim Value2 As String Value1 = "Bangalore" Value2 = "BANGALORE" Dim FinalResult As String FinalResult = StrComp(Value1, Value2, vbTextCompare) End Sub

Now show the “Final Result” variable in the message box in VBA.

Code:

Sub String_Comparison_Example1() Dim Value1 As String Dim Value2 As String Value1 = "Bangalore" Value2 = "BANGALORE" Dim FinalResult As String FinalResult = StrComp(Value1, Value2, vbTextCompare) MsgBox FinalResult End Sub

Ok, let’s run the code and see the result.

Output:

Since both the strings “Bangalore” and “BANGALORE” are the same, we got the result as 0, i.e., matching. Both the values are case sensitive since we have supplied the argument as “vbTextCompare” it has ignored case sensitive match and matched only values, so both the values are the same, and the result is 0, i.e., TRUE.

Code:

Sub String_Comparison_Example1() Dim Value1 As String Dim Value2 As String Value1 = "Bangalore" Value2 = "BANGALORE" Dim FinalResult As String FinalResult = StrComp(Value1, Value2, vbTextCompare) MsgBox FinalResult End Sub

Example #2

For the same code, we will change the compare method from “vbTextCompare” to “vbBinaryCompare.”

Code:

Sub String_Comparison_Example2() Dim Value1 As String Dim Value2 As String Value1 = "Bangalore" Value2 = "BANGALORE" Dim FinalResult As String FinalResult = StrComp(Value1, Value2, vbBinaryCompare) MsgBox FinalResult End Sub

Now run the code and see the result.

Output:

Even though both the strings are the same, we got the result as 1, i.e., Not Matching because we have applied the compare method as “vbBinaryCompare,” which compares two values as case sensitive.

Example #3

Now we will see how to compare numerical values. For the same code, we will assign different values.

Code:

Sub String_Comparison_Example3() Dim Value1 As String Dim Value2 As String Value1 = 500 Value2 = 500 Dim FinalResult As String FinalResult = StrComp(Value1, Value2, vbBinaryCompare) MsgBox FinalResult End Sub

Both the values are 500, and we will get 0 as a result because both the values are matched.

Output:

Now I will change the Value1 number from 500 to 100.

Code:

Sub String_Comparison_Example3() Dim Value1 As String Dim Value2 As String Value1 = 1000 Value2 = 500 Dim FinalResult As String FinalResult = StrComp(Value1, Value2, vbBinaryCompare) MsgBox FinalResult End Sub

Run the code and see the result.

Output:

We know Value1 & Value2 aren’t the same, but the result is -1 instead of 1 because for numerical comparison when the String 1 value is greater than String 2, we will get this -1.

Code:

Sub String_Comparison_Example3() Dim Value1 As String Dim Value2 As String Value1 = 1000 Value2 = 500 Dim FinalResult As String FinalResult = StrComp(Value1, Value2, vbBinaryCompare) MsgBox FinalResult End Sub

Now I will reverse the values.

Code:

Sub String_Comparison_Example3() Dim Value1 As String Dim Value2 As String Value1 = 500 Value2 = 1000 Dim FinalResult As String FinalResult = StrComp(Value1, Value2, vbBinaryCompare) MsgBox FinalResult End Sub

Run the code and see the result.

Output:

This is not special. If not match, we will get 1 only.

Things to Remember here

  • (Compare) argument of “StrComp” is optional, but in case of case sensitive match, we can utilize this, and the option is “vbBinaryCompare.”
  • The result of numerical values is slightly different in case String 1 is greater than string 2, and the result will be -1.
  • Results are 0 if matched and 1 if not matched.

Recommended Articles

यह VBA स्ट्रिंग तुलना के लिए एक मार्गदर्शक रहा है। यहां हम चर्चा करते हैं कि एक्सेल VBA में StrComp फ़ंक्शन का उपयोग करके दो स्ट्रिंग मानों की तुलना उदाहरणों के साथ कैसे करें और एक एक्सेल टेम्पलेट डाउनलोड करें। एक्सेल VBA से संबंधित अन्य लेखों पर भी आपकी नज़र हो सकती है -

  • VBA स्ट्रिंग फ़ंक्शंस के लिए गाइड
  • VBA स्प्लिट स्ट्रिंग ऐरे में
  • VBA सबस्ट्रिंग के तरीके
  • VBA पाठ

दिलचस्प लेख...