benjamin.grimm
Registered User.
- Local time
- Yesterday, 20:20
- Joined
- Sep 3, 2013
- Messages
- 125
Hey guys,
i have the following code:
The code exports data to a fix Excel sheet from a querry.
The querry has the following elements
Here is my code:
When i name the Excel Sheets by SuWID, the code works perfectly well.
If i want to name the Excel Sheets by SAP_Nummer. It doesnt work. Why.
Here the problem Zone of the code:
This works:
This doesnt work:
There i get a run time-error 3265. (element is not part of the list)
Hmm, but it is part of the querry.
Does someone has an idea?
i have the following code:
The code exports data to a fix Excel sheet from a querry.
The querry has the following elements
- SAP_Nummer
- SAP
- Geris
- Pauschale
- SuWID
- Jahr_Y
Here is my code:
Code:
Private Sub Befehl5_Click()
Dim xlApp As Object 'Excel.Application
Dim xlBook As Object 'Excel.Workbook
Dim xlSheet As Object 'Excel.Worksheet
Dim rstID As DAO.Recordset, tmpStr As String
Dim rstGr As DAO.Recordset, strSQL As String
Dim Dateipfad As String
Dim dlg As FileDialog
Dim strPfad As String
strSQL = "SELECT SuWID FROM Abfrage_alles GROUP BY SuWID;"
Set rstID = CurrentDb.OpenRecordset(strSQL)
If rstID.RecordCount > 0 Then
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set dlg = Application.FileDialog(msoFileDialogOpen)
'Titelzeile
dlg.Title = "Bitte geben Sie den Pfad der Exceldatei!"
'standardpfad
dlg.InitialFileName = "C:\"
'Button text
dlg.ButtonName = "speichern"
dlg.Filters.Clear
dlg.Filters.Add "Excel", "*.xlsm"
If dlg.show Then
'wenn Exceldatei angegeben wurde
Dateipfad = dlg.SelectedItems(1)
Set xlBook = xlApp.Workbooks.Open(Dateipfad)
Set xlSheet = xlBook.Sheets("Tabelle1")
xlSheet.Range("A2:E14").ClearContents
Do While Not rstID.EOF
Set xlSheet = xlBook.Sheets("Tabelle" & rstID.Fields("SuWID"))
xlSheet.Name = "" & rstID.Fields("SuWID")
Set rstGr = CurrentDb.OpenRecordset("SELECT SAP, Geris, Pauschale, SAP_Nummer, Jahr_Y FROM Abfrage_alles WHERE SuWID = " & rstID.Fields("SuWID"))
xlSheet.Range("A2").CopyFromRecordset rstGr
rstGr.Close
rstID.MoveNext
Loop
Else
MsgBox "No information to export", vbInformation, "No data exported"
End If
End If
rstID.Close
Set rstID = Nothing
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
When i name the Excel Sheets by SuWID, the code works perfectly well.
If i want to name the Excel Sheets by SAP_Nummer. It doesnt work. Why.
Here the problem Zone of the code:
This works:
Code:
Do While Not rstID.EOF
Set xlSheet = xlBook.Sheets("Tabelle" & rstID.Fields("SuWID"))
xlSheet.Name = "" & rstID.Fields("SuWID")
This doesnt work:
Code:
Do While Not rstID.EOF
Set xlSheet = xlBook.Sheets("Tabelle" & rstID.Fields("SAP_Nummer"))
xlSheet.Name = "" & rstID.Fields("SAP_Nummer")
There i get a run time-error 3265. (element is not part of the list)
Hmm, but it is part of the querry.
Does someone has an idea?