+ Reply to Thread
Results 1 to 32 of 32

How to add a line of VBA script to delete file?

  1. #1
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    How to add a line of VBA script to delete file?

    Dear All Expert in forum
    Have manage to list out the files after select a folder. However, if I will like to click a hyperlink and delete the file from Excel.
    May I know how can I add in the line?

    Thank you so much for any help. Will definitely choose "Solved" and click "Reputable" for anyone who help me solve this headache.
    Last edited by EvelynLoh; 11-08-2017 at 12:17 PM.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,619

    Re: How to add a line of VBA script to delete file?

    change to
    Please Login or Register  to view this content.
    and in your worksheet code (where Private Sub btnBrowse_Click() and other procedures reside) add:
    Please Login or Register  to view this content.

    PS. Why again without attachment ?
    Just by chance I remembered also your previous thread and used it as a reference here, but how about those who had no chance to participate in previous thread?
    Best Regards,

    Kaper

  3. #3
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Hi Kaper,

    Thank you so much for your help again. I try change the code but it just open and not delete. Any idea how to call the WorkSheet_FollowHyperlink sub routine?

    Thank you so much..
    Last edited by EvelynLoh; 11-06-2017 at 03:52 AM.

  4. #4
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Hi Kaper,

    Any update please on above question as just abit more... Please help me.

    Thank you very much and hope to hear from you soon.

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,619

    Re: How to add a line of VBA script to delete file?

    Quote Originally Posted by EvelynLoh View Post
    Any idea how to call the WorkSheet_FollowHyperlink sub routine?
    This is Event handler for clicking hyperlink.

    So to call it - just click on hyperlink.

    This is how I interpreted what you asked in thread-opening post: "if I will like to click a hyperlink and delete the file from Excel."

  6. #6
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Thank you so much for your reply Kaper. But when I click on the link, it open the file instead of delete. Any idea how do I attach the sub routine to the hyperlink?

    Sorry for the question... Thank you very much.

  7. #7
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,619

    Re: How to add a line of VBA script to delete file?

    Not instead, but it does both: opens and deletes.

    As I dont know how to cancel opening in this procedure, so may be another idea:
    delete followHyperlink code from worksheet code and add:

    Please Login or Register  to view this content.
    And in TextToDisplay for column I use not Click Here to Delete but:
    Please Login or Register  to view this content.

  8. #8
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Hi Kaper,

    Thank you so much for your help. It really works now. Appreciate your help.

    Thank you for save my life...

  9. #9
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,619

    Re: How to add a line of VBA script to delete file?

    Saving life is for sure exaggerated
    But I'm glad I could help
    And thanks for marking thread solved and for reputation point

  10. #10
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Hi Kaper,

    Sad to say, this function still not solve..

    We notice that some of the file can't be deleted. Not sure why though.. It just say "File not found" though the file is there.
    Any idea why?? Stress up again as only this function is not working properly. Do you think is due to the script is wrong somewhere else why it says files not found but when click to open, can view but can't delete.

    Please Login or Register  to view this content.
    For the code that can click to open file .. it is stated below.

    Please Login or Register  to view this content.
    PLEASE HELP ME!!.... beg beg.. Thank you.

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: How to add a line of VBA script to delete file?

    Does the hyperlink of the files that can't be deleted have fullpath in it's address ?
    eg D:\whateverfile
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  12. #12
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: How to add a line of VBA script to delete file?

    Suddenly solved ??????

  13. #13
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Yes else it can't even open.. the path is there..
    Now the problem is it can open to see but can't delete when right click.. as it says files not found..

  14. #14
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100
    Not solve yet..;( pls advise ...thk u

  15. #15
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: How to add a line of VBA script to delete file?

    Kill doesn't delete read-only files. Is this the case ?
    If so you need to change the attribute using SetAttr.

  16. #16
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,619

    Re: How to add a line of VBA script to delete file?

    You could try it this way - also other attributes are tested (but I agree with bakerman2 that read-only is quite probable)
    Please Login or Register  to view this content.

  17. #17
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Hi...

    But some is shortcut also can't be deleted... Any idea how to change the attribute?

    Really head big big now... thank you so much.

  18. #18
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Hi Kaper and bakerman,

    After deep investigation, just realise that is due to the path of the sign is change for backslash to Yen sign.

    Any idea how to tell Excel to change all Yen sign to back slash so can delete the file successfully?

    Thank you so much.
    Last edited by EvelynLoh; 12-29-2017 at 09:41 PM.

  19. #19
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Thank you very much...
    Last edited by EvelynLoh; 01-05-2018 at 04:11 AM.

  20. #20
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: How to add a line of VBA script to delete file?

    Maybe you can try this.

    Please Login or Register  to view this content.

  21. #21
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Thank you bakerman2

    Sadly still doesn't work as first time can delete but after I close the file and open and try to delete again. It says file not found again.

    Any idea how to resolve the hyperlink issue so can find the file to delete?

    Thank you soo much guys for your advise.
    Last edited by EvelynLoh; 01-05-2018 at 04:11 AM.

  22. #22
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Any help please.........

  23. #23
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: How to add a line of VBA script to delete file?

    Sadly still doesn't work as first time can delete but after I close the file and open and try to delete again. It says file not found again.
    In one of your codes the row with the link is also removed after deleting the file so how can you try again to delete ???????

  24. #24
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Hi bakerman

    Sorry you mean below code? As I need to delete the row also else is confuse if the file is no longer there but row still shows.
    Sorry as have limited knowledge on Excel. How can I change the code so that can delete successfully everytime when goes in?
    Thank you very much.
    Last edited by EvelynLoh; 01-05-2018 at 04:11 AM.

  25. #25
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: How to add a line of VBA script to delete file?

    Yes, otherwise link remains to none existing file.

  26. #26
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Thank you bakerman,

    May I know is there any way to delete the file successfully as well as remove the row in Excel after file deleted instantly?

    How should I improve the script? Hope to hear from you soon.

    Thank you.

  27. #27
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: How to add a line of VBA script to delete file?

    Your code in post#24 does that.

  28. #28
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Hi Bakerman2

    Yes am aware... But how should I able to kill the file without using Explorer if don't use this formula?

    Also, please advise how can I improve the script so that can delete file upon right click and also delete the row away without remove all the file's hyperlink?

    Please advise?

    Thank you.

  29. #29
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: How to add a line of VBA script to delete file?

    In post#8 you say this works but some files can't be deleted due to path.
    So after amendement regarding path this should work also.

    Please Login or Register  to view this content.
    If this doesn't work for you then I have absolutely no idea anymore of what it is you're after.
    This thread has become so confusing I just can't tell anymore what is what.
    Last edited by bakerman2; 12-21-2017 at 02:44 AM.

  30. #30
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Hi Bakerman2

    I have use exact same script you stated here but as usual, once I download the file, I can delete any files successfully. But once I save the Form, close and open again.. I can't delete anymore. It just stated "File not Found"... :-(
    Last edited by EvelynLoh; 01-05-2018 at 04:10 AM.

  31. #31
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Hi Bakerman2,
    I really appreciate your help too..

    Thank you very much..
    Last edited by EvelynLoh; 01-05-2018 at 04:10 AM.

  32. #32
    Forum Contributor
    Join Date
    11-07-2013
    Location
    Singapore
    MS-Off Ver
    Excel 2007
    Posts
    100

    Re: How to add a line of VBA script to delete file?

    Hi Kaper


    Can you please help me on my bug on why it can't be deleted pls?
    Last edited by EvelynLoh; 01-05-2018 at 04:10 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. script to run the macro from Vb script with out opening the excel file
    By chandanp in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-27-2021, 03:33 AM
  2. How to change this line of script?
    By terrypin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-28-2017, 09:32 AM
  3. Macro delete trailing spaces (1 million line file)
    By Ayadin in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 08-11-2016, 04:41 AM
  4. Open TXT file Delete and first 5 characters from 5th line
    By Tellm in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-18-2015, 05:14 PM
  5. [SOLVED] Script to save to folder in "v:\dept2", confirm file saved, then delete from "v:\dept1"
    By ks100 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-28-2014, 11:42 AM
  6. [SOLVED] command line Excel Script
    By [email protected] in forum Excel General
    Replies: 0
    Last Post: 08-22-2006, 08:30 AM
  7. which line mercy script
    By nastech in forum Excel General
    Replies: 6
    Last Post: 02-05-2006, 12:50 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1