Sub ValidaRUT(ByRef txt As TextBox)
Dim Codigo As String
'Dim Flag As Boolean
Dim Rut As String
Dim rut_integer, i, factor, suma, digito, digitostr
Flag = True
If txt.Text <> "" Then
Flag = False
If ((InStr(1, txt, "-") <> 0) And (InStr(1, txt, "-") = (Len(Trim(txt)) - 1))) Then
'largo y exitencia del guión
If Len(txt.Text) > 3 Then
If IsNumeric(Left(txt.Text, Len(txt.Text) - 2)) Then
Rut = CLng(Left(txt.Text, Len(txt.Text) - 2))
Codigo = Right(txt.Text, 1)
If Codigo = "K" Then Codigo = "k"
suma = 0
rut_integer = CLng(Rut)
factor = 2
For i = Len(Rut) To 0 Step -1
If factor > 7 Then factor = 2
suma = suma + (rut_integer Mod 10) * factor
rut_integer = rut_integer 10
factor = factor + 1
Next
digito = 11 - (suma Mod 11)
If digito = 10 Then digitostr = "k"
If digito = 11 Then digitostr = "0"
If digito < 10 Then digitostr = Right(CStr(digito), 1)
If digitostr = Codigo Then Flag = True
End If
End If
End If
If Flag = False Then
MsgBox "ADVERTENCIA: EL RUT INGRESADO NO ES VALIDO!!", vbOKOnly, "ADVERTENCIA"
txt.Text = ""
txt.SetFocus
End If
End If
End Sub