
Function Valida(codice As String) As Boolean
Dim rgx As New RegExp
'Cambia a false se vuoi che le lettere debbano essere solo maiuscole
rgx.IgnoreCase = True
rgx.Pattern = "^[A-Z]{1}[0-9]{8}[A-Z]{1}$"
Valida = rgx.test(codice)
End Function
|
'ESEMPIO 1
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1.Text <> "" Then
If Not Valida(TextBox1.Text) Then
TextBox1.BackColor = RGB(255, 0, 0)
MsgBox "Codice non valido", vbCritical, "Errore"
Cancel = True
Else
TextBox1.BackColor = RGB(255, 255, 255)
End If
Else
TextBox1.BackColor = RGB(255, 255, 255)
End If
End Sub
'ESEMPIO 2
Private Sub TextBox1_Change()
If Valida(TextBox1.Text) Then
TextBox1.BackColor = RGB(255, 255, 255)
Else
TextBox1.BackColor = RGB(255, 0, 0)
End If
End Sub |
Private Sub TB_LottoSugna_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With UserForm1_Avvio
If .TB_LottoSugna.Text <> "" Then
If Not Valida(.TB_LottoSugna.Text) Then
MsgBox "Lotto sugna non valido!", vbCritical, "Errore di digitazione!"
.TB_LottoSugna.SetFocus
.TB_LottoSugna.SelStart = 0
.TB_LottoSugna.SelLength = Len(.TB_LottoSugna)
End If
End If
End With
End Sub
'il codice seguente è nel modulo 1
'FUNZIONE DI CONTROLLO VALIDITA' LOTTO SUGNA O PEPE
Function Valida(codice As String) As Boolean
Dim rgx As New RegExp
'Cambia a false se vuoi che le lettere debbano essere solo maiuscole
rgx.IgnoreCase = False
rgx.Pattern = "^[A-Z]{1}[0-9]{8}[A-Z]{1}$"
Valida = rgx.Test(codice)
End Function
|
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Call InsTextBox1
Cancel = True
TextBox1.SetFocus
End Sub
|
Sub InsTextBox1()
With UserForm1
If .TextBox1.Text <> "" Then
If Not Valida(.TextBox1.Text) Then
MsgBox "Lotto sugna non valido!", vbCritical, "Errore di digitazione!"
Cancel = True
.TextBox1.SetFocus
.TextBox1.SelStart = 0
.TextBox1.SelLength = Len(.TextBox1)
Else
.TextBox1.Value = UCase(.TextBox1.Text)
End If
End If
End With
End Sub
|
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With UserForm1
If .TextBox1.Text <> "" Then
If Not Valida(.TextBox1.Text) Then
MsgBox "Lotto sugna non valido!", vbCritical, "Errore di digitazione!"
.TextBox1.SetFocus
.TextBox1.SelStart = 0
.TextBox1.SelLength = Len(.TextBox1)
Cancel = True
Else
.TextBox1.Value = UCase(.TextBox1.Text)
End If
End If
End With
End Sub
|
Function Valida(codice As String) As Boolean
Dim rgx As New RegExp
'Cambia a false se vuoi che le lettere debbano essere solo maiuscole
rgx.IgnoreCase = True
rgx.Pattern = "^[A-Z]{1}[0-9]{5,8}[A-Z]{1}$"
Valida = rgx.Test(codice)
End Function
|
Function validaKg(testo)
Dim rgx As New RegExp
Dim corrispondenze As Object
Dim tempVal As String
rgx.Global = True
rgx.IgnoreCase = True
'Cambio il case della stringa Kg (se presente) dai formati "KG", "kG", "kg" in "Kg"
'Se non presente lo aggiungo
rgx.Pattern = "(Kg)+"
Set corrispondenze = rgx.Execute(testo)
If corrispondenze.Count > 0 Then
testo = rgx.Replace(testo, "Kg")
Else
testo = testo & " Kg"
End If
rgx.IgnoreCase = False
'Controllo se il testo rispecchia il formato necessario
rgx.Pattern = "[0-9]+,[0-9]+"
Set corrispondenze = rgx.Execute(testo)
'Se lo rispecchia restituisco il testo così com'è
If corrispondenze.Count > 0 Then
validaKg = testo
End If
'Catturo la stringa dei numeri separati dal .
rgx.Pattern = "[0-9]+.[0-9]+"
Set corrispondenze = rgx.Execute(testo)
'Se la trovo imposto tempVal alla stringa catturata altrimenti lascio il testo così com'è
If corrispondenze.Count > 0 Then
tempVal = corrispondenze.Item(0)
Else
tempVal = testo
End If
'Sostituisco il . con la ,
tempVal = Replace(tempVal, ".", ",")
'Restituisco il nuovo valore
validaKg = rgx.Replace(testo, tempVal)
End Function |
Private Sub TextBox50_Change()
If TextBox50.Text <> "" Then
If ValidaKg(TextBox50.Text) Then 'questo il punto con l'errore
TextBox50.ForeColor = &H0&
TextBox50.BackColor = &HFFFFFF
TextBox50.Font.Bold = False
Else
TextBox50.ForeColor = &HFFFFFF
TextBox50.BackColor = &HFF&
TextBox50.Font.Bold = True
End If
End If
End Sub
|
| A | B | C | |
| 1 | stringa | regex scossa | regex gdito |
| 2 | KG 15.5 | 15,5 Kg | Kg 15,5 |
| 3 | kg 251.25 KG | 251,25 Kg | Kg 251,25 Kg |
| 4 | asdsd | asdsd Kg | |
| 5 | asd 45 | 45 Kg | asd 45 Kg |
| 6 | 251.34 | 251,34 Kg | 251,34 Kg |
| 7 | 251,3454 | 251,3454 Kg | 251,3454 Kg |
| 8 | kg 251,25 KG | 251,25 Kg | Kg 251,25 Kg |
| 9 | 450 | 450 Kg | 450 Kg |
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
Function formatKg(ByVal sString As String) As String
'by scossa
Dim oRegEx As Object
Set oRegEx = CreateObject("vbscript.regexp")
With oRegEx
.Global = True
.IgnoreCase = True
.Pattern = "(?:[A-Zs]*)([0-9]+)(,|.)?([0-9]*)( w*)*" '"(?:w*s*)([0-9]+).([0-9]+)+( w*)*" meglio: "(?:[A-Zs]*)([0-9]+)(,|.)?([0-9]*)( w*)*"
If .test(sString) Then
formatKg = Replace(Trim(.Replace(sString, "$1 $3")), " ", ",") & " Kg"
End If
End With
Set oRegEx = Nothing
End Function
|
Private Sub TextBox50_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
TextBox50.Value = ValidaKg(TextBox50.Text)
End Sub
|
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
Function formatKg(ByVal sString As String) As String
'by scossa
Dim oRegEx As Object
Dim sToken3 As String
Set oRegEx = CreateObject("vbscript.regexp")
With oRegEx
.Global = True
.IgnoreCase = True
.Pattern = "(?:[A-Zs]*)([0-9]+)(,|.)?([0-9]*)( w*)*" '"(?:w*s*)([0-9]+).([0-9]+)+( w*)*" meglio: "(?:[A-Zs]*)([0-9]+)(,|.)?([0-9]*)( w*)*"
If .test(sString) Then
formatKg = .Replace(sString, "$1")
sToken3 = .Replace(sString, "$3")
If sToken3 <> "" Then
formatKg = formatKg & "," & Left(sToken3, 2)
End If
formatKg = formatKg & " Kg"
End If
End With
Set oRegEx = Nothing
End Function |
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
| scossa's web site |
| Se tu hai una mela, ed io ho una mela, e ce le scambiamo, allora tu ed io abbiamo sempre una mela per uno. Ma se tu hai un'idea, ed io ho un'idea, e ce le scambiamo, allora abbiamo entrambi due idee. (George Bernard Shaw) |
Function formatKg(ByVal sString As String) As String
'by scossa
Dim oRegEx As Object
Dim sToken3 As String
Set oRegEx = CreateObject("vbscript.regexp")
With oRegEx
.Global = True
.IgnoreCase = True
.Pattern = "(?:[A-Zs]*)([0-9]+)(,|.)?([0-9]*)( w*)*"
If .test(sString) Then
formatKg = .Replace(sString, "$1")
sToken3 = .Replace(sString, "$3")
.Pattern = "(d)(?=(ddd)+(?!d))"
formatKg = .Replace(formatKg, "$1.")
If sToken3 <> "" Then
formatKg = formatKg & "," & Left(sToken3, 2)
End If
formatKg = formatKg & " Kg"
End If
End With
Set oRegEx = Nothing
End Function
|
