Exportar qry a excel para crear Gráfico y Editarlo
Publicado por Sabael (3 intervenciones) el 13/09/2018 16:54:48
Hola a todos.
Diseñé en MS Access 2016 una Base de Datos Relacional. He invertido mucho tiempo en este ejercicio.
En la BD tengo un formulario con 3 cajas de texto y un botón de comando con un código para exportar una consulta a Excel donde crea un Gráfico con dos ejes (xlPrimario y xlSecudario).
El código funciona bien, sin embargo, necesito que corra varias líneas de código que a continuación indico:
Adicionalmente, no he podido:
1. Garantizarme que todo el código es de origen MS Access VBA y que sea eficiente. .
2. Asignar el formato #.##0,00 a los valores de las etiquetas del eje secundario
3. Esconder los valores del eje secundario
4. Colocar en "interlineado sencillo" la segunda línea del código con respecto a la primera.
5. Que al exportar el archivo excel, sobreescribir el nombre del archivo Excel si ya existe en la carpeta.
6. En la Hoja ajustar el tamaño de las columnas al contenido
Agradezco su ayuda y sugerencias.
A continuación, incluyo mi código completo:
Diseñé en MS Access 2016 una Base de Datos Relacional. He invertido mucho tiempo en este ejercicio.
En la BD tengo un formulario con 3 cajas de texto y un botón de comando con un código para exportar una consulta a Excel donde crea un Gráfico con dos ejes (xlPrimario y xlSecudario).
El código funciona bien, sin embargo, necesito que corra varias líneas de código que a continuación indico:
1
2
3
4
5
6
'En la Hoja, las siguientes líneas de código no colocan el gráfico en la posición que se indica.
Set RngToCover = ws.Range("E1:O22")
ch.Height = RngToCover.Height
ch.Width = RngToCover.Width
ch.Top = RngToCover.Top
ch.Left = RngToCover.Left
1
2
3
4
5
6
7
8
9
'Las siguientes líneas no colorea el area de trazado
With ws.PlotArea.Fill '' this lines of code do not work
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent6
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Solid
End With
Adicionalmente, no he podido:
1. Garantizarme que todo el código es de origen MS Access VBA y que sea eficiente. .
2. Asignar el formato #.##0,00 a los valores de las etiquetas del eje secundario
3. Esconder los valores del eje secundario
4. Colocar en "interlineado sencillo" la segunda línea del código con respecto a la primera.
5. Que al exportar el archivo excel, sobreescribir el nombre del archivo Excel si ya existe en la carpeta.
6. En la Hoja ajustar el tamaño de las columnas al contenido
Agradezco su ayuda y sugerencias.
A continuación, incluyo mi código completo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Sub cmdTransfer_Click()
Dim sExcelWB As String
Dim xl As Object ''Excel.Application
Dim wb As Object ''Excel.Workbook
Dim ws As Object ''Excel.Worksheet
Dim ch As Object ''Excel.Chart
Dim myMax, myMind As Double
Dim RngToCover As Range
On Error Resume Next
Set xl = CreateObject("excel.application")
sExcelWB = "D:\testing2\" & Replace(Me.txttask_from, "/", "_") & " _" & Replace(Me.txttask_to, "/", "_") & " - " & Replace(Me.txttask_plot, "/", " _") & "_qry_task.xls"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "qry_task", sExcelWB, True
Set wb = xl.Workbooks.Open(sExcelWB)
Set ws = wb.Sheets("qry_task")
Set ch = ws.Shapes.AddChart.Chart
'En la Hoja, las siguientes líneas de código no colocan el gráfico en la posición que se indica.
Set RngToCover = ws.Range("E1:O22")
ch.Height = RngToCover.Height
ch.Width = RngToCover.Width
ch.Top = RngToCover.Top
ch.Left = RngToCover.Left
With ch
.ChartType = xlColumnClustered
.SeriesCollection(2).AxisGroup = 2
.SeriesCollection(2).ChartType = xlLineMarkers
.ChartGroups(1).GapWidth = 69
'Add Chart Title and position (vbCrLf) of second part of the title
.HasTitle = True
.ChartTitle.Text = "Plot " & Me.txttask_plot.Value & " " & ws.Range("C1").Value & " " & vbCrLf & "Between" & " " & "(" & Me.txttask_from.Value & " and " & Me.txttask_to.Value & ")"
.Format.TextFrame2.TextRange.Font.Size = 12
.Format.TextFrame2.TextRange.Characters(pos + 1, Len(.ChartTitle.Text) - pos).Font.Size = 10
' .ChartTitle.Format.TextFrame2.TextRange.Font.Size = 12
' .ChartTitle.Format.TextFrame2.TextRange.Characters(pos + 1, Len(.ChartTitle.Text) - pos).Font.Size = 10
.Axes(xlValue).MajorGridlines.Delete
.Axes(xlCategory, xlPrimary).HasTitle = False
.Axes(xlValue, xlPrimary).HasTitle = False
'Etiquetas para ColumnClustered
.FullSeriesCollection(1).Select
.SetElement (msoElementDataLabelShow)
.SetElement (msoElementDataLabelInsideBase)
.FullSeriesCollection(1).DataLabels.Select
Selection.Format.TextFrame2.TextRange.Font.Size = 9
With Selection.Format.TextFrame2.TextRange.Font.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Solid
End With
Selection.Format.TextFrame2.TextRange.Font.Bold = msoTrue
'etiquetas para xllinemarkers
.FullSeriesCollection(2).Select
.SetElement (msoElementDataLabelShow)
.SetElement (msoElementDataLabelTop)
.FullSeriesCollection(2).DataLabels.Select
Selection.Format.TextFrame2.TextRange.Font.Size = 9
Selection.Format.TextFrame2.TextRange.Font.Bold = msoTrue
'Título del eje vertical izquierdo
With .Axes(xlValue, xlPrimary)
.HasTitle = True
.AxisTitle.Text = "Jornales"
End With
'Formato del valor numérico del eje vertical izquierdo
ActiveChart.Axes(xlValue, xlPrimary).TickLabels.NumberFormat = "General"
'Formato del valor numérico del eje vertical derecho
ActiveChart.Axes(xlValue, xlSecondary).Selection.TickLabels.NumberFormat = "0.000,00"
'titulo eje vertical derecho
With .Axes(xlValue, xlSecondary)
.HasTitle = True
.AxisTitle.Text = "Costo de Jornales"
End With
' Esconder valores del eje vertical derecho
With ch.Axes(XlAxisType.xlCategory).TickLabels.Font
.ColorIndex = 2
.Background = xlTransparent
End With
'Legend
.SetElement (msoElementLegendBottom)
pos = InStr(.ChartTitle.Text, vbCrLf)
.Legend.Format.Shadow.Style = msoShadowStyleOuterShadow
'Valores Mínimos y Máximos del Eje Vertical Izquierdo
myMax = DMax("Total_Sal", "qry_task")
myMin = DMin("Total_Sal", "qry_task")
With .Axes(xlValue, xlPrimary)
.MinimumScale = myMin
.MaximumScale = myMax
End With
'Valores Mínimos y Máximos del Eje Vertical Derecho
myMax = DMax("Task_Val", "qry_task")
myMin = DMin("Task_Val", "qry_task")
With .Axes(xlValue, xlSecondary)
.MinimumScale = myMin
.MaximumScale = myMax
End With
' Color del fondo del área del Gráfico
ws.ChartObjects("Gráfico 1").Activate
With ws.Shapes("Gráfico 1").Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent6
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Solid
End With
' Color del fondo del área de trazado del Gráfico. Estas líneas de código no funcionan
With ws.PlotArea.Fill
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent6
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = 0
.Transparency = 0
.Solid
End With
End With
xl.Visible = True
xl.UserControl = True
End Sub
Valora esta pregunta
0