+ Reply to Thread
Results 1 to 13 of 13

Macro to delete content from same range on multiple worksheets

  1. #1
    Registered User
    Join Date
    09-05-2019
    Location
    United Kingdom
    MS-Off Ver
    Office for Mac
    Posts
    12

    Macro to delete content from same range on multiple worksheets

    I have a spreadsheet with 13 sheets worth of information relating to a transport company's employee KPIs. It is all more or less finished the only thing I need to work out is how to set up a button to delete the content in range B3:AZ159 across all 13 worksheets.

    This button should only be pressed after all the data has been collated so I have been asked if there is a way to implement a password input to stop it being used by mistake. Or at the very least one or two "are you sure" style alerts before the script is triggered.

    Thanks in advance.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,409

    Re: Macro to delete content from same range on multiple worksheets

    You could use a MsgBox to ask for confirmation or you can use an InputBox to request a password. Of course, if you want a password to be input, you'll need to store it somewhere (in the code or on a worksheet) so you can check it.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    07-21-2019
    Location
    Italy
    MS-Off Ver
    2016
    Posts
    58

    Re: Macro to delete content from same range on multiple worksheets

    Hi,
    try this
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    09-05-2019
    Location
    United Kingdom
    MS-Off Ver
    Office for Mac
    Posts
    12

    Re: Macro to delete content from same range on multiple worksheets

    Thanks MrGes.

    Where would you put that code exactly? In each worksheet it refers to? Or do I need to specify which worksheets within the code somewhere?

  5. #5
    Registered User
    Join Date
    09-05-2019
    Location
    United Kingdom
    MS-Off Ver
    Office for Mac
    Posts
    12

    Re: Macro to delete content from same range on multiple worksheets

    That sounds like a good idea TMS. Being new to VBA though I wouldn't have a clue where to begin with writing that code though

    Do you have a starting point for me to work on?

    Thanks for your reply.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,409

    Re: Macro to delete content from same range on multiple worksheets

    MrGes's code asks once to confirm that you want to clear the cells. If you say "yes", it loops through all the sheets and clears the range. Apply that code to your button, or call it from your button code as a free standing subroutine.

  7. #7
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Macro to delete content from same range on multiple worksheets

    Quote Originally Posted by MrGes View Post
    Hi,
    try this
    Please Login or Register  to view this content.
    You can do your macro without using a loop...
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    09-05-2019
    Location
    United Kingdom
    MS-Off Ver
    Office for Mac
    Posts
    12

    Re: Macro to delete content from same range on multiple worksheets

    Thanks Rick.

    So there are a few sheets where I do not want the content to be cleared such as a reporting page. Is there a way to specify to clear content (and comments) from all worksheets with the word "week" in the name?

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,409

    Re: Macro to delete content from same range on multiple worksheets

    You would have to use the loop for that and check the sheet name.

  10. #10
    Registered User
    Join Date
    09-05-2019
    Location
    United Kingdom
    MS-Off Ver
    Office for Mac
    Posts
    12

    Re: Macro to delete content from same range on multiple worksheets

    So I am still having trouble with this.

    I have tried the following code to specify any worksheet beginning with "Week" to clear content and comments in range B3:AZ152 and have assigned it to a button on my reporting page (which I want to be disregarded by the script) which when pressed ONLY clears the content on my reporting page and not the "Week" sheets as expected. Any Ideas? I haven't yet managed to add an alert to ask if you are sure you want to do this as I want to make sure it does what it is supposed to. Thanks

    Sub ClearAll()

    Dim ws As Worksheet
    Set Rng = Range("B3:AZ152")

    For Each ws In ActiveWorkbook.Sheets

    If ws.Name Like "Week *" Then
    Rng.Select
    Selection.ClearContents
    Selection.ClearComments
    Else: If Not ws.Name Like "Week *" Then Exit Sub

    End If

    Next ws


    End Sub

  11. #11
    Registered User
    Join Date
    09-05-2019
    Location
    United Kingdom
    MS-Off Ver
    Office for Mac
    Posts
    12

    Re: Macro to delete content from same range on multiple worksheets

    Okay I think I am making progress. I am now using the following code.

    Sub ClearAll()

    Dim ws As Worksheet

    mymsg = MsgBox("Are you sure? This content cannot be recovered once cleared.", vbYesNo)
    If mymsg = vbYes Then
    For Each ws In Worksheets

    If ws.Name Like "Week *" Then
    ws.Range("B3:AZ159").ClearContents
    ws.Range("B3:AZ159").ClearComments


    End If


    Next ws

    End If

    End Sub



    The problem now is that the "Week" sheets are protected so as to avoid any inadvertent deletions of other unrelated formulas. I know there is a way to bypass this but I am not sure how. The sheets have all been set to allow objects to be edited but what needs putting into the code?

    Thanks

  12. #12
    Registered User
    Join Date
    09-05-2019
    Location
    United Kingdom
    MS-Off Ver
    Office for Mac
    Posts
    12

    Re: Macro to delete content from same range on multiple worksheets

    Fixed it. Certain cells were still locked. Thanks for your help all.

  13. #13
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,409

    Re: Macro to delete content from same range on multiple worksheets

    You're welcome.

+ 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. [SOLVED] Macro to Clear Content Across Multiple Worksheets
    By Shawn1888 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-03-2018, 01:58 AM
  2. Need Macro that clears data from cell but does not delete the content
    By rscottland in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2017, 11:27 PM
  3. [SOLVED] Macro to check and delete content of cell
    By kent97 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-11-2015, 10:58 PM
  4. [SOLVED] VBA Macro : copy cell content between worksheets
    By giovanni18 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-30-2013, 02:36 PM
  5. How to examine the content of a range and to delete a cell below a value found
    By sundial in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-21-2013, 04:03 PM
  6. [SOLVED] Macro to delete rows if any cell contains specific content
    By adayoan in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-31-2012, 07:11 PM
  7. Auto delete cell content for specified data range
    By RumanaM in forum Excel General
    Replies: 0
    Last Post: 05-15-2007, 04:21 AM

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