Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default finding the "(" in text

I have cells that contain a brief sentence

in the sentence I have information that is surrounded by ( data )

how do I tell excel to copy the data between the ( copy this data )

Thank you



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default finding the "(" in text

You can get the text within (but not including) the parentheses with a
formula like:


=MID(A1,FIND("(",A1,1)+1,FIND(")",A1,1)-FIND("(",A1,1)-1)

where A1 has the text.

Or, you can use VBA:

Function GetDataText(R As Range) As String
Dim S As String
Dim N As Long
Dim M As Long

S = R.Text
M = InStr(1, S, "(")
If M = 0 Then
Exit Function
End If
N = InStr(M, S, ")")
If N = 0 Then
Exit Function
End If
GetDataText = Mid(S, M + 1, N - M - 1)
End Function

and call this either from other VBA or from a worksheet cell with
=GetDataText(A1)

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Mon, 17 Aug 2009 12:03:03 -0700, dstiefe
wrote:

I have cells that contain a brief sentence

in the sentence I have information that is surrounded by ( data )

how do I tell excel to copy the data between the ( copy this data )

Thank you


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default finding the "(" in text

can you use the first formul within vba code?

"Chip Pearson" wrote:

You can get the text within (but not including) the parentheses with a
formula like:


=MID(A1,FIND("(",A1,1)+1,FIND(")",A1,1)-FIND("(",A1,1)-1)

where A1 has the text.

Or, you can use VBA:

Function GetDataText(R As Range) As String
Dim S As String
Dim N As Long
Dim M As Long

S = R.Text
M = InStr(1, S, "(")
If M = 0 Then
Exit Function
End If
N = InStr(M, S, ")")
If N = 0 Then
Exit Function
End If
GetDataText = Mid(S, M + 1, N - M - 1)
End Function

and call this either from other VBA or from a worksheet cell with
=GetDataText(A1)

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Mon, 17 Aug 2009 12:03:03 -0700, dstiefe
wrote:

I have cells that contain a brief sentence

in the sentence I have information that is surrounded by ( data )

how do I tell excel to copy the data between the ( copy this data )

Thank you



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default finding the "(" in text

You can't really use a formula within VBA code. The formula I posted
can be put into the cell in which you want the text within the
parentheses display. For example, if cell A1 has

This is some (data text) stuff

and B1 has the formula

=MID(A1,FIND("(",A1,1)+1,FIND(")",A1,1)-FIND("(",A1,1)-1)

cell B1 will display

data text

If you don't want to use a formula but would rather use a function
written in VBA, press ALT F11 to open the VBA editor, go to the Insert
menu, and choose Module. In that module, paste the VBA code I
provided. Close the VBA editor to return to Excel.

Now, you can enter

=GetDataText(A1)

in some cell and that cell will display the text within the
parentheses from cell A1.

The formula approach and the VBA approach have the same end result --
the text within the parentheses. Since you posted in the Programming
newsgroup, the presumption is that you want a VBA solution. That's
what I provided. However, since the same thing can be had with a
simple formula without using VBA, I provided that as an alternative.
Pick either the formula solution or the VBA solution. The result is
basically the same.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)


On Mon, 17 Aug 2009 14:40:01 -0700, dstiefe
wrote:

can you use the first formul within vba code?

"Chip Pearson" wrote:

You can get the text within (but not including) the parentheses with a
formula like:


=MID(A1,FIND("(",A1,1)+1,FIND(")",A1,1)-FIND("(",A1,1)-1)

where A1 has the text.

Or, you can use VBA:

Function GetDataText(R As Range) As String
Dim S As String
Dim N As Long
Dim M As Long

S = R.Text
M = InStr(1, S, "(")
If M = 0 Then
Exit Function
End If
N = InStr(M, S, ")")
If N = 0 Then
Exit Function
End If
GetDataText = Mid(S, M + 1, N - M - 1)
End Function

and call this either from other VBA or from a worksheet cell with
=GetDataText(A1)

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Mon, 17 Aug 2009 12:03:03 -0700, dstiefe
wrote:

I have cells that contain a brief sentence

in the sentence I have information that is surrounded by ( data )

how do I tell excel to copy the data between the ( copy this data )

Thank you



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 73
Default finding the "(" in text


last question...

i'm alwasy uncertain how to use functions

how do I pass the string to the function in my vba code

and how do I get the output?

make sense
"Chip Pearson" wrote:

You can get the text within (but not including) the parentheses with a
formula like:


=MID(A1,FIND("(",A1,1)+1,FIND(")",A1,1)-FIND("(",A1,1)-1)

where A1 has the text.

Or, you can use VBA:

Function GetDataText(R As Range) As String
Dim S As String
Dim N As Long
Dim M As Long

S = R.Text
M = InStr(1, S, "(")
If M = 0 Then
Exit Function
End If
N = InStr(M, S, ")")
If N = 0 Then
Exit Function
End If
GetDataText = Mid(S, M + 1, N - M - 1)
End Function

and call this either from other VBA or from a worksheet cell with
=GetDataText(A1)

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)



On Mon, 17 Aug 2009 12:03:03 -0700, dstiefe
wrote:

I have cells that contain a brief sentence

in the sentence I have information that is surrounded by ( data )

how do I tell excel to copy the data between the ( copy this data )

Thank you





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,651
Default finding the "(" in text

On Mon, 17 Aug 2009 12:03:03 -0700, dstiefe
wrote:

I have cells that contain a brief sentence

in the sentence I have information that is surrounded by ( data )

how do I tell excel to copy the data between the ( copy this data )

Thank you



Since this is a programming group, here is a User Defined Function.

To enter this User Defined Function (UDF), <alt-F11 opens the Visual Basic
Editor.
Ensure your project is highlighted in the Project Explorer window.
Then, from the top menu, select Insert/Module and
paste the code below into the window that opens.

To use this User Defined Function (UDF), enter a formula like

=BetweenParenth(A1)

in some cell where A1 contains the string you wish to parse.

===================================
Option Explicit
Function BetweenParenth(s As String) As String
Dim re As Object
Set re = CreateObject("vbscript.regexp")
re.Global = True
re.Pattern = "^[^(]*\(([^)]+).*$"
If re.test(s) = True Then
BetweenParenth = re.Replace(s, "$1")
End If
End Function
============================
--ron
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Text "comparison" operator for "contains" used in an "IF" Function Pawaso Excel Worksheet Functions 4 April 4th 23 11:35 AM
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
Insert "-" in text "1234567890" to have a output like this"123-456-7890" Alwyn Excel Discussion (Misc queries) 3 October 25th 05 11:36 PM
problems finding "~" in text rachel Excel Programming 4 September 8th 05 07:47 AM
How do I split "A1B2" into "A1" and "B2" using text to column fun. Jennifer Excel Programming 1 February 2nd 05 10:01 PM


All times are GMT +1. The time now is 03:27 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"