Adding Paranthesis Around Multiplied Numbers in a Textbox

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Give this function a try. Simply pass it the text from the TextBox into the function and reassign the text it returns back to the TextBox.
Code:
Function AddParens(ByVal Txt As String) As String
  Dim X As Long, Arr() As String
  Txt = Replace(Replace(Replace(Replace(Replace(Txt, " ", ""), "(", ""), ")", ""), "+", " + "), "-", " - ")
  Arr = Split(Txt)
  For X = 0 To UBound(Arr)
    If Arr(X) Like "*[*/]*" Then Arr(X) = "(" & Arr(X) & ")"
  Next
  AddParens = Replace(Replace(Join(Arr), "*", " * "), "/", " / ")
End Function
 
Upvote 0
Thank you for the quick response on this! and for the help with this.

It works exactly as intended! I wish you all the best
 
Upvote 0
I forgot, one more thing is the possibility to count the functions in parentheses and insert the value to second TextBox
 
Upvote 0
...count the functions in parentheses...
What do you mean by the above? Do you want a count of the number of sets of parentheses (no matter how many numbers are being multiplied inside each set) or do you want the count of numbers that are being multiplied?
 
Upvote 0
Well for an example in the picture there are 4 sets of parentheses shown, so in the textbox it would show the number 4.
So I would like it to calculate the sets of parentheses and not the numbers inside the parentheses.

I'm sorry if this is still confusing, English is my second language, so if you don't get it I can explain more.
 
Upvote 0
Give this function a try... just pass in the contents of the first TextBox after my first function formatted it into the function below and assign its output to your second TextBox)...
Code:
Function GroupCount(Txt As String) As Long
  GroupCount = Len(Txt) - Len(Replace(Txt, "(", ""))
End Function
 
Upvote 0
Great! This is amazing!!! Thank you so much, that's exactly what I was thinking about, its like you read my mind and you did custom function with both codes.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top