Alessandro Giraldi
New member
- Local time
- Today, 04:10
- Joined
- Feb 24, 2020
- Messages
- 15
Val(str) * Val(Mid(str, InStr(str, "(") + 1)) * Val(Mid(str, InStr(str, "(") + 1 + Len(CStr(Val(Mid(str, InStr(str, "(") + 1)))) + 1))
Codes are implementations of rules, and a rule will only be able to take into account what is known.i can have a different expression any time
Hi,When you say Via calculation do you mean calculating a printed circuit board Via. If so there are javascripts available. See link
You could build what is shown all the formulas are there
If you need help on building that, I can help. I do a lot of math focused databases.
Private pRegEx As Object
Public Property Get oRegEx(Optional Reset As Boolean) As Object
If (pRegEx Is Nothing) Then Set pRegEx = CreateObject("Vbscript.Regexp")
If Reset Then Set pRegEx = Nothing
Set oRegEx = pRegEx
End Property
Public Function RegExMatchCollection(ByVal SourceText As String, _
ByVal SearchPattern As String, _
Optional ByVal bIgnoreCase As Boolean = True, _
Optional ByVal bGlobal As Boolean = True, _
Optional ByVal bMultiLine As Boolean = True) As Object
With oRegEx
.Pattern = SearchPattern
.IgnoreCase = bIgnoreCase
.Global = bGlobal
.MultiLine = bMultiLine
Set RegExMatchCollection = .Execute(SourceText)
End With
End Function
Public Function RegExReplace(ByVal SourceText As String, _
ByVal SearchPattern As String, _
ByVal ReplaceText As String, _
Optional ByVal bIgnoreCase As Boolean = True, _
Optional ByVal bGlobal As Boolean = True, _
Optional ByVal bMultiLine As Boolean = True) As String
With oRegEx
.Pattern = SearchPattern
.IgnoreCase = bIgnoreCase
.Global = bGlobal
.MultiLine = bMultiLine
RegExReplace = .Replace(SourceText, ReplaceText)
End With
End Function
Sub test_textil()
Const cT1 = "2(2a2b2c)2a16b6c3(3c3b)"
'Const cT2 = "2(2A2B)"
Dim oMC As Object, oMC2 As Object
Dim oM As Object, oM2 As Object
Dim sF As String, lF As Long
Set oMC = RegExMatchCollection(cT1, "((\d+\((\d+[a-z])+\))|(\d+[a-z])+)")
For Each oM In oMC
'Debug.Print oM.Value, oM.FirstIndex + 1, oM.Length
sF = oM.Value
sF = RegExReplace(sF, "(\d+)\(.+\)", "$1")
'Debug.Print sF
If InStr(oM.Value, "(") > 0 Then
lF = Val(sF)
Else
lF = 1
End If
'Debug.Print lF
Set oMC2 = RegExMatchCollection(oM.Value, "\d+[a-z]")
For Each oM2 In oMC2
Debug.Print lF, Val(oM2.Value), Replace(oM2.Value, Val(oM2.Value), "")
Next
Next
End Sub
Hi and tk u very much, can i ask how can help pass the result to labelSince RegEx was in demand, here's a little gimmick to break down a expression so that detailed information can be written into a table so that calculations can be carried out on it.
Code:Private pRegEx As Object Public Property Get oRegEx(Optional Reset As Boolean) As Object If (pRegEx Is Nothing) Then Set pRegEx = CreateObject("Vbscript.Regexp") If Reset Then Set pRegEx = Nothing Set oRegEx = pRegEx End Property Public Function RegExMatchCollection(ByVal SourceText As String, _ ByVal SearchPattern As String, _ Optional ByVal bIgnoreCase As Boolean = True, _ Optional ByVal bGlobal As Boolean = True, _ Optional ByVal bMultiLine As Boolean = True) As Object With oRegEx .Pattern = SearchPattern .IgnoreCase = bIgnoreCase .Global = bGlobal .MultiLine = bMultiLine Set RegExMatchCollection = .Execute(SourceText) End With End Function Public Function RegExReplace(ByVal SourceText As String, _ ByVal SearchPattern As String, _ ByVal ReplaceText As String, _ Optional ByVal bIgnoreCase As Boolean = True, _ Optional ByVal bGlobal As Boolean = True, _ Optional ByVal bMultiLine As Boolean = True) As String With oRegEx .Pattern = SearchPattern .IgnoreCase = bIgnoreCase .Global = bGlobal .MultiLine = bMultiLine RegExReplace = .Replace(SourceText, ReplaceText) End With End Function
Calling routine
Code:Sub test_textil() Const cT1 = "2(2a2b2c)2a16b6c3(3c3b)" 'Const cT2 = "2(2A2B)" Dim oMC As Object, oMC2 As Object Dim oM As Object, oM2 As Object Dim sF As String, lF As Long Set oMC = RegExMatchCollection(cT1, "((\d+\((\d+[a-z])+\))|(\d+[a-z])+)") For Each oM In oMC 'Debug.Print oM.Value, oM.FirstIndex + 1, oM.Length sF = oM.Value sF = RegExReplace(sF, "(\d+)\(.+\)", "$1") 'Debug.Print sF If InStr(oM.Value, "(") > 0 Then lF = Val(sF) Else lF = 1 End If 'Debug.Print lF Set oMC2 = RegExMatchCollection(oM.Value, "\d+[a-z]") For Each oM2 In oMC2 Debug.Print lF, Val(oM2.Value), Replace(oM2.Value, Val(oM2.Value), "") Next Next End Sub
Eberhard
Public Type t_Ordito
A As Integer
B As Integer
C As Integer
D As Integer
E As Integer
Total As Integer
End Type
Public Function GetOrdito(strOrdito As String) As t_Ordito
Dim Ordito As t_Ordito
Dim character As String
Dim strnumberBefore As String
Dim numberBefore As Integer
Dim distributeNumber As Integer
Dim distribute As Boolean
Dim i As Integer
distributeNumber = 1
For i = 1 To Len(strOrdito)
character = Mid(strOrdito, i, 1)
Debug.Print "Char in beginning" & character
If IsNumeric(character) Then
strnumberBefore = strnumberBefore & character
numberBefore = CInt(strnumberBefore)
Else
If distribute = False Then
distributeNumber = 1
Else
distributeNumber = numberBefore
End If
' Debug.Print "Char " & character & "Distribute " & distribute & " number " & distributeNumber & " Number before " & numberBefore
Select Case character
Case "A"
Ordito.A = Ordito.A + numberBefore * distributeNumber
Debug.Print Ordito.A & " A"
strnumberBefore = ""
Case "B"
Ordito.B = Ordito.B + numberBefore * distributeNumber
strnumberBefore = ""
Case "C"
Ordito.C = Ordito.C + numberBefore * distributeNumber
strnumberBefore = ""
Case "D"
Ordito.D = Ordito.D + numberBefore * distributeNumber
strnumberBefore = ""
Case "E"
Ordito.E = Ordito.E + numberBefore * distributeNumber
strnumberBefore = ""
Case "("
distribute = True
distribute = numberBefore
strnumberBefore = ""
Case ")"
distribute = False
distributeNumber = 1
strnumberBefore = ""
End Select
End If
Next i
Ordito.Total = Ordito.A + Ordito.B + Ordito.C + Ordito.D + Ordito.E
GetOrdito = Ordito
End Function
Public Sub TestOrditio()
Dim Ordito As t_Ordito
Ordito = GetOrdito("2(2a2b2c)2a5b6c3(3c3b)")
Debug.Print "A = " & Ordito.A
Debug.Print "B = " & Ordito.B
Debug.Print "C = " & Ordito.C
Debug.Print "D = " & Ordito.D
Debug.Print "E = " & Ordito.E
Debug.Print "Totol = "; Ordito.Total
End Sub
A = 6
B = 18
C = 19
D = 0
E = 0
Total = 43
Public Function GetOrditoA(strOrdito As String) As Integer
Dim ordito As t_Ordito
ordito = GetOrdito(strOrdito)
GetOrditoA = ordito.A
End Function
Tk u very much,Solution provided
Code:Public Type t_Ordito A As Integer B As Integer C As Integer D As Integer E As Integer Total As Integer End Type Public Function GetOrdito(strOrdito As String) As t_Ordito Dim Ordito As t_Ordito Dim character As String Dim strnumberBefore As String Dim numberBefore As Integer Dim distributeNumber As Integer Dim distribute As Boolean Dim i As Integer distributeNumber = 1 For i = 1 To Len(strOrdito) character = Mid(strOrdito, i, 1) Debug.Print "Char in beginning" & character If IsNumeric(character) Then strnumberBefore = strnumberBefore & character numberBefore = CInt(strnumberBefore) Else If distribute = False Then distributeNumber = 1 Else distributeNumber = numberBefore End If ' Debug.Print "Char " & character & "Distribute " & distribute & " number " & distributeNumber & " Number before " & numberBefore Select Case character Case "A" Ordito.A = Ordito.A + numberBefore * distributeNumber Debug.Print Ordito.A & " A" strnumberBefore = "" Case "B" Ordito.B = Ordito.B + numberBefore * distributeNumber strnumberBefore = "" Case "C" Ordito.C = Ordito.C + numberBefore * distributeNumber strnumberBefore = "" Case "D" Ordito.D = Ordito.D + numberBefore * distributeNumber strnumberBefore = "" Case "E" Ordito.E = Ordito.E + numberBefore * distributeNumber strnumberBefore = "" Case "(" distribute = True distribute = numberBefore strnumberBefore = "" Case ")" distribute = False distributeNumber = 1 strnumberBefore = "" End Select End If Next i Ordito.Total = Ordito.A + Ordito.B + Ordito.C + Ordito.D + Ordito.E GetOrdito = Ordito End Function
Code:Public Sub TestOrditio() Dim Ordito As t_Ordito Ordito = GetOrdito("2(2a2b2c)2a5b6c3(3c3b)") Debug.Print "A = " & Ordito.A Debug.Print "B = " & Ordito.B Debug.Print "C = " & Ordito.C Debug.Print "D = " & Ordito.D Debug.Print "E = " & Ordito.E Debug.Print "Totol = "; Ordito.Total End Sub
Output
Code:A = 6 B = 18 C = 19 D = 0 E = 0 Total = 43
It was actually easier than I thought. You would then have to either do the insert query or you could wrap in individual functions for sql. like
Code:Public Function GetOrditoA(strOrdito As String) As Integer Dim ordito As t_Ordito ordito = GetOrdito(strOrdito) GetOrditoA = ordito.A End Function