Fin en vba

08/06/2007 - 23:00 por crx73 | Informe spam
Buenas tardes amigos.
Tengo en una hoja de calculo una tabla con información de personas
(Clave, nombre completo, clave fiscal, domicilio, etc), esa
información la he agregado por medio de un userform sin necesidad de
abrir la hoja y capturar.
Ahora bien, quiero efectuar una busqueda en esa hoja por medio de otro
userform, dicho userform cuenta con 2 campos de texto (textbox) que en
realidad el objeto a buscar, ya que uno es para efectuar la busqueda
por nombre y otro para la busqueda por la clave fiscal.
Cabe aclarar que solo se puede efectuar la busqueda por alguno de los
dos campos disponibles, se hara por el nombre o por la clave fiscal.

Para ello he hecho algo asi:

Private Sub cmdOK_Click()
'
'Abrimos el Catálogo de Clientes
Sheets("CC").Visible = True
ActiveWorkbook.Sheets("CC").Activate
ActiveWindow.DisplayGridlines = False
'Asignamos el nombre CveRFC al valor del campo txtRFC
Dim CveRFC As String
CveRFC = txtRFC.Value
'Asignamos el nombre NomDen al valor del campo txtNombre
Dim NomDen As String
NomDen = txtNombre.Value
If optBuscaRFC = True Then
'Iniciamos la búsqueda por RFC
Dim RFCIni As String
RFCIni = "$D$7"
Dim RFCFin As String
Range(RFCIni).Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
RFCFin = ActiveCell.Offset(-1, 0).Address

'Establecemos nuestro rango de búsqueda e iniciamos con la operación
ActiveWorkbook.Names.Add Name:="RanBusqRFC",
RefersToR1C1:=Range(RFCIni, RFCFin)
Application.Goto Reference:="RanBusqRFC"

Set BuskRFC = Selection.Find(What:=CveRFC, After:=ActiveCell,
LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns,
SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
'Si el RFC que ingresamos existe en nuestro catálogo, terminamos la
búsqueda
If Not BuskRFC Is Nothing Then
BuskRFC.Activate
Dim CveClisRFC As String
CveClisRFC = (ActiveCell.Offset(0, -3).Value)
Dim NomClisRFC As String
NomClisRFC = (ActiveCell.Offset(0, -1).Value)
'Informamos a quién pertenece el RFC
MsgBox "El R.F.C. que ingresó pertenece a: " &
UCase(NomClisRFC), _
vbInformation, "Cliente encontrado"
Dim Response
Response = MsgBox("¿Es correcto?", vbYesNo +
vbQuestion, "Verificando información")
If Response = vbYes Then
MsgBox "La clave del cliente es: " & CveClisRFC, _
vbInformation, "Tome nota..."
Sheets("CC").Visible = False
Sheets("Detalle").Activate
Exit Sub
Else
Exit Sub
Unload Me
Sheets("CC").Visible = False
Sheets("Detalle").Activate
'ESTE ES EL PUNTO DONDE QUIERO CONTINUAR LA
BUSQUEDA YA QUE EL USUARIO DICE QUE LO QUE ENCONTRO NO ES LO QUE
BUSCABA, SABEMOS QUE EN UNA TABLA PUEDEN HABER MAS DE UN NOMBRE
REPETIDO, POR LO QUE LA PRIMERA CELDA ENCONTRADA NO PUEDE SER LA QUE
BUSCAMOS.

End If
Else
'Pero si no encontramos nada informamos de ello
Unload Me
MsgBox "La clave de R.F.C. que ingresó no existe",
vbCritical, _
"R.F.C. no existe"
Sheets("CC").Visible = False
Sheets("Detalle").Activate
Exit Sub
End If

Else
'Iniciamos la búsqueda por nombre

Dim NomIni As String
NomIni = "$D$7"
Dim NomFin As String
Range(NomIni).Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
NomFin = ActiveCell.Offset(-1, 0).Address

'Establecemos nuestro rango de búsqueda e iniciamos con la
operación
ActiveWorkbook.Names.Add Name:="RanBusqNom", _
RefersToR1C1:=Range(NomIni, NomFin)
Application.Goto Reference:="RanBusqNom"
Set BuskNom = Selection.Find(What:=NomDen, After:=ActiveCell,
_
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, MatchCase:=False,
SearchFormat:=False)
'Si el nombre que ingresamos existe en nuestro catálogo,
terminamos la búsqueda
If Not BuskNom Is Nothing Then
BuskNom.Activate
Dim CveClisNom As String
CveClisNom = (ActiveCell.Offset(0, -2).Value)
Dim NomClisNom As String
NomClisNom = (ActiveCell.Value)
MsgBox "El nombre que ingresó pertenece a: " &
UCase(NomClisNom), _
vbInformation, "Cliente encontrado"
Dim Msg2, Style2, Title2, Response2
Msg2 = "¿Es correcto?"
Style2 = vbYesNo + vbQuestion
Title2 = "Vericando información"
Response2 = MsgBox(Msg, Style, Title)
Do
If Response2 = vbNo Then
Set BuskNom = Selection.Find(What:=NomDen,
After:=ActiveCell, _
LookIn:=xlValues, LookAt:=xlPart,
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, MatchCase:=False,
SearchFormat:=False)
'Si el nombre que ingresamos existe en nuestro
catálogo, terminamos la búsqueda
If Not BuskNom Is Nothing Then
BuskNom.Activate
Dim CveClisNom1 As String
CveClisNom1 = (ActiveCell.Offset(0,
-2).Value)
Dim NomClisNom1 As String
NomClisNom1 = (ActiveCell.Value)
MsgBox "El nombre que ingresó pertenece a: " &
UCase(NomClisNom), _
vbInformation, "Cliente encontrado"
Dim Msg3, Style3, Title3, Response3
Msg3 = "¿Es correcto?"
Style3 = vbYesNo + vbQuestion
Title3 = "Vericando información"
Response3 = MsgBox(Msg, Style, Title)
Loop Until Response2 = vbYes
MsgBox "La clave del cliente es: " & CveClisNom, _
vbInformation, "Tome nota..."
Sheets("CC").Visible = False
Sheets("Detalle").Activate
Exit Sub

End If
Else
Unload Me
MsgBox "El nombre que ingresó no existe", vbCritical, _
"Nombre no existe"
Sheets("CC").Visible = False
Sheets("Detalle").Activate
Exit Sub
End If

End Sub
 

Leer las respuestas

#1 Héctor Miguel
09/06/2007 - 08:50 | Informe spam
hola, 'anonimo/a' !

1) la *parte* de codigo donde expones tu *inquietud* ES cuando estas bucando por *clave de RFC*
y [hasta donde se] se supone/espera/asume/endiende/... que NO encontraras dos RFC's iguales [o si ?]
-> a menos que en su TextBox se indiquen caracteres para una busqueda *parcial* -???-

