A continuación os muestro el codigo de una funcion que abre una nueva hoja de excel con los datos que queremos exportar, a esta funcion unicamente se la pasa la cadena sql con la consulta que queremos exportar
Sub ExpAExcel(cadSQL As Variant)
Dim appExcel As Object 'Excel.Application
Dim hoja As Object
Dim con As Connection
Dim rst As Recordset 'DAO.Recordset
Dim fld As Object 'DAO.Field
Dim i As Integer
Dim nom As String
Dim fila As Integer, columna As Integer
Set appExcel = CreateObject("Excel.Application")
Set con = Application.CurrentProject.Connection
' abrimos excel y añadimos un libro nuevo
appExcel.Visible = True
appExcel.Workbooks.Add
' añadimos una hoja nueva por cada consulta que se
' haya pasado como parámetro
Set hoja = appExcel.Sheets.Add
nom = cadSQL
' si el nombre de la consulta es >31 caracteres
' dará error así que lo recortamos
If Len(nom) > 31 Then
nom = Left(nom, 31)
End If
' ... y le damos nombre a la hoja
'hoja.Name = cadSQL
' abrimos un recordset
Set rst = CreateObject("ADODB.RecordSet")
rst.Open cadSQL, con, 1
' ponemos nombre a las columnas de las hojas
' igual que el nombre de los campos del recordset
fila = 10
columna = 1
For Each fld In rst.Fields
hoja.cells(fila, columna) = fld.Name
columna = columna + 1
Next
' después traspasamos el valor de los campos
' a las celdas de la hoja de excel
fila = 10
columna = 1
While Not rst.EOF
For Each fld In rst.Fields
hoja.cells(fila, columna) = fld.Value
columna = columna + 1
Next
columna = 1
fila = fila + 1
rst.MoveNext
Wend
Dim rango As String
Dim letran As Integer
Dim letra As String
letran = Lista26.ColumnCount
If (letran = 1) Then
letra = "a"
ElseIf (letran = 2) Then
letra = "b"
ElseIf (letran = 3) Then
letra = "c"
ElseIf (letran = 4) Then
letra = "d"
End If
rango = "A1:" & letra & Lista26.ListCount
Dim xlCategory, xlPrimary As Object
'Dim linea As Object
'Set linea = CreateObject("Excel.XlChartType")
'linea = "xlLine"
appExcel.Range(rango).Select
appExcel.Charts.Add
appExcel.ActiveChart.SetSourceData Source:=appExcel.Sheets("Hoja4").Range(rango), PlotBy:=xlColumns
'appExcel.ActiveChart.Location Where:=xlLocationAsObject, Name:="Hoja1"
With appExcel.ActiveChart
appExcel.ActiveChart.HasTitle = True
appExcel.ActiveChart.ChartTitle.Characters.Text = "EVOLUCION DEL PARAMETRO " & Cuadro_combinado30.Value & " " & Texto70.Value & " EN LA ESTACION " & Cuadro_combinado3.Value
'appExcel.ActiveChart.Axes(xlCategory, xlPrimary).HasTitle = True
'appExcel.ActiveChart.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "FECHA"
'appExcel.ActiveChart.Axes(xlValue, xlPrimary).HasTitle = True
'appExcel.ActiveChart.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "VALOR"
End With
appExcel.ActiveChart.HasLegend = False
'appExcel.ActiveChart.ApplyDataLabels Type:=xlDataLabelsShowValue, LegendKey:=False
'appExcel.ActiveChart.ChartType = linea
Set appExcel = Nothing