VBA INSTR FUNCTION

INSTR

With the help of INSTR Function in excel vba we find out the position of a substring within a string from left side. If substring found then the function returns integer value, if not found then function returs the value 0.

 

INSTR Function की मदद से हम Excel VBA में किसी string में से किसी substring का position पता करते हैं left side से. अगर substring पाया गया तो function उस substring का position number में return करता है, नहीं तो फंक्शन 0 return करता है.

Syntax:-
Instr( [start], string, substring, [compare] )

Start
It is optional. It refers from where the search should begin. If it is omitted then function search from position 1st from left to right.

यह ऑप्शनअल है, यह बताता है कि खोज कहाँ से शुरू होनी चाहिए। यदि हम इसे छोड़ते है तो function 1st position से खोजता हैं.

String
The substring is to be searched inside this string.

substring को इस string के अंदर खोजा जाना है.

 

Substring
Substring which has to be searched inside the string.

सुब्स्ट्रिंग जिसे string के अंदर खोजा जाना है।

Example 1:-

Sub INSTR1()
MsgBox InStr(“COMPUTER”, “M”)
End Sub

Here message box will display 3, because position of M, in COMPUTER is 3rd

यहां message box 3 प्रदर्शित करेगा, क्योंकि कंप्यूटर में M की position 3rd है.


Example 2:-

Sub INSTR2()
MsgBox InStr(“PEN ON THE TABLE”, “T”)
End Sub

Here message box will display 8, because position of T, in “PEN ON THE TABLE” is 8th.

यहां message box 8 प्रदर्शित करेगा, क्योंकि “PEN ON THE TABLE” में T की position 8th है.


Example 3:-

But if we use this macro with start

Sub INSTR3()
MsgBox InStr(9, “PEN ON THE TABLE”, “T”)
End Sub

Here message box will display 12, because here insrt will start search the position of T from the 9th position, but the searched position will be counted from started.

 

यहां message box 12 प्रदर्शित करेगा, क्योंकि INSTR यहाँ T का पोजीशन नवें position से search करना start करेगा, लेकिन खोजा जाने वाला position left side से count होंगे.


Example 4:-

In below macro N is being searched in the value which is typed in A1 cell and message will be displayed 3, beacause N is located at 3rd position.

निचे के Macro में N को cell A1 के value में खोजा जा रहा है और message ३ display होगा, क्योंकि N 3rd position पर है.

Sub INSTR4()
MsgBox InStr(Range(“A1”).Value, “N”)
End Sub


Example 5:-

Here we have to extract first name from a name in A5 cell and put first name in B5 cell with vba.

In below macro I sp variable stores the position of space in A5 cell’s value. Thus sp hold the value 5, then I used left function to extract first from the John Marshal, sp value 5-1=4, picked 4 character from left side.

हमें यहाँ A5 cell में मौजूद वैल्यू से first name नाम निकालना है और first name को B5 cell में रखना है macro से. निचे के macro में मैंने sp variable A5 cell के value से space का position को  स्टोर करता है. इस प्रकार sp 5 store करता है, तब मैंने left function का उपयोग John Marshal से first name निकालने के लिए किया है. sp वैल्यू 5-1=4, left side से 4 character उठाया है और उसे B5 cell में paste किया है.

Sub INSTR5()
SP = InStr(Range(“A5″).Value, ” “)
Range(“B5”).Value = Left(Range(“A5”).Value, SP – 1)
End Sub

 

Download Excel File