Adding Paranthesis Around Multiplied Numbers in a Textbox

JarekM

Board Regular
Joined
Nov 13, 2018
Messages
86
Hi,
I need help with creating a code where in equations that I am making, parenthesis would go around the multiplication, just like in the picture below.

Here is the photo:
https://imgur.com/a/kJ1YAMM


 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
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,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

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