+ Reply to Thread
Results 1 to 41 of 41

VBA to Clear contents from a range if text appears in a certain cell

  1. #1
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    VBA to Clear contents from a range if text appears in a certain cell

    hi,

    I am looking to clear the contents of E120:K152 if the word STOP appears in cell G1

    I need to apply this to 60 seperate sheets
    Thanks for any help
    Paul

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: VBA to Clear contents from a range if text appears in a certain cell

    Something like this...All 60 sheets in same workbook?
    Please Login or Register  to view this content.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    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,431

    Re: VBA to Clear contents from a range if text appears in a certain cell

    Try:

    Please Login or Register  to view this content.
    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


  4. #4
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    thank you for your response ... yes all 60 in the same workbook ... but there are other sheets in the workbook that I would not want this to apply to, so does that mean that I have to place this code in each sheet ?
    cheers
    Paul

  5. #5
    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,431

    Re: VBA to Clear contents from a range if text appears in a certain cell

    No, the code, in both examples, loops through every sheet in the workbook. You would need to specifically include the worksheets you want tested or exclude those that you don't. Assuming there are less that you want to exclude, you'd need to create a list of them in the code, or on a worksheet.

    That said, if you don't have STOP in cell G51 on the sheets, then the macros don't change anything.

  6. #6
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: VBA to Clear contents from a range if text appears in a certain cell

    No the sheets need to be excluded like this...
    Please Login or Register  to view this content.

  7. #7
    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,431

    Re: VBA to Clear contents from a range if text appears in a certain cell

    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    thanks to you both for your help, much appreciated
    Regards
    Paul

  9. #9
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: VBA to Clear contents from a range if text appears in a certain cell

    Glad I could contribute...Tx for rep +

  10. #10
    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,431

    Re: VBA to Clear contents from a range if text appears in a certain cell

    You're welcome. Thanks for the rep.

  11. #11
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    I put the code as follows into a Module ..

    Please Login or Register  to view this content.
    but it has caused a problem with other code I have in another module as follows ..

    Please Login or Register  to view this content.

    Have I put it in the wrong place or is the clash inevitable
    Thanks again ..
    Last edited by sherman51; 01-17-2018 at 10:57 AM. Reason: has caused a problem with other code

  12. #12
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: VBA to Clear contents from a range if text appears in a certain cell

    Your post does not comply with rule #3.
    3. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing.
    So, do the following pls:
    Edit your post, highlight your code and click the [#] button at the top of the post window.

    has caused a problem with other code
    Other code in same module....Could be because you have not declared variables...
    Last edited by sintek; 01-17-2018 at 10:37 AM.

  13. #13
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: VBA to Clear contents from a range if text appears in a certain cell

    ....deleted....

  14. #14
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    i tried that, but seems to have made no diference to the post, sorry I don't know what else to do ..
    Cheers
    Paul

  15. #15
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: VBA to Clear contents from a range if text appears in a certain cell

    Tried what...I asked that you edit Post 11
    So, do the following pls:
    Go to Post 11 and Edit your post by highlighting your code and click the [#] button at the top of the post window.

  16. #16
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA to Clear contents from a range if text appears in a certain cell

    The only problem I can see is you haven't declared DataSht, DestSht or Cntr.

    Add this at the top of the module just below Option Explicit.

    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  17. #17
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    thats what i did .. shall i try to re-post ?

  18. #18
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: VBA to Clear contents from a range if text appears in a certain cell

    As per post 12...
    Could be because you have not declared variables...
    Do what Norie suggests in Post 16 and will solve...

  19. #19
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    got it now thanks .. both start and end codes were going in together, my apologies ..

  20. #20
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    will try that, thanks again guys ..

  21. #21
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    I added the lines posted by Norie (post 16) but I still had the clash with other vba problem, also the "clear cell range " vba did not work .

    I then tried the code posted by TMS (post3), this does not cause a problem with other vba but does not clear the cell range either ..

    Any further advice would be much appreciated ..
    Paul

  22. #22
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA to Clear contents from a range if text appears in a certain cell

    Paul

    Any chance you could upload a sample workbook?

    Click on GO ADVANCED, scroll down and click Manage Attachments.

  23. #23
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    i'd be happy to copy all the codes if that would help.. not sure that posting the workbook would help as there is a procedure to get teh links up and running from the source (which you would need to be subscribed to) in order to see if there's a clash ..

    would that help ?
    Thanks
    Paul

  24. #24
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: VBA to Clear contents from a range if text appears in a certain cell

    Codes won't help...Just out of curiosity...Where did you put norie's declare code...

    Edit...
    but it has caused a problem with other code
    You are still to tell us what problem it causes...error Message ?

  25. #25
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    i'd be happy to copy all the codes if that would help.. not sure that posting the workbook would help as there is a procedure to get teh links up and running from the source (which you would need to be subscribed to) in order to see if there's a clash ..

    would that help ?
    Thanks
    Paul

  26. #26
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    Please Login or Register  to view this content.
    changed "Stop" to In-Play and excluded the sheets that didn't need the code ...

  27. #27
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: VBA to Clear contents from a range if text appears in a certain cell

    There's your problem...
    Norie's declare statements...
    Should go below...Your other Module...the one causing the problems...
    Please Login or Register  to view this content.
    Last edited by sintek; 01-17-2018 at 02:22 PM.

  28. #28
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    "In-Play" turned out to be the text and not Stop .. or can you see something other ? ..

  29. #29
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA to Clear contents from a range if text appears in a certain cell

    Sintek

    The declarations should be at the top of the module, not in a sub, because they are used in two subs, TimePaste and Setup - see post #11

    Not ideal I know but the code would need to be rewritten to avoid that.

  30. #30
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: VBA to Clear contents from a range if text appears in a certain cell

    @ Norie

    Sorry did not notice that...Still a problem though...See post #26
    Wrong module perhaps...
    Public Declare?

  31. #31
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA to Clear contents from a range if text appears in a certain cell

    sintek

    To be honest I'm not actually sure what the problem is, the declarations don't actually have anything to do with the sub named Clear.

    I think it might help to see all the code, the error message(s) and/or a sample workbook.

  32. #32
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,231

    Re: VBA to Clear contents from a range if text appears in a certain cell

    Yeah true Norie

    OP is still to tell us what the errors are... So come on now Sherman51...get with the program...

  33. #33
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    thanks for your help so far guys, I now seem to have another problem, when I open the macros to view/edit, I can't do anything with them, just keep getting a sound/ding .. I'll have to get back to a working copy and start again ..
    I actually have to go shortly and so will pick it up tomorrow if thats ok ....
    Thanks again
    Paul

  34. #34
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    This is all the vba code in the workbook ..

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    hope this helps ...
    . and there are a couple of macros that I have recorded for copying and sorting ..
    when teh vba hangs, i hit de-bug and the purple text above is what's highlited ..

    Paul
    Last edited by sherman51; 01-17-2018 at 03:29 PM.

  35. #35
    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,431

    Re: VBA to Clear contents from a range if text appears in a certain cell

    You have two Workbook_Open routines. That can't be right.

    When it hangs and you Debug it, what values do the public variables have?

    Not sure if the value of having a separate Setup routine.

  36. #36
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    sorry, not sure what the public variables are .. teh workbook runs fine, it was only when I addes todays code that there was a problem, and when i hit de-bug it highlited the code I made purple in teh above post (34) ..

  37. #37
    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,431

    Re: VBA to Clear contents from a range if text appears in a certain cell

    These are the public variables

    Please Login or Register  to view this content.
    We're all guessing here. Please post the workbook so we can see it in context.

  38. #38
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA to Clear contents from a range if text appears in a certain cell

    Any chance you could upload a sample workbook?

    Click on GO ADVANCED, scroll down and click Manage Attachments.

  39. #39
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    Hi guys, apologies for the delay, ... life's distractions ..
    I have found that this code works if I manually type into cell G1, but it dosent work when cell G1 changes from blank to having text in it ...
    Is there a difference in a cell being "updated from an outside source" and manualy typing in a cell? and can I get around it ?
    thanks
    Paul

    Please Login or Register  to view this content.

  40. #40
    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,431

    Re: VBA to Clear contents from a range if text appears in a certain cell

    Well, two things. 1) you're using a Selection Change Event handler so you only have to select a/any cell and it will fire; you don't have to type anything. And 2) a Change Event handler only fires if you manually change a cell or you use code to change the value of a cell; it doesn't fire when the value of a formula changes.

  41. #41
    Registered User
    Join Date
    08-26-2015
    Location
    Staffordshire UK
    MS-Off Ver
    2007
    Posts
    98

    Re: VBA to Clear contents from a range if text appears in a certain cell

    ok, thank you for your reply ...

+ 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. Clear merged cell contents in comand button(clear all)
    By mohan_984 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-25-2015, 10:39 AM
  2. [SOLVED] If each cell in range equals 0 then clear contents of range and move to the next row
    By dagardner in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-19-2014, 08:44 PM
  3. [SOLVED] Clear contents of last used cell in a range and those previous to it (excel 2003)
    By chris_norton in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-17-2012, 02:06 PM
  4. Clear cell after message box appears
    By shivrklfc in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-16-2012, 11:34 AM
  5. [SOLVED] How do I clear contents from a range excluding formulas and text?
    By Normezo in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-23-2012, 10:36 AM
  6. Macro that will clear contents of cell based on format of text in adjacent cell
    By judasdac in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-12-2012, 01:56 AM
  7. Clear cell contents by text
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-25-2008, 11:18 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