Delete A .xlsm File With Variables

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
245
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am trying to come up with some code to delete a file in a location that I know exists as the file creates this before the need to delete the file, the filename is created and is store in cell i1 - see the code I have below unfortunetely I keep getting an error. If anyone can help it would be greatly appreciated.

VBA Code:
Sub KillXLSM()

Application.DisplayAlerts = False

Dim usrnme As String

'Get username
usrnme = Environ("username")

Sheets("123").Visible = True
Sheets("123").Select

    ActiveSheet.Unprotect
            
Dim myFolderName As String
    Dim myFileName As String
    
'   Build folder name
    myFolderName = Environ("userprofile") & "\Desktop\123 Submission Docs\"


'   Build file name
    myFileName = Range("i1") & ".xlsm"
    

    ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False

ActiveWindow.SelectedSheets.Visible = False

'   Kill file
    Kill FileName:=myFolderName & myFileName

Application.DisplayAlerts = True

End Sub


Any help can provide would be greatly appreciated.

Regards
Steve
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
What is the error, on which line does it occur, and have you verified the correct values are in your variables?
 
Upvote 0
Solved

VBA Code:
Sub KillXLSM()

Application.DisplayAlerts = False

Dim usrnme1 As String

'Get username
usrnme1 = Environ("username")

Sheets("321").Visible = True
Sheets("321").Select
   
Dim myFolderName1 As String
    Dim myFileName1 As String
   
'   Build folder name
                                  myFolderName1 = Environ("userprofile") & "\Desktop\123\"

'   Build file name
                                  myFileName1 = myFolderName1 & Range("i1") & ".xlsm"

ActiveWindow.SelectedSheets.Visible = False

Sheets("987").Select

'   Kill file
                                   Kill (myFileName1

Application.DisplayAlerts = True


End Sub
 
Upvote 0

Forum statistics

Threads
1,215,635
Messages
6,125,945
Members
449,275
Latest member
jacob_mcbride

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