Macro Excel 2016 para reducir su tamaño
La macro es larga y por eso no deja subir la pregunta. Espero alguna respuesta para enviar la 2ª parte
Private Sub CommandButton1_Click()
'Por.Dante Amor
'Act. Adriel
'Filtrar por fecha
Dim u As Double, i As Double
Dim h1 As Object, h2 As Object, h3, h4 As Object
'
'Primer option Hoja PRODUCTOS
If OptionButton1 Then
'
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'
Set h1 = Sheets("Productos")
Set h2 = Sheets("Filtro")
h2.Cells.Clear
'
If DTPicker1 > DTPicker2 Then
MsgBox "La fecha inicial no puede ser superior a la final", vbExclamation, "REVISAR FECHAS"
Application.ScreenUpdating = True
Exit Sub
End If
'
If h1.AutoFilterMode Then h1.AutoFilterMode = False
u = h1.Range("E" & Rows.Count).End(xlUp).Row
h1.Range("A1:g" & u).AutoFilter
h1.Range("A1:g" & u).AutoFilter Field:=5, Criteria1:=">=" & Format(DTPicker1, "mm/dd/yyyy"), _
Operator:=xlAnd, Criteria2:="<=" & Format(DTPicker2, "mm/dd/yyyy")
If h1.Range("E" & Rows.Count).End(xlUp).Row = 1 Then
MsgBox "No existen registros", vbExclamation, "REVISAR FECHAS"
If h1.AutoFilterMode Then h1.AutoFilterMode = False
Application.ScreenUpdating = True
Exit Sub
End If
'
h1.Range("A1:g" & u).Copy h2.[A1]
ListBox1.RowSource = h2.Name & "!A2:g" & h2.Range("g" & Rows.Count).End(xlUp).Row
If h1.AutoFilterMode Then h1.AutoFilterMode = False
''' Cuenta y muestra cantidad de items en el ListBox
'txtExistencia.Text = ListBox1.ListCount
'''
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End If
' option Hoja ENTRADA
'''''sigue la misma macro solo cambian las letras de columna
' opcion Hoja SALIDA
,,,,,sigue la misma macro solo cambian las letras de columna
End SubEsta macro se repite 3 veces, lo que si cambia son los rangos
Principalmente las letras y hojas son las que cambian porque en la hoja Productos (h1) tengo 7 columnas, las fechas estan en E
En las hojas Entrada y Salida (h3, h4 respectivamente) las fechas estan en D y son solo 5 columnas por eso la repeticion casi copleta de la misma macro.
¿Alguien qué me ponga la macro mas corta y que me funcione segun el OptionButton que seleccione?
2 respuestas
Respuesta de James Bond
1
Respuesta de Programar Excel




esta es la pantalla para la hoja entrada
esta es la pantalla salida

