• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Problem with excel macro (VBA)

Dear all,

I got the following VBA code whick opens a folders, checks whether a file is there and then export an excel file from access in the map. All works fine except that the macro does not work if there's no file in the map.

Does anybody know how I can enhance code below so it
- checks whether there is a file

if yes - delete and execute rest
if no - just execute macro

Public Function ExportDashboardExcel()

Kill "X:\SSC_HR\SENS\Bedrijfsbureau\Rapportages\sterren\MAANDELIJKSE RAPPORTAGES\UITDRAAI DB_MAANDELIJKS_DASHBOARD\*.xls"
MsgBox ("De bestanden zijn gedelete")

Const FILE_PATH As String = "X:\SSC_HR\SENS\Bedrijfsbureau\Rapportages\Sterren\MAANDELIJKSE RAPPORTAGES\UITDRAAI DB_MAANDELIJKS_DASHBOARD\"
Dim FullPath As String
Dim bestandsnaam As String
Dim datum As String, tijdstempel As String

strFullPath = FILE_PATH
datum = Date

tijdstempel = Format(datum, "d-m-yyyy")
bestandsnaam = "Dashboard " & tijdstempel & ".xls"

DoCmd.TransferSpreadsheet acExport, , "Dashboard", strFullPath & bestandsnaam, False
MsgBox ("De bestanden staan er")

End Function
 
Try this..
Code:
Public Function ExportDashboardExcel()

'----
'Modified Section
'----
If Dir("X:\SSC_HR\SENS\Bedrijfsbureau\Rapportages\sterren\MAANDELIJKSE RAPPORTAGES\UITDRAAI DB_MAANDELIJKS_DASHBOARD\*.xls") <> "" Then
  Kill "X:\SSC_HR\SENS\Bedrijfsbureau\Rapportages\sterren\MAANDELIJKSE RAPPORTAGES\UITDRAAI DB_MAANDELIJKS_DASHBOARD\*.xls"
  MsgBox ("De bestanden zijn gedelete")
End If
'----
Const FILE_PATH As String = "X:\SSC_HR\SENS\Bedrijfsbureau\Rapportages\Sterren\MAANDELIJKSE RAPPORTAGES\UITDRAAI DB_MAANDELIJKS_DASHBOARD\"
Dim FullPath As String
Dim bestandsnaam As String
Dim datum As String, tijdstempel As String

strFullPath = FILE_PATH
datum = Date

tijdstempel = Format(datum, "d-m-yyyy")
bestandsnaam = "Dashboard " & tijdstempel & ".xls"

DoCmd.TransferSpreadsheet acExport, , "Dashboard", strFullPath & bestandsnaam, False
MsgBox ("De bestanden staan er")

End Function
 
Hi, marcvanderpeet12!

Another (shorter) option is to embed the Kill statement between these 2:
On Error Resume Next
On Error Goto 0

Regards!
 
Back
Top