+ Reply to Thread
Results 1 to 33 of 33

Macro operated by button clears worksheet but not executed successfully automatically

  1. #1
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Macro operated by button clears worksheet but not executed successfully automatically

    I have a macro that runs when I click a button on a worksheet to clear the contents. It works without fault.

    Please Login or Register  to view this content.
    I want it to also run when the worksheet is opened so that other users don't see the data when they open it up and it's empty, ready for them to use.

    So I tried to introduce code into the worksheet.

    Please Login or Register  to view this content.
    or

    Please Login or Register  to view this content.
    It does remove most of the data, but there are still SOME cells in column i and I don't understand why this is?

    If I click on the button and run the same code after the worksheet is opened, it clears the remaining cells (as it should). If I test the code in Microsoft Visual Basic for Applications, it runs without fault?
    Last edited by ThiaJay; 08-06-2020 at 05:33 AM.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2505 (Windows 11 Home 24H2 64-bit)
    Posts
    91,192

    Re: Strange behaviour

    Administrative Note:

    We would very much like to help you with your query, however the thread title does not really convey what your request is about. Tell us what the strange behaviour is.

    Please take a moment to amend your thread title. Make sure that the title properly explains your request. Your title should be explicit and not be generic (this includes function names used without an indication of what you are trying to achieve).

    Please see Forum Rule #1 about proper thread titles and adjust accordingly. To edit the thread title, open the original post to edit and then click on Go Advanced (bottom right) to access the area where you can edit your title.

    (Note: this change is not optional. No help to be offered until this moderation request has been fulfilled.)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Expert
    Join Date
    10-10-2016
    Location
    Sheffield
    MS-Off Ver
    365 and rarely 2016
    Posts
    3,212

    Re: Strange behaviour

    If it is possible, it would also be worth you attaching the file or a desensitised, representative smaller file that exhibits the same behaviour, at the same time as editing your title, you will get a quicker response then. Although no help till as Ali says the title has been changed

  4. #4
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Strange behaviour

    Title has been changed, I realise that would make life easier but there's too much sensitive data for me to share the workbook/even just the worksheet names and headings.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2505 (Windows 11 Home 24H2 64-bit)
    Posts
    91,192

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Thanks for changing the title - much better.

    Please read the instructions at the top of the page explaining how to create and share a SAMPLE workbook. If members feel they need to see your code in situ, you really need to try to facilitate this.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,015

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Just in case: an Auto_Open routine has to be in a normal module, not a worksheet code module, or it won't run. I'd also suggest using:

    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  7. #7
    Valued Forum Contributor
    Join Date
    08-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2007, 2010, 2013, 2016, Office 365
    Posts
    492

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Hi

    If your Reset1 macro works, why not use
    Please Login or Register  to view this content.
    ..and stick that in a normal module as Rory says.

    zeddy

  8. #8
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Legendary, thanks. It works!

    My concern is that if someone views one of the other worksheets and saves it, then someone else opens it on a different worksheet that it will delete all the data on the other worksheets? How do I avoid that?

  9. #9
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Ah, Rory already advised on that?

    Please Login or Register  to view this content.
    Last edited by ThiaJay; 08-06-2020 at 06:41 AM.

  10. #10
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    In the same way, except avoiding resetting the data, how do I make all worksheets except Search select A2 to force the view back to the top result on open? Is there a way to do that without specifying the name of each sheet? I don't want it to break if I add more sheets.

  11. #11
    Valued Forum Contributor
    Join Date
    08-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2007, 2010, 2013, 2016, Office 365
    Posts
    492

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Hi ThiaJay

    ..but your Reset1 macro already specifically refers to Worksheet("Search")
    ..so you don't need to do the With and End With malarkey.

    Unless of course you changed your Reset1 macro to..
    Please Login or Register  to view this content.
    ..in which case you would then do the With etc etc etc

    zeddy
    Excel Clearance Basement

  12. #12
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Sorry, you are right. It's safe!

    Please Login or Register  to view this content.
    Do you know about the other question I had above?

  13. #13
    Valued Forum Contributor
    Join Date
    08-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2007, 2010, 2013, 2016, Office 365
    Posts
    492

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Hi ThiaJay

    You could try this bit of code..
    Please Login or Register  to view this content.
    zeddy
    Excel Window Slider

  14. #14
    Valued Forum Contributor
    Join Date
    08-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2007, 2010, 2013, 2016, Office 365
    Posts
    492

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    ..so you would then use..

    Please Login or Register  to view this content.
    zeddy

  15. #15
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Thank you so much, it works perfectly of course!

  16. #16
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Spoke too soon, if the user switches to another worksheet other than Search and saves it, when it's next opened up it brings up an error.

    Run-time error '1004':

    Select method of Range class failed.

    It's pointing in debug to

    Please Login or Register  to view this content.

  17. #17
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2024
    Posts
    6,179

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Try this:
    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Thank you, but what about the images that aren't being removed now? You've commented it out and I'm not sure how to make it work.

    Please Login or Register  to view this content.
    Doesn't work
    Last edited by ThiaJay; 08-06-2020 at 08:56 AM.

  19. #19
    Valued Forum Contributor
    Join Date
    08-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2007, 2010, 2013, 2016, Office 365
    Posts
    492

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Hi ThiaJay

    You can't 'select' a cell unless you are on that particular sheet - hence the 'Select method of Range class failed' message if you aren't on the Worksheet("Search")

    So perhaps we could use this code:
    Please Login or Register  to view this content.
    Let me know if this works.
    If it doesn't, let me know anyway.

    zeddy

  20. #20
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Thanks zeddy, it seems to be working. I'll keep testing.

  21. #21
    Valued Forum Contributor
    Join Date
    08-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2007, 2010, 2013, 2016, Office 365
    Posts
    492

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Hi ThiaJay

    Always a pleasure to be of some service.
    Sometimes my help is valuable.
    Sometimes it is invaluable.

    zeddy

  22. #22
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    I've given you as much reputation as I can :D

  23. #23
    Valued Forum Contributor
    Join Date
    08-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2007, 2010, 2013, 2016, Office 365
    Posts
    492

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Hi ThiaJay

    ..nobody has ever done that for me on this Forum before - whopping thanks!

    zeddy

  24. #24
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    You are welcome

    Lastly, could you please confirm how I would exclude one other specific worksheet from the search called "Contents"?

  25. #25
    Valued Forum Contributor
    Join Date
    08-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2007, 2010, 2013, 2016, Office 365
    Posts
    492

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Hi ThiaJay

    There are lots of different ways to exclude multiple sheets from a particular loop-operation.

    For just a couple of sheets, you might just use..
    Please Login or Register  to view this content.
    Another way, which I like to use, is to get the tab-colour of the sheet (e.g. red, black etc etc) and skip the sheet if say, the tab colour is red etc etc etc

    You could also have a named-list of sheets-to-be-ignored, an array-list of sheets-to-be-ignored etc etc etc

    I'm sure we could provide more examples if you like.

    zeddy
    Excel Sheet Laundry Service

  26. #26
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Quote Originally Posted by zzzeddy View Post
    Hi ThiaJay

    There are lots of different ways to exclude multiple sheets from a particular loop-operation.

    For just a couple of sheets, you might just use..
    Please Login or Register  to view this content.
    Another way, which I like to use, is to get the tab-colour of the sheet (e.g. red, black etc etc) and skip the sheet if say, the tab colour is red etc etc etc

    You could also have a named-list of sheets-to-be-ignored, an array-list of sheets-to-be-ignored etc etc etc

    I'm sure we could provide more examples if you like.

    zeddy
    Excel Sheet Laundry Service
    Thanks Andy, I'm thinking this will work.

    Please Login or Register  to view this content.
    It's good to know about the other ways though, I didn't know you could do it based on tab colours. That could help me in the future.

  27. #27
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Ah, it didn't work as expected. I think this code needs to go within the search, as it's bring up content from the content page at the moment.

  28. #28
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Not sure how to integrate that into

    Please Login or Register  to view this content.

  29. #29
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Could you please get back to me?

  30. #30
    Valued Forum Contributor
    Join Date
    08-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2007, 2010, 2013, 2016, Office 365
    Posts
    492

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Hi

    Are you waiting for Andy???

    Is it possible to attach a sample file - it would help a lot
    Dummy data is OK

    zeddy

  31. #31
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Thanks for your reply, not particularly.

    I've attached a dummy workbook.
    Attached Files Attached Files
    Last edited by ThiaJay; 08-11-2020 at 12:01 PM.

  32. #32
    Valued Forum Contributor
    Join Date
    08-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2007, 2010, 2013, 2016, Office 365
    Posts
    492

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Hi ThiaJay

    In the post#26, (quote : Thanks Andy, I'm thinking this will work) your Sub MoveSelect() is missing an 'End If'
    You need to have and 'End If' for every 'If' you use, i.e. these need to be in 'pairs' for each worksheet you want to exclude.

    I made the change in the attached file, and it seems to work OK

    zeddy

  33. #33
    Forum Contributor
    Join Date
    11-08-2014
    Location
    UK
    MS-Off Ver
    Office 2013
    Posts
    208

    Re: Macro operated by button clears worksheet but not executed successfully automatically

    Thank you very much.

+ 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] strange behaviour from vba
    By harryv27 in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 04-14-2016, 04:50 AM
  2. [SOLVED] XIRR strange behaviour
    By phe in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 09-06-2013, 01:51 AM
  3. Strange Behaviour
    By Mark@Work in forum Excel General
    Replies: 2
    Last Post: 09-30-2008, 12:22 PM
  4. Strange if(***) behaviour?
    By Excel 2003 - SPB in forum Excel General
    Replies: 6
    Last Post: 08-06-2006, 12:35 PM
  5. [SOLVED] Strange VBA Behaviour
    By Ricko in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-28-2005, 03:05 AM
  6. Very strange add-in file behaviour
    By RB Smissaert in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 05-18-2005, 02:45 PM
  7. [SOLVED] Strange behaviour
    By Edgar Thoemmes in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-08-2005, 12:06 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