Excel. Macros. Impresión de rangos variables
Área de impresión con rangos variables
Hace poco he descubierto las macro por lo que tengo un nivel muy básico.
Acabo de crear una macro que, de una base de ventas, me crea un excel por cada comercial que a su vez alimenta a una tabla dinámica.
Mi problema es que la macro selecciona los datos de la base por el rango de impresión que he grabado, pero los datos de la base son variables y aumentan diariamente, por lo todos los datos que exceden el rango de impresión quedan fuera.
Mirando por distintos foros, he encontrado el siguiente ejemplo,
Sub SetPrintArea()
Dim x As Long, y As Long
Dim rngPrintArea As Range
x = Cells(1, Columns.Count).End(xlToLeft).Column
y = Cells(Rows.Count, 1).End(xlUp).Row
Set rngPrintArea = Range(Cells(4, 1), Cells(y, x))
ActiveSheet.PageSetup.PrintArea = rngPrintArea.Address
End Sub
Mi problema es que no sé cómo integrarlo en mi macro.
1 Respuesta
Debes dejar escrita tu macro aquí para poder integrar este código o u otro similar.
Hola Matilde.
Te la copio. Es un poco larga y repetitiva.
Gracias
Sub ImportarDatos()
'
' ImportarDatos Macro
'
'
Windows("BASE - 2015-2013.xlsx").Activate
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.Range("$A$1:$AK$125000").AutoFilter Field:=3, Criteria1:=Array( _
"Budget-2015", "RFO", "="), Operator:=xlFilterValues
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AK$125000").AutoFilter Field:=21, Criteria1:= _
"1900"
Columns("V:V").Select
Selection.ClearContents
Range("V1").Select
ActiveCell.FormulaR1C1 = "mes"
Range("V101774").Select
ActiveSheet.Range("$A$1:$AK$125000").AutoFilter Field:=21, Criteria1:=Array _
("1900", "2014", "2015"), Operator:=xlFilterValues
ActiveWindow.ScrollColumn = 11
ActiveWindow.ScrollColumn = 10
ActiveWindow.ScrollColumn = 9
ActiveWindow.ScrollColumn = 8
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollRow = 2
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AK125000").Select
Selection.Copy
Windows("Listado Semanal.xlsm").Activate
Range("A1").Select
ActiveSheet.Paste
Sheets("Javier").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("Rosa").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("Vacante").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("JB").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("Sebas").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("Aitor").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("Pedro").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("AngelR").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("AngelM").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("Madrid").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("Jordi").Select
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$65000").AutoFilter Field:=19, Criteria1:=Array( _
"0", "1019", "1021", "1023", "1026", "1029", "1030", "1034", "1038", "1039", "1041", _
"2333", "526", "785", "8888"), Operator:=xlFilterValues
ActiveWindow.LargeScroll ToRight:=-1
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$20000").AutoFilter Field:=19
ActiveWindow.LargeScroll ToRight:=-1
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AK20000").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$AK$20000"
Sheets("Javier").Select
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$65000").AutoFilter Field:=19, Criteria1:=Array( _
"0", "1019", "1021", "1023", "1027", "1029", "1030", "1034", "1038", "1039", "1041", _
"2333", "526", "785", "8888"), Operator:=xlFilterValues
ActiveWindow.LargeScroll ToRight:=-1
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$10823").AutoFilter Field:=19
ActiveWindow.LargeScroll ToRight:=-1
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AK10823").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$AK$10823"
Sheets("Rosa").Select
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$65000").AutoFilter Field:=19, Criteria1:=Array( _
"0", "1021", "1023", "1026", "1027", "1029", "1030", "1034", "1038", "1039", "1041", _
"2333", "526", "785", "8888"), Operator:=xlFilterValues
ActiveWindow.LargeScroll ToRight:=-1
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$13209").AutoFilter Field:=19
ActiveWindow.LargeScroll ToRight:=-1
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AK13209").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$AK$13209"
Sheets("Vacante").Select
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$65000").AutoFilter Field:=19, Criteria1:=Array( _
"0", "1019", "1021", "1023", "1026", "1027", "1029", "1030", "1034", "1038", "1039", _
"2333", "526", "785", "8888"), Operator:=xlFilterValues
ActiveWindow.LargeScroll ToRight:=-1
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$10232").AutoFilter Field:=19
ActiveWindow.LargeScroll ToRight:=-1
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AK6232").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$AK$10232"
Sheets("JB").Select
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$65000").AutoFilter Field:=19, Criteria1:=Array( _
"0", "1019", "1021", "1023", "1026", "1027", "1029", "1030", "1038", "1039", "1041", _
"2333", "526", "785", "8888"), Operator:=xlFilterValues
ActiveWindow.LargeScroll ToRight:=-1
Rows("36:36").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$5858").AutoFilter Field:=19
ActiveWindow.LargeScroll ToRight:=-1
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AK5858").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$AK$5858"
Sheets("Sebas").Select
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$65000").AutoFilter Field:=19, Criteria1:=Array( _
"0", "1019", "1023", "1026", "1027", "1029", "1030", "1034", "1038", "1039", "1041", _
"2333", "526", "785", "8888"), Operator:=xlFilterValues
ActiveWindow.LargeScroll ToRight:=-1
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveWindow.SmallScroll ToRight:=1
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$1238").AutoFilter Field:=19
Range("Q1").Select
ActiveWindow.LargeScroll ToRight:=-2
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AK1238").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$AK$1238"
Sheets("Aitor").Select
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$65000").AutoFilter Field:=19, Criteria1:=Array( _
"0", "1019", "1021", "1023", "1026", "1027", "1029", "1030", "1034", "1038", "1041", _
"2333", "526", "785", "8888"), Operator:=xlFilterValues
ActiveWindow.LargeScroll ToRight:=-1
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$776").AutoFilter Field:=19
ActiveWindow.LargeScroll ToRight:=-1
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AK776").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$AK$776"
Sheets("Pedro").Select
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$65000").AutoFilter Field:=19, Criteria1:=Array( _
"0", "1019", "1021", "1023", "1026", "1027", "1030", "1034", "1038", "1039", "1041", _
"2333", "526", "785", "8888"), Operator:=xlFilterValues
ActiveWindow.LargeScroll ToRight:=-1
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$610").AutoFilter Field:=19
ActiveWindow.LargeScroll ToRight:=-1
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AK610").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$AK$610"
Sheets("AngelR").Select
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$65000").AutoFilter Field:=19, Criteria1:=Array( _
"0", "1019", "1021", "1023", "1026", "1027", "1029", "1030", "1034", "1038", "1039", _
"1041", "526", "785", "8888"), Operator:=xlFilterValues
ActiveWindow.LargeScroll ToRight:=-1
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$983").AutoFilter Field:=19
ActiveWindow.LargeScroll ToRight:=-1
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AK983").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$AK$983"
Sheets("AngelM").Select
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$65000").AutoFilter Field:=19, Criteria1:=Array( _
"0", "1019", "1021", "1023", "1026", "1027", "1029", "1030", "1034", "1039", "1041", _
"2333", "526", "785", "8888"), Operator:=xlFilterValues
ActiveWindow.LargeScroll ToRight:=-1
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$498").AutoFilter Field:=19
ActiveWindow.LargeScroll ToRight:=-1
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AK498").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$AK$498"
Sheets("Madrid").Select
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$65000").AutoFilter Field:=19, Criteria1:=Array( _
"526", "785", "8888"), Operator:=xlFilterValues
ActiveWindow.LargeScroll ToRight:=-1
Rows("2:2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
ActiveWindow.LargeScroll ToRight:=1
ActiveSheet.Range("$A$1:$AL$57357").AutoFilter Field:=19
ActiveWindow.LargeScroll ToRight:=-1
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AK57357").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$AK$57357"
Sheets("Jordi").Select
Range("B32").Select
ActiveWorkbook.Save
End Sub
Podes reemplazar las líneas donde dejas el rango fijo:
Range("A1:AK125000").Select
Por esta otra donde el fin de rango lo da la col que tenga datos en todas sus filas, en mi ejemplo es A:
Range("A1:AK" & Range("A" & Rows.Count).End(xlUp).Row).Select
Para la impresión:
Buscá todas las veces que aparece este grupo de instrucciones:
ActiveSheet.Range("$A$1:$AL$20000").AutoFilter Field:=19
ActiveWindow.LargeScroll ToRight:=-1
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A1:AK20000").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$AK$20000"
Y reemplazalas por éstas:
ActiveSheet.Range("$A$1:$AL$" & Range("A" & Rows.Count).End(xlUp).Row).AutoFilter Field:=19
Range("A1:AK" & Range("A" & Rows.Count).End(xlUp).Row).Select
ActiveSheet.PageSetup.PrintArea = Selection.Address
Atención que en algunas utilizas AK y en otras AL.
Luego podes dejar otra consulta (o leer el tema en la sección Macros o Guías de mi sitio) de cómo armar un bucle y no tener que repetir tantas veces las mismas instrucciones.
- Compartir respuesta