Replace or Substitute for COMBIN function
I have lots of lines in this format to convert into COMBIN function
If you wanted to call this function in a loop, one wouldn't want to
initialize Re with each call.
Perhaps make Re a public variable as in this example. You may want to
remove the Evaluate line as well.
Option Explicit
Public Re As RegExp
Function ToCombin(s As String, Optional Eval As Boolean = False)
If Re Is Nothing Then Set Re = New RegExp
Re.Global = True
Re.Pattern = "(\d+)c(\d+)"
ToCombin = Replace(s, Chr(120), Chr(42)) 'x - *
ToCombin = Re.Replace(ToCombin, "Combin($1,$2)")
If Eval Then ToCombin = Evaluate(ToCombin)
End Function
Sub TestIt()
Const s As String = "(45c6 / (6c5 x (39c1 - 37c1)))"
'What you asked...
Debug.Print ToCombin(s)
'If you want to evaluate the equation
Debug.Print ToCombin(s, True) 'True to return number
End Sub
Returns:
(Combin(45,6) / (Combin(6,5) * (Combin(39,1) - Combin(37,1))))
678755
--
Dana DeLouis
Win XP & Office 2003
"Dana DeLouis" wrote in message
...
xcy should become (COMBIN(x,y))
Just an additional idea. If you would like to try Regular Expressions,
then set a vba library reference to:
'// Microsoft VBScript Regular Expressions 5.5
This returns the following:
(45c6 / (6c5 x (39c1 - 37c1)))
(Combin(45,6) / (Combin(6,5) * (Combin(39,1) - Combin(37,1))))
678755
Sub Demo()
'// Your string variable:
Dim s As String
s = "(45c6 / (6c5 x (39c1 - 37c1)))"
Debug.Print s
'// Our Program:
Dim re As New RegExp
Const fx As String = "Combin($1,$2)"
With re
.Global = True
.IgnoreCase = True
.MultiLine = True
.Pattern = "(\d+)c(\d+)"
s = Replace(s, "x", "*")
s = .Replace(s, fx)
End With
Debug.Print s
Debug.Print Evaluate(s)
End Sub
HTH. :)
--
Dana DeLouis
Win XP & Office 2003
"Jaja" wrote in message
...
Sorry about the parenthesis disfigure. I have worked it out. Also I have
simplified it for each format as written after OR.
The original format
(45c6 / (6c5 x (39c1 - 37c1)))
OR
45c6
( _________________________ )
(6c5 X (39c1 - 37c1))
The above format should become like this
((COMBIN(45,6))/((COMBIN(6,5))*((COMBIN(39,1))-(COMBIN(37,1)))))
OR
(COMBIN(45,6))
( _______________________________________________ )
((COMBIN(6,5)) X ((COMBIN(39,1)) - (COMBIN(37,1))))
Easiest way to convert is just enclose it in bracket. For example
xcy should become (COMBIN(x,y))
x and y is enclosed in a bracket with COMBIN outside enclosed again with
outer bracket.
The rest of the bracket in the original format will stay intact.
|