Workaround DIR() limitation

aggiemarine07

New Member
Joined
Nov 5, 2013
Messages
46
Ok so I am running into a problem with using the below code since I started storing it on OneDrive. Basically, what the code is supposed to do is copy the existing worksheet to a new workbook and then save the workbook; prior to this though it is supposed to delete any previous/existing version of the file prior to creating the new workbook. My problem is that the code errors out on the
VBA Code:
If Len(Dir$(strFile)) > 0 Then Kill strFile
line of the code

VBA Code:
Sub Macro1()

mypath = Application.ActiveWorkbook.Path
'delete previous weeks sheet
Dim strFile  As String: strFile = Application.ActiveWorkbook.Path & "\" & Mid(FileOnly, 1, InStr(FileOnly, ".") - 1) & " " & Format(Date - 7, "YYYY.MM.DD") & ".xlsb"
If Len(Dir$(strFile)) > 0 Then Kill strFile
ThisWorkbook.Sheets("Allocations").Copy
ActiveWorkbook.SaveAs mypath & "\" & Mid(FileOnly, 1, InStr(FileOnly, ".") - 1) & " " & Format(Date, "YYYY.MM.DD") & ".xlsb", FileFormat:=50

End Sub

I apologize in advance if this is a duplicate and/or has been answered elsewhere but I have searched Google and other forums but have not found a good answer, so maybe I am searching with the wrong keywords. Thanks!
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
What data do you have in this variable: "FileOnly"? I guess it's the name of Thisworkbook

It is not necessary to delete the file previously, just put this instruction "Application.DisplayAlerts = False" at the beginning of your macro

Try this:

VBA Code:
Sub Macro1()
  Dim mypath As String, strFile As String, FileOnly
  Application.DisplayAlerts = False
 
  mypath = Application.ActiveWorkbook.Path
  FileOnly = ThisWorkbook.Name
  strFile = mypath & "\" & Mid(FileOnly, 1, InStr(FileOnly, ".") - 1) & " " & Format(Date - 7, "YYYY.MM.DD") & ".xlsb"
  ThisWorkbook.Sheets("Allocations").Copy
  ActiveWorkbook.SaveAs strFile, FileFormat:=50
End Sub
 
Upvote 0
If you also want to delete the file, then it would be like this:

VBA Code:
Sub Macro1()
  Dim mypath As String, strFile As String, FileOnly As String
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
  
  mypath = Application.ActiveWorkbook.Path
  FileOnly = ThisWorkbook.Name
  strFile = mypath & "\" & Mid(FileOnly, 1, InStr(FileOnly, ".") - 1) & " " & Format(Date - 7, "YYYY.MM.DD") & ".xlsb"
  If Len(Dir$(strFile)) > 0 Then Kill strFile
  ThisWorkbook.Sheets("Allocations").Copy
  ActiveWorkbook.SaveAs strFile, FileFormat:=50
  ActiveWorkbook.Close False
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top