Mid Function Implementation Without Using String Functions

September 23, 2010

Few days back some of my reader asked me about mid function. He wanted to know how it works and can we implement a function similar to mid.

Yes ...we do. I tried to implement the mid function here. What i have done is
1.transfer the string to Array
2.then take the size of the array
3.k is the counter to check if it is equal to strtcut that is which position i need to cut and length is the variable to understand till how much i need to cut.

Sub Macro1()

Call midexplained("Animesh", 2, 2)
'same as mid("Animesh",2,2)
End Sub

Function midexplained(inputq, strtcut, length)
Dim bText() As Byte
Dim j As Integer
j = 0
Dim k As Integer
k = 0
Dim a(999)
Dim n As String
Dim MyString As String: MyString = inputq
bText = MyString
For i = 0 To UBound(bText) - 1
If (bText(i) = 0) Then
a(j) = Chr(bText(i))
j = j + 1
End If
For k = 0 To UBound(a) - 1
If a(k) = "" Then
ElseIf k + 1 >= strtcut And k < ((strtcut + length) - 1) Then
n = n & a(k)
End If

MsgBox "value of n is " & trim(n)
End Function
The output for both the variable is same.Please let me know if this conversion is ok. Also please let me know if there is any modification is required.

You Might Also Like