koolwaters
Active Member
- Joined
- May 16, 2007
- Messages
- 403
Hi!
I was not certain if I should use the Access or Excel forum for this post but I chose Access.
I have an Access database where data is exported on a weekly basis into three worksheets. The data is exported to tables in the Excel workbook. The tables are then used to generate a number of PivotTables. I opted to use Tables as the data source to make my PivotTables dynamic as new data is continuously being added to the database. Once the data is exported, I also have code in Access to refresh all of the PivotTables in the workbook. This process works perfectly.
On closing the Excel workbook,I have code which deletes the records from the tables on the three worksheets to ensure that when the data is next exported, no existing records are there. To this end, I have code in the Before Close event of the workbook to delete the table records.
This is where the problem comes. I have tried to save the file as .xlsx on export, but despite the file being saved in the .xlsx format, upon closing the macro runs.
This is the code I was using in Access to save the file.
I have done quite a bit of searching but have found nothing to assist with this issue.
I have tried copying the sheets to a new workbook after the template is opened and then saving the file but the file does not open. I get the message "Run-time error '1004': This extension cannot be used with the selected file type...
This is the code I am using in the Workbook Open event.
Any assistance is appreciated. Thanks in advance.
I was not certain if I should use the Access or Excel forum for this post but I chose Access.
I have an Access database where data is exported on a weekly basis into three worksheets. The data is exported to tables in the Excel workbook. The tables are then used to generate a number of PivotTables. I opted to use Tables as the data source to make my PivotTables dynamic as new data is continuously being added to the database. Once the data is exported, I also have code in Access to refresh all of the PivotTables in the workbook. This process works perfectly.
On closing the Excel workbook,I have code which deletes the records from the tables on the three worksheets to ensure that when the data is next exported, no existing records are there. To this end, I have code in the Before Close event of the workbook to delete the table records.
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error GoTo DataErr
Worksheets("New Applications").Activate
Range("Pending").Select
Application.DisplayAlerts = False
Selection.Delete
Application.DisplayAlerts = True
Range("A2").Select
Worksheets("Disbursements").Activate
Range("Disbursements").Select
Application.DisplayAlerts = False
Selection.Delete
Application.DisplayAlerts = True
Range("A2").Select
Worksheets("Outstanding").Activate
Range("Outstanding").Select
Application.DisplayAlerts = False
Selection.Delete
Application.DisplayAlerts = True
Worksheets("New Applications").Activate
Range("A2").Select
ThisWorkbook.Save
Exit Sub
DataErr_Exit:
Exit Sub
DataErr:
MsgBox "Error: (" & Err.Number & ") " & Err.Description, vbCritical
End Sub
This is where the problem comes. I have tried to save the file as .xlsx on export, but despite the file being saved in the .xlsx format, upon closing the macro runs.
This is the code I was using in Access to save the file.
Code:
stTitle = Format(myEndDate, "mmmm") & " Analysis Report"
stSaveName = stPath2 & "\" & stTitle & ".xlsx"
XlBook.SaveAs fileName:=stSaveName, FileFormat:=xlOpenXMLWorkbook
I have done quite a bit of searching but have found nothing to assist with this issue.
I have tried copying the sheets to a new workbook after the template is opened and then saving the file but the file does not open. I get the message "Run-time error '1004': This extension cannot be used with the selected file type...
This is the code I am using in the Workbook Open event.
Code:
Private Sub Workbook_Open()
Dim NewName As String, NewPath As String
Dim ws As Worksheet
NewPath = Path
If Right$(NewPath, 1) = "\" Then
NewPath = Left$(NewPath, Len(NewPath) - 1)
End If
NewPath = Left$(NewPath, InStrRev(NewPath, "\"))
With Application
.ScreenUpdating = False
On Error GoTo DataErr
Sheets(Array("New Applications", "Disbursements", "Outstanding", "Analysis")).Copy
On Error GoTo 0
For Each ws In ActiveWorkbook.Worksheets
ws.Cells.Copy
ws.[A1].PasteSpecial Paste:=xlValues
ws.Cells.Hyperlinks.Delete
Application.CutCopyMode = False
Cells(1, 1).Select
ws.Activate
Next ws
Cells(1, 1).Select
Worksheets("New Applications").Activate
Range("A4").Select
NewName = "Analysis Report"
ActiveWorkbook.SaveAs NewPath & NewName & ".xlsx"
.ScreenUpdating = True
End With
Exit Sub
DataErr:
MsgBox "Error: (" & Err.Number & ") " & Err.Description, vbCritical
End Sub
Any assistance is appreciated. Thanks in advance.