|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jul 2007
Messaggi: 1092
|
[excel vba] aggiungere fogli di lavoro
Ciao a tutti. La mia necessità è quella di aggiungere dei fogli ad un file di excel tramite vba. Dovrò creare un foglio per ciascuna tabella presente in un mio database access ma qui posto un codice semplificato giusto per rendere l'idea. Finchè importo i dati di sole 3 tabelle funziona bene. I problemi sorgono quando devo aggiungere i fogli.
Di seguito il mio codice semplificato al massimo: Codice:
Option Compare Database
Private Sub Comando0_Click()
Dim dbs As Database
Dim strSQL As String
Dim strQueryName As String
Dim qryDef As QueryDef
Dim xlApp As Excel.Application
Dim xlSheet As Excel.Worksheet
Dim xlWorkbook As Excel.Workbook
Dim objRST As Recordset
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlWorkbook = xlApp.Workbooks.Add
' Foglio 1 ****************************************************************
Set xlSheet = xlWorkbook.Sheets(1)
With xlSheet
.Range("A1").Value = "valore1"
End With
' Rinomino il foglio di lavoro di excel
xlSheet.Name = "Tabella_uno"
' Fine foglio 1 **************************************************************
'Foglio 4 *********************************************************************
Set xlSheet = Excel.Worksheets.Add(After:=Sheets(Sheets.Count))
'Altre prove fatte
'Set xlSheet = Worksheets.Add(After:=Sheets(Sheets.Count))
'Set xlSheet = Sheets.Add(After:=Sheets(Sheets.Count))
' ActiveSheet.Name = "Tabella_quattro"
With xlSheet
.Range("A1").Value = "valore4"
End With
' Rinomino il foglio di lavoro di excel
xlSheet.Name = "Tabella_quattro"
' Fine foglio 4 *************************************************************
' Libero le risorse
Set xlSheet = Nothing
Set xlWorkbook = Nothing
Set xlApp = Nothing
End Sub
Come posso fare in modo che il codice funzioni sempre? Grazie a tutti fin da ora. Ultima modifica di john_revelator : 02-02-2008 alle 16:55. |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 07:58.



