2) si existe la posibilidad de dos [o mas] *registros* iguales/parecidos/... [ya sea nombre o RFC]
[probablemente] te servira mas utilizar [auto]filtros [o avanzados] para llenar [p.e.] un cuadro de lista...
-> desde el cual podria seleccionarse *cual de todos* [los encontrados] es al que se necesita hacer referencia -?-

si cualquier duda [o informacion adicional]... comentas ?
saludos,
hector.

__ la consulta original __
Buenas tardes amigos.
Tengo en una hoja de calculo una tabla con información de personas (Clave, nombre completo, clave fiscal, domicilio, etc)
esa información la he agregado por medio de un userform sin necesidad de abrir la hoja y capturar.
Ahora bien, quiero efectuar una busqueda en esa hoja por medio de otro userform
dicho userform cuenta con 2 campos de texto (textbox) que en realidad el objeto a buscar
ya que uno es para efectuar la busqueda por nombre y otro para la busqueda por la clave fiscal.
Cabe aclarar que solo se puede efectuar la busqueda por alguno de los dos campos disponibles, se hara por el nombre o por la clave fiscal.

Para ello he hecho algo asi:
Private Sub cmdOK_Click()
'Abrimos el Catálogo de Clientes
Sheets("CC").Visible = True
ActiveWorkbook.Sheets("CC").Activate
ActiveWindow.DisplayGridlines = False
'Asignamos el nombre CveRFC al valor del campo txtRFC
Dim CveRFC As String
CveRFC = txtRFC.Value
'Asignamos el nombre NomDen al valor del campo txtNombre
Dim NomDen As String
NomDen = txtNombre.Value
If optBuscaRFC = True Then
'Iniciamos la búsqueda por RFC
Dim RFCIni As String
RFCIni = "$D$7"
Dim RFCFin As String
Range(RFCIni).Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
RFCFin = ActiveCell.Offset(-1, 0).Address
'Establecemos nuestro rango de búsqueda e iniciamos con la operación
ActiveWorkbook.Names.Add Name:="RanBusqRFC", RefersToR1C1:=Range(RFCIni, RFCFin)
Application.Goto Reference:="RanBusqRFC"
Set BuskRFC = Selection.Find(What:=CveRFC, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
'Si el RFC que ingresamos existe en nuestro catálogo, terminamos la búsqueda
If Not BuskRFC Is Nothing Then
BuskRFC.Activate
Dim CveClisRFC As String
CveClisRFC = (ActiveCell.Offset(0, -3).Value)
Dim NomClisRFC As String
NomClisRFC = (ActiveCell.Offset(0, -1).Value)
'Informamos a quién pertenece el RFC
MsgBox "El R.F.C. que ingresó pertenece a: " & UCase(NomClisRFC), _
vbInformation, "Cliente encontrado"
Dim Response
Response = MsgBox("¿Es correcto?", vbYesNo + vbQuestion, "Verificando información")
If Response = vbYes Then
MsgBox "La clave del cliente es: " & CveClisRFC, _
vbInformation, "Tome nota..."
Sheets("CC").Visible = False
Sheets("Detalle").Activate
Exit Sub
Else
Exit Sub
Unload Me
Sheets("CC").Visible = False
Sheets("Detalle").Activate
'ESTE ES EL PUNTO DONDE QUIERO CONTINUAR LA BUSQUEDA YA QUE EL USUARIO DICE QUE LO QUE ENCONTRO
NO ES LO QUE BUSCABA, SABEMOS QUE EN UNA TABLA PUEDEN HABER MAS DE UN NOMBRE REPETIDO,
POR LO QUE LA PRIMERA CELDA ENCONTRADA NO PUEDE SER LA QUE BUSCAMOS.
End If
Else
'Pero si no encontramos nada informamos de ello
Unload Me
MsgBox "La clave de R.F.C. que ingresó no existe", vbCritical, _
"R.F.C. no existe"
Sheets("CC").Visible = False
Sheets("Detalle").Activate
Exit Sub
End If
Else
'Iniciamos la búsqueda por nombre
Dim NomIni As String
NomIni = "$D$7"
Dim NomFin As String
Range(NomIni).Select
Do
If IsEmpty(ActiveCell) = False Then
ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
NomFin = ActiveCell.Offset(-1, 0).Address
'Establecemos nuestro rango de búsqueda e iniciamos con la operación
ActiveWorkbook.Names.Add Name:="RanBusqNom", _
RefersToR1C1:=Range(NomIni, NomFin)
Application.Goto Reference:="RanBusqNom"
Set BuskNom = Selection.Find(What:=NomDen, After:=ActiveCell, _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
'Si el nombre que ingresamos existe en nuestro catálogo, terminamos la búsqueda
If Not BuskNom Is Nothing Then
BuskNom.Activate
Dim CveClisNom As String
CveClisNom = (ActiveCell.Offset(0, -2).Value)
Dim NomClisNom As String
NomClisNom = (ActiveCell.Value)
MsgBox "El nombre que ingresó pertenece a: " & UCase(NomClisNom), _
vbInformation, "Cliente encontrado"
Dim Msg2, Style2, Title2, Response2
Msg2 = "¿Es correcto?"
Style2 = vbYesNo + vbQuestion
Title2 = "Vericando información"
Response2 = MsgBox(Msg, Style, Title)
Do
If Response2 = vbNo Then
Set BuskNom = Selection.Find(What:=NomDen, After:=ActiveCell, _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
'Si el nombre que ingresamos existe en nuestro catálogo, terminamos la búsqueda
If Not BuskNom Is Nothing Then
BuskNom.Activate
Dim CveClisNom1 As String
CveClisNom1 = (ActiveCell.Offset(0, -2).Value)
Dim NomClisNom1 As String
NomClisNom1 = (ActiveCell.Value)
MsgBox "El nombre que ingresó pertenece a: " & UCase(NomClisNom), _
vbInformation, "Cliente encontrado"
Dim Msg3, Style3, Title3, Response3
Msg3 = "¿Es correcto?"
Style3 = vbYesNo + vbQuestion
Title3 = "Vericando información"
Response3 = MsgBox(Msg, Style, Title)
Loop Until Response2 = vbYes
MsgBox "La clave del cliente es: " & CveClisNom, _
vbInformation, "Tome nota..."
Sheets("CC").Visible = False
Sheets("Detalle").Activate
Exit Sub
End If
Else
Unload Me
MsgBox "El nombre que ingresó no existe", vbCritical, _
"Nombre no existe"
Sheets("CC").Visible = False
Sheets("Detalle").Activate
Exit Sub
End If
End Sub

Preguntas similares