String Handling Functions in VBA in Hindi
Makhanlal Chaturvedi University / BCA / VBA programming
String Handling Functions in VBA in Hindi
String Handling Functions in VBA in Hindi
Introduction to String Handling in VBA
Visual Basic for Applications (VBA) में string handling functions का उपयोग किसी भी text को manipulate, analyze या modify करने के लिए किया जाता है। जब हम Excel VBA में किसी user के input, cells में मौजूद डेटा या किसी variable के text को control या analyze करना चाहते हैं, तब हमें string functions का इस्तेमाल करना पड़ता है। यह functions Excel VBA में automation को अधिक smart और efficient बनाते हैं।
Commonly Used String Functions
VBA में कुछ प्रमुख string functions होते हैं जो अक्सर इस्तेमाल किए जाते हैं:
- Left() – किसी string की शुरुआत से characters निकालने के लिए
- Right() – किसी string के आखिरी characters निकालने के लिए
- Mid() – string के बीच से characters निकालने के लिए
- Len() – किसी string की लंबाई (characters की संख्या) पता करने के लिए
- Instr() – किसी character या substring की position खोजने के लिए
- LCase() और UCase() – lowercase या uppercase में बदलने के लिए
Using Left, Right, Mid Functions in VBA in Hindi
Left() Function
Left() function किसी string के शुरू से निर्धारित संख्या में characters को निकालने के लिए प्रयोग होता है।
Syntax: Left(string, length)
जहाँ string वह text होता है जिससे आप characters निकालना चाहते हैं और length वह संख्या होती है जितने characters चाहिए।
Example:
Sub LeftFunctionDemo()
Dim msg As String
msg = "Excel VBA"
MsgBox Left(msg, 5) 'Output: Excel
End Sub
Right() Function
Right() function किसी string के अंत से characters को extract करता है।
Syntax: Right(string, length)
Example:
Sub RightFunctionDemo()
Dim msg As String
msg = "Excel VBA"
MsgBox Right(msg, 3) 'Output: VBA
End Sub
Mid() Function
Mid() function string के बीच से characters निकालता है, शुरुआत की position और कितने characters चाहिए यह निर्धारित करना होता है।
Syntax: Mid(string, start, length)
Example:
Sub MidFunctionDemo()
Dim msg As String
msg = "Excel VBA Programming"
MsgBox Mid(msg, 7, 3) 'Output: VBA
End Sub
Using Len Function to Get String Length in Hindi
Len() function किसी भी string में कुल कितने characters हैं यह बताता है। इसमें spaces भी count होते हैं।
Syntax: Len(string)
Example:
Sub LenFunctionDemo()
Dim text As String
text = "Excel VBA"
MsgBox Len(text) 'Output: 9
End Sub
उपरोक्त उदाहरण में "Excel VBA" में 9 characters हैं (space भी शामिल है)।
Combining String Functions for Effective String Handling in Hindi
जब हम VBA में multiple string functions को एक साथ उपयोग करते हैं, तो हम बहुत ही शक्तिशाली और flexible string processing कर सकते हैं। नीचे कुछ महत्वपूर्ण उदाहरण दिए गए हैं जहाँ हमने string functions को combine किया है।
Example 1: Extract First Name from Full Name
मान लीजिए हमारे पास एक नाम है – "Rahul Sharma", और हमें सिर्फ "Rahul" निकालना है।
Sub ExtractFirstName()
Dim fullName As String
Dim spacePos As Integer
fullName = "Rahul Sharma"
spacePos = InStr(fullName, " ")
MsgBox Left(fullName, spacePos - 1) 'Output: Rahul
End Sub
यहाँ हमने InStr function से space की position निकाली और फिर Left() से उतने characters लिए।
Example 2: Extract Domain from Email
अगर आपके पास एक email है – "student@example.com", और आपको "example.com" निकालना है।
Sub ExtractDomain()
Dim email As String
Dim atPos As Integer
email = "student@example.com"
atPos = InStr(email, "@")
MsgBox Mid(email, atPos + 1) 'Output: example.com
End Sub
String Cleaning: Removing Unwanted Spaces
VBA में Trim() function का प्रयोग leading और trailing spaces को हटाने के लिए किया जाता है।
Sub TrimExample()
Dim messy As String
messy = " Hello VBA "
MsgBox Trim(messy) 'Output: Hello VBA
End Sub
Using UCase and LCase
- UCase(string) – पूरे string को upper case में बदल देता है।
- LCase(string) – पूरे string को lower case में बदल देता है।
Sub CaseExample()
Dim text As String
text = "Excel VBA"
MsgBox UCase(text) 'Output: EXCEL VBA
MsgBox LCase(text) 'Output: excel vba
End Sub
Table: Important String Functions in VBA
| Function | Purpose |
|---|---|
| Left() | Start से characters निकालता है |
| Right() | End से characters निकालता है |
| Mid() | Middle से characters निकालता है |
| Len() | String की length निकालता है |
| Instr() | Substring की position बताता है |
| Trim() | Unwanted spaces हटाता है |
| UCase() | Upper Case में convert करता है |
| LCase() | Lower Case में convert करता है |
इन सभी string functions को सीखकर आप Excel VBA में text से जुड़ी लगभग हर problem को solve कर सकते हैं। इन functions को साथ में use करके complex string manipulation करना बहुत आसान हो जाता है, जिससे automation और reporting की quality काफी बढ़ जाती है।
FAQs
Len("Your Text")
Trim(" Your Text ")