Como coloco codigos en un modulo

Tengo un archivo que debido a toda la programacion que le he colocado ha quedado muy pesado, quiero saber como puedo colocar cada operacion en un modulo diferente y que cuando yo haga el cambio el textbox o del combobox me traiga la operacion del modulo y asi poder quitarla del userform.

Por ejemplo tengo este codigo en un textbox y quiero colocarlo en un modulo pero cuando vuelva a configurar el textbox pues solamente seria colocarle la operacion de que llame al modulo.

Private Sub cod_ref_AfterUpdate()

Set h = Sheets("AAA")
Set b = h.Columns("A").Find(cod_ref)
ref_prod = ""
If Not b Is Nothing Then
ref_prod = h.Cells(b.Row, "B")
End If
codmp1 = ""
If Not b Is Nothing Then
codmp1 = h.Cells(b.Row, "D")
End If
codmp2 = ""
If Not b Is Nothing Then
codmp2 = h.Cells(b.Row, "E")
End If
codmp3 = ""
If Not b Is Nothing Then
codmp3 = h.Cells(b.Row, "F")
End If
codmp4 = ""
If Not b Is Nothing Then
codmp4 = h.Cells(b.Row, "G")
End If
codmp5 = ""
If Not b Is Nothing Then
codmp5 = h.Cells(b.Row, "H")
End If
codmp6 = ""
If Not b Is Nothing Then
codmp6 = h.Cells(b.Row, "I")
End If
codmp7 = ""
If Not b Is Nothing Then
codmp7 = h.Cells(b.Row, "J")
End If
codmp8 = ""
If Not b Is Nothing Then
codmp8 = h.Cells(b.Row, "K")
End If
codmp9 = ""
If Not b Is Nothing Then
codmp9 = h.Cells(b.Row, "L")
End If
codmp10 = ""
If Not b Is Nothing Then
codmp10 = h.Cells(b.Row, "M")
End If
codmp11 = ""
If Not b Is Nothing Then
codmp11 = h.Cells(b.Row, "N")
Else
MsgBox "EL CODIGO NO EXISTE", vbExclamation, "BUSCAR EN LA BASE"
cod_ref.SetFocus
End If
Set h = Sheets("MP")
Set b = h.Columns("A").Find(codmp1)
desmp1 = ""
If Not b Is Nothing Then
desmp1 = h.Cells(b.Row, "B")
End If
Set b = h.Columns("A").Find(codmp2)
desmp2 = ""
If Not b Is Nothing Then
desmp2 = h.Cells(b.Row, "B")
End If
Set b = h.Columns("A").Find(codmp3)
desmp3 = ""
If Not b Is Nothing Then
desmp3 = h.Cells(b.Row, "B")
End If
Set b = h.Columns("A").Find(codmp4)
desmp4 = ""
If Not b Is Nothing Then
desmp4 = h.Cells(b.Row, "B")
End If
Set b = h.Columns("A").Find(codmp5)
desmp5 = ""
If Not b Is Nothing Then
desmp5 = h.Cells(b.Row, "B")
End If
Set b = h.Columns("A").Find(codmp6)
desmp6 = ""
If Not b Is Nothing Then
desmp6 = h.Cells(b.Row, "B")
End If
Set b = h.Columns("A").Find(codmp7)
desmp7 = ""
If Not b Is Nothing Then
desmp7 = h.Cells(b.Row, "B")
End If
Set b = h.Columns("A").Find(codmp8)
desmp8 = ""
If Not b Is Nothing Then
desmp8 = h.Cells(b.Row, "B")
End If
Set b = h.Columns("A").Find(codmp9)
desmp9 = ""
If Not b Is Nothing Then
desmp9 = h.Cells(b.Row, "B")
End If
Set b = h.Columns("A").Find(codmp10)
desmp10 = ""
If Not b Is Nothing Then
desmp10 = h.Cells(b.Row, "B")
End If
Set b = h.Columns("A").Find(codmp11)
desmp11 = ""
If Not b Is Nothing Then
desmp11 = h.Cells(b.Row, "B")
End If

End Sub

1 Respuesta

Respuesta

La primera parte la puedes resumir así:

Private Sub cod_ref_AfterUpdate()

Set h = Sheets("AAA")
Set b = h.Columns("A").Find(cod_ref)
If Not b Is Nothing Then
ref_prod = h.Cells(b.Row, "B")
codmp1 = h.Cells(b.Row, "D")
codmp2 = h.Cells(b.Row, "E")
codmp3 = h.Cells(b.Row, "F")
codmp4 = h.Cells(b.Row, "G")
codmp5 = h.Cells(b.Row, "H")
codmp6 = h.Cells(b.Row, "I")
codmp7 = h.Cells(b.Row, "J")
codmp8 = h.Cells(b.Row, "K")
codmp9 = h.Cells(b.Row, "L")
codmp10 = h.Cells(b.Row, "M")
codmp11 = h.Cells(b.Row, "N")
Else
MsgBox "EL CODIGO NO EXISTE", vbExclamation, "BUSCAR EN LA BASE"
cod_ref.SetFocus
End If

pero la otra parte son muchas busquedas  para colacar en modulo lo consigues asi

sub busqueda1 ( )

Set h = Sheets("MP")
Set b = h.Columns("A").Find(codmp1)
If Not b Is Nothing Then
desmp1 = h.Cells(b.Row, "B")
End If

end sub

sub busqueda2 ( )

Set b = h.Columns("A").Find(codmp2)
If Not b Is Nothing Then
desmp2 = h.Cells(b.Row, "B")
End If

end sub

... Y ve agregando las búsquedas enmurada 1,2,3,4...

La parte donde le dice que limpie no he necesario esta desmp11 = ""

Lo que me tiene confuso es que toda la búsqueda están en la misma columna pero esta bien

Si te silve no olvides valora para cerrar la pregunta

$$\begin{align}&hola\end{align}$$

Hola ya hice la primero parte y me quedo ok pero la segunda parte no la entiendo; ¿Lo qué entiendo es que todo ese código con los más que le agregue lo coloque en un modulo? Y después de eso como hago para que al momento de que haga la primera de la operación osea la que me enviaste se haga de una vez la segunda... gracias

Solo tiene que agregar el nombre asi despues de esta parte

MsgBox "EL CODIGO NO EXISTE", vbExclamation, "BUSCAR EN LA BASE"
cod_ref.SetFocus
End If

Busqueda1

Busqeuda2

Busqueda3

...

Si te quiere puedo darle una chequeada y ver como agilizar mas la macro

$$\begin{align}&recuerdad valorar si te silve para cerrar la pregunta \end{align}$$

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas