A Regular Expression can be used.
Code
Dim texto As String
Dim objCorresp As Object, objExpReg As Object
Set objExpReg = CreateObject("VBScript.RegExp")
'Expressão Regular
With objExpReg
.Pattern = "nome:[\s\S]+?(?=e-mail)"
.Global = True
.MultiLine = True
.IgnoreCase = True
End With
texto = "Bla bla bla bla bla NOME: JOSH IDNUMBER: 098766 E-MAIL: [email protected] bla bla bla."
Set objCorresp = objExpReg.Execute(texto)
If objCorresp.Count <> 0 Then
For Each c In objCorresp
Debug.Print Trim(c)
Next c
End If
Upshot
For the example of the code, the result is: NOME: JOSH IDNUMBER: 098766
For a string with multiple lines:
texto = "Bla bla bla bla bla NOME: JOSH IDNUMBER: 098766 E-MAIL: [email protected] bla bla bla." & vbNewLine & _
"Bla bla bla bla bla NOME: JOAO IDNUMBER: 098766 E-MAIL: [email protected] bla bla bla."
The result is:
NOME: JOSH IDNUMBER: 098766
NOME: JOAO IDNUMBER: 098766
Regular Expression
The following expression may be used: nome:[\s\S]+?(?=e-mail)
Where this expression captures the text that starts with nome:
and is followed by any character with quantifier Lazy [\s\S]+?
, before e-mail (?=e-mail)
And the demo can be seen in this link
Enable Regex in Excel
- Regex needs to be enabled, Enable the Developer mode
- In the 'Developer' tab, click 'Visual Basic' and the VBA window will open.
- Go to 'Tools' -> 'References...' and a window will open.
- Search for 'Microsoft Vbscript Regular Expressions 5.5', as in the image below. And enable this option.
Function Defined by the User
An UDF can be created, with the following code:
Function extrair_texto_entre(inicio As String, fim As String, texto) As String
Dim objCorresp As Object, objExpReg As Object
Set objExpReg = CreateObject("VBScript.RegExp")
'Expressão Regular
With objExpReg
.Pattern = inicio & "[\s\S]+?(?=" & fim & ")"
.Global = True
.MultiLine = True
.IgnoreCase = True
End With
Set objCorresp = objExpReg.Execute(texto)
If objCorresp.Count <> 0 Then
For Each c In objCorresp
extrair_texto_entre = Trim(c)
Next c
End If
End Function
Upshot
This function can be used in the Worksheet as follows:
=extrair_texto_entre("string de início"; "string de fim"; célula ou "string")
Where it can be used either by referencing a cell or by entering a String.
String
In it you use the function as follows:
=extrair_texto_entre("nome:";"e-mail";"Bla bla bla bla bla NOME: JOSH IDNUMBER: 098766 E-MAIL: [email protected] bla bla bla.")
And get the following result: NOME: JOSH IDNUMBER: 098766
Cell-powered
This way you insert the function into the spreadsheet as follows:
=extrair_texto_entre("nome:";"e-mail";B2)
And get the result:
NOME: JOSH IDNUMBER: 098766
Are you Aware this is a Portuguese-speaking forum? How are your data in the spreadsheet? Are they all in the same column? In principle, you could achieve the desired result with a combination of formulas from Excel itself, without having to resort to a Macro.
– VBobCat
You failed to notice that we Speak Portuguese. Please translate your question into English.
– Victor Stafusa