Left - Right - Mid Functions

Posted on 2000-02-10
Medium Priority
Last Modified: 2010-05-02
I am having a lot of trouble
understanding the Left - Right and Mid functions and how they work. If i gave you an example of one could you break it down and explain to me what each piece of code is doing?

Lets say that FirstString = < and SecondString = > Lets say TheWholeString = <L>

We will use the Mid Function As the example.

Text1.Text = Mid$(TheWholeString,_ InStr(TheWholeString,_
FirstString) + Len(FirstString),_ InStr(TheWholeString,_
SecondString) - (Len(FirstString) +_
InStr(TheWholeString, FirstString)))

I know after running this code, text1.text would = L
but i cant figure out how it does this. Can you explain
to me what each piece of code in the brackets is doing.
Question by:poorboy
  • 2

Expert Comment

ID: 2509606
Where did you come up with this?  Okay, let's break it down.  You have the Mid function with parameters a, b, c. thus Mid(a, b, c).

a is the string named TheWholeString or <L>

b is InStr(TheWholeString, FirstString) + Len(FirstString)  OR

    InStr(<L>, <) equals 1 since the character first appears in the first position of the target string <L>.  If the target string had been &&<<<L>> then the value of InStr(<L>, <) would be 3.

   Len(<) equals 1 or the length of the target string.

   Therefore b = 1 + 1 = 2

c is InStr(TheWholeString, SecondString) - (Len(FirstString) +                  InStr(TheWholeString, FirstString)))

   InStr(<L>, >) = 3

   Len(<) = 1

   InStr(<L>, <) = 1

So c = 3 - (1 + 1) = 1

Now back to our original formula

Mid(<L>, 2, 1) means start at position number 2 in the string <L> and take 1 charachter.  Thus the answer is L.

More examples:   Mid("Hello", 2, 3) = ell

                            Mid("Good Morning", 3, 6) = od Mor

                            InStr("Hello", l) = 3

                            InStr("Hello", o) = 5

                            Len("Good Morning") = 12

Hope this helps.

LVL 14

Accepted Solution

mcrider earned 400 total points
ID: 2509663
You need to look at the function in  algebraic terms... Always resolve things in parenthesis to get to the lowest absolute, which in this case is the Mid$ function:

Mid$ returns a substring of a string.  There are 3 arguments that are passed to Mid$:

      mid$(String_to_use, Start_of_substring, Length_of_substring)

Here is an algebraic breakdown of the formula. Paste the following into VB so it will make sense.  It will not show up properly here...

Dim FirstString, SecondString, TheWholeString
FirstString = "<"
SecondString = ">"
TheWholeString = "<L>"

text1.Text = Mid$(TheWholeString, InStr(TheWholeString, FirstString) + Len(FirstString), InStr(TheWholeString, SecondString) - (Len(FirstString) + InStr(TheWholeString, FirstString)))
'                                 \                                /   \              /  \                                 /    \              /   \                               /
'                                  \           returns            /     \   returns  /    \            returns            /      \   returns  /     \           returns           /
'                                                 1                  +         1       ,                  3                  -  (       1        +                 1                 )
'  Plug the numbers into the mid$ function, so it looks like this:
'       text1.Text = Mid$(TheWholeString, 1 + 1, 3 - (1 + 1))
'  Resolve the numbers again so it looks like this:
'       text1.Text = Mid$(TheWholeString, 2, 3 - 2)
'  Resolve the numbers again so it looks like this:
'       text1.Text = Mid$(TheWholeString, 2, 1)

Hope this helps!


Author Comment

ID: 2509765
I just made the formula up in case
you were wondering. Thanks for the help.
LVL 14

Expert Comment

ID: 2510128
You made the formula up?? Ok...

Thanks for the points! Glad I could help!


Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question