Magic_Doctor
Board Regular
- Joined
- Mar 18, 2009
- Messages
- 56
This function is used to extract, from a chain where are distributed telephone numbers, a number following its position in the chain.
Chain example:
"Pitaluga has several telephone numbers: 06 66 99 55 78 & 294 54 20 44 & 099 925 301 & 06 300 200 11 & 095 233 144 & 600 25 52 30"
A phone number is a set of numbers usually grouped by 2 or 3.
The only requirements of the chain are that the groups of numbers must be separated by a space and that the numbers (sets of groups of numbers) are separated by anything but a space. Which is my logical faith.
The function works almost perfectly.
In the chain I give as an example, there are 6 phone numbers (we could have put a lot more).
If I want to extract only the mobile numbers, I will write (assuming that the string in question is in cell A25) in the cell (for example) A1:
= ExtractTelPortable ([A25]; 1; ".")
I "pull" all the way down to A6:
= ExtractTelPortable ([A25]; 6; ".")
In order to recover all the number of mobile.
You follow me ?
Curiously, the loop does not start with 1 but by 2 and the 6th number is the same as the 5th (which is logical considering the offset at the start of a number).
I put, at the end of the numbers of telephones returned by the function the value of n (see macro) to better show the problem.
Here, n should start with 1 and end with 6.
In the loop, n starts with 2 and ends with 7 ...
Could someone explain to me why to help me solve the problem?
Thank you in advance.
Chain example:
"Pitaluga has several telephone numbers: 06 66 99 55 78 & 294 54 20 44 & 099 925 301 & 06 300 200 11 & 095 233 144 & 600 25 52 30"
A phone number is a set of numbers usually grouped by 2 or 3.
The only requirements of the chain are that the groups of numbers must be separated by a space and that the numbers (sets of groups of numbers) are separated by anything but a space. Which is my logical faith.
Code:
Function ExtractTelPortable(maChaine As String, x As Byte, sep As String, Optional tel As Boolean = True) As String
'Extraie des séries de Nº d'une chaîne
'- maChaine : the chain containing series of Nº
'- x : position of the number we are looking for in the chain
'- sep : separator that we want to place between the groups of numbers of the same Nº
'- tel : default "True" -> cellphones. If "False" -> phones
Dim i As Integer, j As Integer, trouve As String, cadena As String
For n = 1 To x
For i = 1 To Len(maChaine)
trouve = Mid(maChaine, i, 1)
If IsNumeric(trouve) Then
maChaine = Right(maChaine, Len(maChaine) - i + 1) 'we remove everything that is not numeric left of the chain
For j = 1 To Len(maChaine)
trouve = Mid(maChaine, j, 1)
If trouve <> " " And IsNumeric(trouve) = False Then
cadena = Trim(Left(maChaine, j - 1)) 'we only get the first set of digits (left) of "maChaine": the one that interests us
maChaine = Mid(maChaine, Len(cadena) + 1, Len(maChaine) - Len(cadena)) '"maChaine" is cut off from its first series of figures
Exit For
End If
Next
Exit For
End If
Next
Next
If tel Then 'we only want to recover the number of cellphones
ExtractTelPortable = IIf(Left(cadena, 1) <> 0, "", Replace(cadena, " ", sep)) & " | n = " & n
Else 'we only want to recover the number of phones
ExtractTelPortable = IIf(Left(cadena, 1) = 0, "", Replace(cadena, " ", sep)) & " | n = " & n
End If
End Function
The function works almost perfectly.
In the chain I give as an example, there are 6 phone numbers (we could have put a lot more).
If I want to extract only the mobile numbers, I will write (assuming that the string in question is in cell A25) in the cell (for example) A1:
= ExtractTelPortable ([A25]; 1; ".")
I "pull" all the way down to A6:
= ExtractTelPortable ([A25]; 6; ".")
In order to recover all the number of mobile.
You follow me ?
Curiously, the loop does not start with 1 but by 2 and the 6th number is the same as the 5th (which is logical considering the offset at the start of a number).
I put, at the end of the numbers of telephones returned by the function the value of n (see macro) to better show the problem.
Here, n should start with 1 and end with 6.
In the loop, n starts with 2 and ends with 7 ...
Could someone explain to me why to help me solve the problem?
Thank you in advance.