I am writing a macro that takes a list of names that are in an LDAP format converts them to First, Last (region). For those of you that do not know what LDAP looks like, it is below:
CN=John Smith (region),OU=Legal,DC=example,DC=comand
In Excel VBA, I do not appear to be able to use string.substring(start, end). A search on Google seems to reveal that Mid(string, start, end) is the best option. The problem is this: in Mid, the integer for end is the distance from start, not the actual index location of the character. This means that different name sizes will have different ending locations and I cannot use index of ")" to find the end of the region. Since all of the names start with CN=, I can find the end of the first substring properly, but I cannot find ")" properly because names are different lengths.
I have some code below:
mgrSub1 = Mid(mgrVal, InStr(1, mgrVal, "=") + 1, InStr(1, mgrVal, "\") - 4)
mgrSub2 = Mid(mgrVal, InStr(1, mgrVal, ","), InStr(1, mgrVal, ")") - 10)
manager = mgrSub1 & mgrSub2
Does anyone know of a way to actually use a set end point instead of an end point that is so many values away from the start?
The first/last name bit is easy if you start with this:
I'm not sure I got your question right, but here is my implementation of (hopefully) what you want:
It illustrates the use of
Split
andMid
functions.It is a quick and dirty implementation serving illustrative purposes only. To use it in real code you need to add several checks (e.g. that the
kv
andparts
collections contain at least two elements).UPD: To cover two possible formats of the CN field, namely
"last\, first (region)"
and"first last (region)"
and make things a little less messy I would take the regular expressions approach.I would use InStr to find the position of the three characters that separate the values and then use Left/Right on them.
This is what I hacked together real quick:
Then concatenate them to get the string output you want.
This is vba.. no string.substring ;)
this is more like VB 6 (or any one below).. so you are stuck with mid, instr, len (to get the total len of a string).. I think you missed len to get the total of chars in a string? If you need some clarification just post a comment.
edit:
Another quick hack..
Clearly your problem is that since you need a diference for the second parameter, you should always do some math for it...