+ Reply to Thread
Results 1 to 19 of 19

VBA: Delete cell contents if other cell=1

  1. #1
    Registered User
    Join Date
    08-15-2017
    Location
    Norfolk, England
    MS-Off Ver
    Office 365
    Posts
    13

    VBA: Delete cell contents if other cell=1

    Good afternoon,

    I'm very much new to vba, even though I have some knowledge of excel.

    What I'm looking for is some code that evaluates cells AP9,11,13,15,17,19 and if they have a value of 1 (derived from a formula), to clear the contents of the corresponding cells in O9,11,13,15,17,19. This needs to be applied to an entire workbook with multiple sheets.

    I'm looking to learn more about vba, but I'm completely stuck on this at the moment, so any help would be much appreciated.

    Cheers all,

    George

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA: Delete cell contents if other cell=1

    Something like:
    Please Login or Register  to view this content.
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Registered User
    Join Date
    08-15-2017
    Location
    Norfolk, England
    MS-Off Ver
    Office 365
    Posts
    13

    Re: VBA: Delete cell contents if other cell=1

    Cheers Olly,

    Thanks for the rapid response!

    When I run it I get the following message:

    Run-time error '13'; Type mismatch

    If ws.Cells(lRow, "AP").Value = 1 Then

    Is then highlighted when I debug.

  4. #4
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA: Delete cell contents if other cell=1

    I can't recreate that error. Can you attach your workbook, please?

  5. #5
    Valued Forum Contributor
    Join Date
    04-24-2014
    Location
    United States
    MS-Off Ver
    Office 365 ProPlus
    Posts
    853

    Re: VBA: Delete cell contents if other cell=1

    I'm pretty sure you can't mix and match the column letter with Cells.

    Perhaps replace this:

    Please Login or Register  to view this content.
    with this
    Please Login or Register  to view this content.

  6. #6
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA: Delete cell contents if other cell=1

    Quote Originally Posted by ptmuldoon View Post
    I'm pretty sure you can't mix and match the column letter with Cells.
    You really can. It works perfectly for me (on several machines!)

    Go test it - in your immediates window, enter:

    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: VBA: Delete cell contents if other cell=1

    Thanks a lot Olly for your awesome and effective solutions ..
    @ George : the error '13' is because of type mismatch and as you need to learn I expect you are trying to type the code yourself so may be when typing the variable lRow, may be you have typed 1Row (so you may typed one 1 instead of the letter L in this variable name)
    If not you can upload sample of your workbook. May be there some sheets protected ..
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  8. #8
    Registered User
    Join Date
    08-15-2017
    Location
    Norfolk, England
    MS-Off Ver
    Office 365
    Posts
    13

    Re: VBA: Delete cell contents if other cell=1

    So when cells O9,11,13,15,17,19 are filled, a timestamp is triggered in the corresponding cell in AO. When the difference is more than 1 second, the corresponding cell in AP is changed.

    So if AP is "1" I want the corresponding O cell to be cleared.

    The existing macro needs to be run for the time to update.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    08-15-2017
    Location
    Norfolk, England
    MS-Off Ver
    Office 365
    Posts
    13

    Re: VBA: Delete cell contents if other cell=1

    Thanks to everyone for getting back to me so quickly.

    I copied and pasted Olly's suggested code, and likewise ptmuldoon's but still have the same error.

    As, I say, I'm new to all this, so maybe there's a simpler way to do the whole shebang in vba.

  10. #10
    Valued Forum Contributor
    Join Date
    04-24-2014
    Location
    United States
    MS-Off Ver
    Office 365 ProPlus
    Posts
    853

    Re: VBA: Delete cell contents if other cell=1

    Quote Originally Posted by Olly View Post
    You really can. It works perfectly for me (on several machines!)

    Go test it - in your immediates window, enter:

    Please Login or Register  to view this content.
    Thanks, I wasn't aware you could do that.

    Anyway. I got your same error. Give us a few minutes to troubleshoot

  11. #11
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA: Delete cell contents if other cell=1

    Aha, your cells in AP contain errors.

    So we either need to handle that in the code, or change your formulas in AP.

    Code:
    Please Login or Register  to view this content.
    Formula:
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Change one, or the other, or both, then you'll achieve what you want.

  12. #12
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: VBA: Delete cell contents if other cell=1

    The result of the formula is the cause of the error. So yo have to add a line in the code like that
    Please Login or Register  to view this content.

  13. #13
    Valued Forum Contributor
    Join Date
    04-24-2014
    Location
    United States
    MS-Off Ver
    Office 365 ProPlus
    Posts
    853

    Re: VBA: Delete cell contents if other cell=1

    Yep.

    The underlying issue is you don't have end date for for everyone in all the worksheets. Per Olly, Fixing the formula with IfError and/or the macro to account for it should fix it for you.

  14. #14
    Registered User
    Join Date
    08-15-2017
    Location
    Norfolk, England
    MS-Off Ver
    Office 365
    Posts
    13

    Re: VBA: Delete cell contents if other cell=1

    You guys are amazing!

    I've updated both the AP formulae and the code. The run-time error has now disappeared but the cells in O aren't deleted.

    I've uploaded the updated workbook, maybe it's just me missing something simple.
    Attached Files Attached Files

  15. #15
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: VBA: Delete cell contents if other cell=1

    Your use of NOW() in the formulae is flawed - that's a volatile function, so whenever a calculation happens, all the cells referring to NOW() will change their value - and it looks like that's not what you want.

    I'd suggest you start a new thread, as it's a very different issue to this thread....

    In terms of the code, when I manually enter '1' in a cell in AP, then run the macro, it does what you asked.

  16. #16
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: VBA: Delete cell contents if other cell=1

    I have tested your workbook and it works well. Enter the value 12:00 in column "AO" to get the value 1 in "AP" and run the code ..
    The value in column "O" will be cleared....!!

  17. #17
    Registered User
    Join Date
    08-15-2017
    Location
    Norfolk, England
    MS-Off Ver
    Office 365
    Posts
    13

    Re: VBA: Delete cell contents if other cell=1

    OK. I've played with it a bit more today and I see that it works. Is it not possible though to have it continuously running when the worksheet is active, rather than run once and stop, once and stop, etc.

  18. #18
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: VBA: Delete cell contents if other cell=1

    You may call the macro in worksheet change but I don't recommend that.. It is better to update your data with one click when needed ..

  19. #19
    Registered User
    Join Date
    08-15-2017
    Location
    Norfolk, England
    MS-Off Ver
    Office 365
    Posts
    13

    Re: VBA: Delete cell contents if other cell=1

    Cheers guys. You've been a great help but I need it to be running continuously so I'm going to start a new thread as suggested by Olly.

+ 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] Delete contents of a cell in a given range if it matches contents of a specific cell
    By rcicconetti in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-13-2016, 06:46 AM
  2. Macro to enter todays date in an active cell then delete contents of cell to left
    By Ted Dennis in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-10-2015, 09:50 AM
  3. Delete Rows that contain matching cell contents in column to the contents in cell B1
    By sspatriots in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-23-2015, 05:39 PM
  4. [SOLVED] Macro Delete Contents in 23rd corresponding cell if the cell in range is a non-blank cell
    By murtaza.khan in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-04-2014, 01:11 AM
  5. Need to delete the contents of a cell every time another cell is updated
    By Rajesg Dutta in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-26-2013, 11:41 AM
  6. [SOLVED] Code to delete cell contents IF the result of a formula in the cell = 0
    By jayjaynz in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-22-2013, 03:58 AM
  7. Use find and replace to delete contents of cell and cell
    By almilw in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-11-2011, 06:56 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