codice:
Private Sub Comando0_Click()
Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlWorkbook As Excel.Workbook
Dim acQuery As QueryDef
Dim objRST As Recordset
Dim strQueryName As String
strQueryName = "nominativi"
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkbook = xlApp.Workbooks.Add
Set objRST = Application.CurrentDb.OpenRecordset(strQueryName)
Set xlSheet = xlWorkbook.Sheets(1)
xlSheet.Cells(1, 1).Value = "Risultati finali"
xlSheet.Cells(1, 1).Font.Color = RGB(255, 0, 0)
xlSheet.Range("A1:D1").Merge
'Selection.Columns.AutoFit
For lvlColumn = 0 To objRST.Fields.Count - 1
xlSheet.Cells(2, lvlColumn + 1).Value = _
objRST.Fields(lvlColumn).Name
Next
'Change the font to bold for the header row
xlSheet.Range(xlSheet.Cells(1, 1), _
xlSheet.Cells(2, objRST.Fields.Count)).Font.Bold = True
xlSheet.Range(xlSheet.Cells(1, 1), _
xlSheet.Cells(2, objRST.Fields.Count)).Font.Size = 10
xlSheet.Range(xlSheet.Cells(1, 1), _
xlSheet.Cells(2, objRST.Fields.Count)).HorizontalAlignment = xlCenter
'Add a border to header row cells
With xlSheet.Range(xlSheet.Cells(1, 1), _
xlSheet.Cells(2, objRST.Fields.Count)).Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlSheet.Range(xlSheet.Cells(1, 1), _
xlSheet.Cells(2, objRST.Fields.Count)).Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlSheet.Range(xlSheet.Cells(1, 1), _
xlSheet.Cells(2, objRST.Fields.Count)).Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlSheet.Range(xlSheet.Cells(1, 1), _
xlSheet.Cells(2, objRST.Fields.Count)).Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlSheet
.Range("A3").CopyFromRecordset objRST
.Name = Left(strQueryName, 31)
End With
xlSheet.Cells(18, 1).Value = "lavorativi"
xlSheet.Cells(19, 1).Value = "formativi"
xlSheet.Cells(20, 1).Value = "avvio impresa"
xlSheet.Cells(21, 1).Value = "altro"
xlSheet.Cells(22, 1).Value = "Totale"
lavorativi = xlApp.CountIf(Range("d3:d15"), "lavorativo")
xlSheet.Cells(18, 4).Value = lavorativi
formativi = xlApp.CountIf(Range("d3:d15"), "formativo")
xlSheet.Cells(19, 4).Value = formativi
avvioImpresa = xlApp.CountIf(Range("d3:d15"), "avvio impresa")
xlSheet.Cells(20, 4).Value = avvioImpresa
altro = xlApp.CountIf(Range("d3:d15"), "altro")
xlSheet.Cells(21, 4).Value = altro
totale = lavorativi + formativi + avvioImpresa + altro
xlSheet.Cells(22, 4).Value = totale
Set xlSheet = Nothing
Set xlWorkbook = Nothing
Set xlApp = Nothing
End Sub
Ho da porvi alcune domande: