mlthornton
New Member
- Joined
- Apr 24, 2022
- Messages
- 2
- Office Version
- 2021
- Platform
- Windows
I'm starting a UDF that will take an operation input that contains multiple inputs (not of Excel or VBA format), for example:
(A OR (B AND C) AND (A OR B))
For the longer ones, keeping the parenthesis quickly get hard to follow. My thinking is that you would put the string in above and the UDF would return:
(1A OR (2B AND C)2 AND (3A OR B)3)1
So that balancing parenthesis is easier. The general idea would be:
1. Find first ")"
2. Rename it as ")1" (first iteration, n = 1)
3. Now going right to left, find first "(" and rename it as "(1"
4. Delete both ")1" and "(1"
5. Repeat step 1, finding the new first ")" since the last set was deleted, and this time it will be ")2"
First does such a tool already exist? If not, here's where I am so far. Any thoughts here? Is there a way to force the For loop to search from right to left instead of left to right?
Function testing(UserInput As Range) As String
Dim Counter As Integer
Dim CurrentCharacter As String
Dim NewCounter As Integer
Dim NewString As String
For Counter = 1 To Len(UserInput)
NewCounter = 0
CurrentCharacter = Mid(UserInput, Counter, 1)
If CurrentCharacter = ")" Then
NewCounter = NewCounter + 1
NewCharacter = ")" & NewCounter
MsgBox NewCharacter
Exit For
End If
Next Counter
testing = UserInput.Value
End Function
(A OR (B AND C) AND (A OR B))
For the longer ones, keeping the parenthesis quickly get hard to follow. My thinking is that you would put the string in above and the UDF would return:
(1A OR (2B AND C)2 AND (3A OR B)3)1
So that balancing parenthesis is easier. The general idea would be:
1. Find first ")"
2. Rename it as ")1" (first iteration, n = 1)
3. Now going right to left, find first "(" and rename it as "(1"
4. Delete both ")1" and "(1"
5. Repeat step 1, finding the new first ")" since the last set was deleted, and this time it will be ")2"
First does such a tool already exist? If not, here's where I am so far. Any thoughts here? Is there a way to force the For loop to search from right to left instead of left to right?
Function testing(UserInput As Range) As String
Dim Counter As Integer
Dim CurrentCharacter As String
Dim NewCounter As Integer
Dim NewString As String
For Counter = 1 To Len(UserInput)
NewCounter = 0
CurrentCharacter = Mid(UserInput, Counter, 1)
If CurrentCharacter = ")" Then
NewCounter = NewCounter + 1
NewCharacter = ")" & NewCounter
MsgBox NewCharacter
Exit For
End If
Next Counter
testing = UserInput.Value
End Function