Tamanini, desculpe a demora mas como disse ando muito ocupado nas últimas semanas.
Dim i As Integer
Dim j As Integer
Private Sub Form_Load()
End Sub
Private Sub txtPaginasParaImpressao_KeyPress(KeyAscii As Integer)
If KeyAscii = 44 Then KeyAscii = 59
If txtPaginasParaImpressao.SelStart = 0 And (KeyAscii = 45 Or KeyAscii = 59) Then KeyAscii = 0: Exit Sub
If fu_ValidaCaracteres(KeyAscii) = True Then KeyAscii = 0: Exit Sub
If KeyAscii = 59 Or KeyAscii = 45 Then If fu_ValidaVirgulaTraco() = True Then KeyAscii = 0: Exit Sub
If KeyAscii = 45 Then If fu_ValidaTraco() = True Then KeyAscii = 0
'fu_ValidaTamanho 'Ainda não foi ativada
End Sub
Private Function fu_ValidaCaracteres(iCaracter As Integer) As Boolean
If Not IsNumeric(Chr(iCaracter)) Then
Select Case iCaracter
Case 8, 13, 45, 59
Case Else
fu_ValidaCaracteres = True
End Select
End If
End Function
Private Function fu_ValidaTraco() As Boolean
Dim iQtde As Integer
For i = Len(txtPaginasParaImpressao.Text) To 0 Step -1
If Mid(txtPaginasParaImpressao.Text, IIf(txtPaginasParaImpressao.SelStart > iQtde, txtPaginasParaImpressao.SelStart - iQtde, txtPaginasParaImpressao.SelStart), 1) = "-" Then fu_ValidaTraco = True: Exit Function
If Mid(txtPaginasParaImpressao.Text, Len(txtPaginasParaImpressao.Text) - iQtde, 1) = ";" Then Exit Function
'If Mid(txtPaginasParaImpressao.Text, IIf(txtPaginasParaImpressao.SelStart > iQtde, txtPaginasParaImpressao.SelStart - iQtde, txtPaginasParaImpressao.SelStart), 1) = ";" Then Exit Function
iQtde = iQtde + 1
Next
iQtde = 0
For i = txtPaginasParaImpressao.SelStart To Len(txtPaginasParaImpressao.Text)
If Mid(txtPaginasParaImpressao.Text, txtPaginasParaImpressao.SelStart + iQtde, 1) = "-" Then fu_ValidaTraco = True: Exit Function
If Mid(txtPaginasParaImpressao.Text, txtPaginasParaImpressao.SelStart + iQtde, 1) = ";" Then Exit Function
iQtde = iQtde + 1
Next
End Function
Private Function fu_ValidaVirgulaTraco() As Boolean
If Len(txtPaginasParaImpressao.Text) = 0 Then
fu_ValidaVirgulaTraco = True
ElseIf Right(txtPaginasParaImpressao.Text, 1) = ";" Or Right(txtPaginasParaImpressao.Text, 1) = "-" Then
fu_ValidaVirgulaTraco = True
ElseIf Mid(txtPaginasParaImpressao.Text, txtPaginasParaImpressao.SelStart, 1) = ";" Or Mid(txtPaginasParaImpressao.Text, txtPaginasParaImpressao.SelStart, 1) = "-" Then
fu_ValidaVirgulaTraco = True
ElseIf Mid(txtPaginasParaImpressao.Text, txtPaginasParaImpressao.SelStart + 1, 1) = ";" Or Mid(txtPaginasParaImpressao.Text, txtPaginasParaImpressao.SelStart + 1, 1) = "-" Then
fu_ValidaVirgulaTraco = True
End If
End Function
Private Function fu_ValidaTamanho() As Boolean
Dim aTexto() As String
Dim iSelStart As Integer
sTexto = Replace(txtPaginasParaImpressao.Text, ";", "X"): sTexto = Replace(sTexto, "-", "X")
aTexto = Split(sTexto, "X")
If UBound(aTexto) > 0 Then
iSelStart = Len(aTexto(0)) + 1
If UBound(aTexto) > 1 Then
For i = 1 To UBound(aTexto)
iSelStart = iSelStart + Len(aTexto(i)) + 1
If aTexto(i - 1) > aTexto(i) Then
MsgBox "O valor ( " & aTexto(i - 1) & " ) é maior que o valor ( " & aTexto(i) & " )" & vbCrLf _
& "Os valores devem ser de forma crescente, Ex:" & vbCrLf & vbCrLf _
& "1;3;4-7;10;15;21-29"
txtPaginasParaImpressao.SelStart = iSelStart - Len(aTexto(i)) - 1
txtPaginasParaImpressao.SelLength = Len(aTexto(i))
fu_ValidaTamanho = True
Exit Function
End If
Next
End If
End If
End Function
Private Sub txtPaginasParaImpressao_LostFocus()
'Retira o últomo caracter, caso não seja numérico
If Not IsNumeric(Right(txtPaginasParaImpressao.Text, 1)) Then
txtPaginasParaImpressao.Text = Left(txtPaginasParaImpressao.Text, Len(txtPaginasParaImpressao.Text) - 1)
End If
'############ Verifica os tamanhos ###############################
Dim aTexto() As String
Dim iSelStart As Integer
sTexto = Replace(txtPaginasParaImpressao.Text, ";", "X"): sTexto = Replace(sTexto, "-", "X")
aTexto = Split(sTexto, "X")
If UBound(aTexto) > 0 Then
iSelStart = Len(aTexto(0)) + 1
'If UBound(aTexto) > 1 Then
For i = 1 To UBound(aTexto)
iSelStart = iSelStart + Len(aTexto(i)) + 1
If CLng((aTexto(i - 1))) > CLng(aTexto(i)) Then
MsgBox "O valor ( " & aTexto(i - 1) & " ) é maior que o valor ( " & aTexto(i) & " )" & vbCrLf _
& "Os valores devem ser de forma crescente, Ex:" & vbCrLf & vbCrLf _
& "1;3;4-7;10;15;21-29", vbCritical + vbOKOnly
txtPaginasParaImpressao.SelStart = iSelStart - Len(aTexto(i)) - 1
txtPaginasParaImpressao.SelLength = Len(aTexto(i))
txtPaginasParaImpressao.SetFocus
Exit Sub
End If
Next
'End If
End If
'###################################################################
'############### Verifica Repetido #################################
'hehehe, vamos brincar um pouquinho com Matriz
Dim aTexto2() As String
Dim iQtdeRepeticoes() As Integer
Dim iSelStart2 As Integer
sTexto = Replace(txtPaginasParaImpressao.Text, ";", "X"): sTexto = Replace(sTexto, "-", "X")
aTexto2 = Split(sTexto, "X")
ReDim iQtdeRepeticoes(UBound(aTexto2))
If UBound(aTexto2) > 0 Then
iSelStart2 = Len(aTexto2(0)) + 1
For i = 0 To UBound(aTexto2)
iSelStart2 = iSelStart2 + Len(aTexto2(i)) + 1
For j = 0 To UBound(aTexto2)
If aTexto2(i) = aTexto2(j) Then iQtdeRepeticoes(i) = iQtdeRepeticoes(i) + 1
Next
Next
End If
For i = 0 To UBound(iQtdeRepeticoes)
If iQtdeRepeticoes(i) > 1 Then
MsgBox "Não é interessante que se repita a(s) página(s).", vbInformation + vbOKOnly
txtPaginasParaImpressao.SelStart = iSelStart2 '- Len(aTexto2(i)) - 1
txtPaginasParaImpressao.SelLength = Len(aTexto2(i))
txtPaginasParaImpressao.SetFocus
Exit Sub
End If
Next
'###################################################################
End Sub
Private Function fu_VerificaRepetidos() As Boolean
'hehehe, vamos brincar um pouquinho com Matriz
Dim aTexto() As String
Dim aTextoTemp() As String
Dim iQtdeRepeticoes As Integer
Dim iSelStart As Integer
sTexto = Replace(txtPaginasParaImpressao.Text, ";", "X"): sTexto = Replace(sTexto, "-", "X")
aTexto = Split(sTexto, "X")
aTextoTemp = Split(sTexto, "X")
If UBound(aTexto) > 0 Then
iQtdeRepeticoes = 0
iSelStart = Len(aTexto(0)) + 1
For i = 0 To UBound(aTexto)
iSelStart = iSelStart + Len(aTexto(i)) + 1
For j = 0 To UBound(aTextoTemp)
If aTexto(i) = aTextoTemp(j) Then iQtdeRepeticoes = iQtdeRepeticoes + 1
If iQtdeRepeticoes = 2 Then
MsgBox "Não é interessante que se repita a(s) página(s).", vbInformation + vbOKOnly
txtPaginasParaImpressao.SelStart = iSelStart - Len(aTexto(i)) - 1
txtPaginasParaImpressao.SelLength = Len(aTexto(i))
txtPaginasParaImpressao.SetFocus
Exit For
End If
Next
Next
End If
End Function
Acho que agora esta legal.