Hoja actual en macro

Hola, estoy armando una macro que me despliegue una tabla y un grafico de dicha tabla en la hoja, luego al dia siguiente tengo que correr la macro en l ahoja siguiente pero me encuentro con que excel crea la tabla en la hoja nueva, toma los datos para la grafica en la hoja nueva pero coloca el grafico terminado en la hoja 1, mi pregunta es si se puede lograr que al igual que la tabla y los datos de la tabla sean cada uno para cada hoja. Es para un archivo que debe ser modificado por varios usuarios varias veces al dia.
desde ya muchas gracias.
{"Lat":-32.9718037763576,"Lng":-60.64453125}

1 Respuesta

Respuesta
1
Como no dejaste tu rutina solo puedo darte algunas instrucciones para que pruebes:
La expresión 'ActiveSheet' hace mención a la hoja activa.
La expresión Sheets("Hoja1") hace mención a la hoja1.
De esta manera podés hacer referencia a dónde están los datos y dónde debe ser guardado el gráfico.
Aquí 2 ejemplos de su uso:
ActiveSheet.Shapes.AddChart.Select
 ActiveChart.SetSourceData Source:=Range("Hoja1!$A$3:$B$12")
Si te fue claro, no olvides finalizar la consulta
hola y desde ya muchas gracias! te paso el codigo de la macro para que veas como actua, es solo un ejemplo pero muestra de forma exacta lo que quiero hacer, una macro que toe los datos de una hoja y me cree un grafico en esa hoja y al pasar a la hoja 2 cree la tabla en l ahoja 2 y genere la grafica en la hoja 2 y despues en la 3 y asi sucesivamente.
Sub tabluski()
'
' tabluski Macro
' Macro recorded 22/04/2010 by MiKiCuN
'
' Keyboard Shortcut: Ctrl+t
'
    ActiveCell.FormulaR1C1 = "Horario"
    Range("A2:A5").Select
    Selection.NumberFormat = "h:mm:ss;@"
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "9:00:00 AM"
    Range("A3").Select
    ActiveCell.FormulaR1C1 = "12:00:00 PM"
    Range("A4").Select
    ActiveCell.FormulaR1C1 = "3:00:00 PM"
    Range("A5").Select
    ActiveCell.FormulaR1C1 = "6:00:00 PM"
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "Upload"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "Download"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "Ping (ms)"
    Range("A1:D5").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Range("A1:D1").Select
    Range("D1").Activate
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Range("A1:A5").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Interior
        .ColorIndex = 35
        .Pattern = xlSolid
    End With
    Range("B1:D1").Select
    With Selection.Interior
        .ColorIndex = 35
        .Pattern = xlSolid
    End With
    Range("A1").Select
    Selection.Interior.ColorIndex = 36
    Range("F3").Select
    Charts.Add
    ActiveChart.ChartType = xlLineMarkers
    ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("A1:D5"), PlotBy:= _
        xlColumns
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
    With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = "1"
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "2"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "3"
    End With
    ActiveWindow.Visible = False
    Windows("Book12").Activate
    Range("D10").Select
End Sub
Te dejo la rutina ajustada y marcada:
Sub tabluski()
'
' tabluski Macro
' Macro recorded 22/04/2010 by MiKiCuN
' Ajustada por Elsamatilde
' Keyboard Shortcut: Ctrl+t
'aquí toma datos de la hoja activa y arma la tabla con bordes
'por lo tanto no es necesario indicarle nbre de hoja
'es decir que si ejecutas la macros desde Hoja1, toma la tabla de Hoja1.
'si ejecutas desde Hoja2 toma los datos de Hoja2.
    ActiveCell.FormulaR1C1 = "Horario"
    Range("A2:A5").Select
    Selection.NumberFormat = "h:mm:ss;@"
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "9:00:00 AM"
    Range("A3").Select
    ActiveCell.FormulaR1C1 = "12:00:00 PM"
    Range("A4").Select
    ActiveCell.FormulaR1C1 = "3:00:00 PM"
    Range("A5").Select
    ActiveCell.FormulaR1C1 = "6:00:00 PM"
    Range("B1").Select
    ActiveCell.FormulaR1C1 = "Upload"
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "Download"
    Range("D1").Select
    ActiveCell.FormulaR1C1 = "Ping (ms)"
    Range("A1:D5").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Range("A1:D1").Select
    Range("D1").Activate
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
    End With
    Range("A1:A5").Select
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlMedium
        .ColorIndex = xlAutomatic
    End With
    With Selection.Interior
        .ColorIndex = 35
        .Pattern = xlSolid
    End With
    Range("B1:D1").Select
    With Selection.Interior
        .ColorIndex = 35
        .Pattern = xlSolid
    End With
    Range("A1").Select
    Selection.Interior.ColorIndex = 36
    Range("F3").Select
'a partir de aquí se crea el gráfico. Para indicarle que tome los datos de la hoja activa
'debes guardar el nombre en 1 variable y utilizarla en lugar de "Sheet1"
mihoja = ActiveSheet.Name
    Charts.Add
    ActiveChart.ChartType = xlLineMarkers
    ActiveChart.SetSourceData Source:=Sheets(mihoja).Range("A1:D5"), PlotBy:= _
        xlColumns
    ActiveChart.Location Where:=xlLocationAsObject, Name:=mihoja
    With ActiveChart
        .HasTitle = True
        .ChartTitle.Characters.Text = "1"
        .Axes(xlCategory, xlPrimary).HasTitle = True
        .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "2"
        .Axes(xlValue, xlPrimary).HasTitle = True
        .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "3"
    End With
    ActiveWindow.Visible = False
    Windows("Book12").Activate
    Range("D10").Select
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas