+ Reply to Thread
Results 1 to 24 of 24

Limit macro to only run if certain cells are selected

  1. #1
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Smile Limit macro to only run if certain cells are selected

    The following code works fine if I select a figure in a cell within column D, run the macro and it will increase or decrease the figure by a percentage.

    I would like to be able to select a cell in either column C or D and have the macro change the figure in column D (the figures to change are always in column D).

    Currently, any cell in the sheet can be selected and the macro runs ok; even on text.
    Could this be limited to only run if selections are made in columns C and D?, again the only figure to change would be in column D.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Mayweed; 09-01-2010 at 07:09 PM. Reason: Solved

  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
    48,155

    Re: Limit macro to only run if certain cells are selected first (Attached)

    One way:


    Please Login or Register  to view this content.
    Regards

  3. #3
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Limit macro to only run if certain cells are selected first (Attached)

    Thank you TMShucks, that has made it much better than the errors I've been getting.
    Is there any way this can be extended so that I can select a cell in column C as well as column D?

  4. #4
    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
    48,155

    Re: Limit macro to only run if certain cells are selected first (Attached)

    Columns("D") => Columns("C:D") and adjust the warning message.

    But when I tried that and amended the data in column C, I got a #VALUE! error.

    Your choice

  5. #5
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Limit macro to only run if certain cells are selected first (Attached)

    The #VALUE! error is the error I'm trying to avoid.
    You have been a great help,thanks again but I will leave this unsolved for now and hopefully someone else will have time to look at it.

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Limit macro to only run if certain cells are selected first (Attached)

    Please Login or Register  to view this content.
    Last edited by snb; 09-01-2010 at 11:59 AM.

  7. #7
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Limit macro to only run if certain cells are selected first (Attached)

    thanks snb but that code gives me a syntax error!

  8. #8
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Limit macro to only run if certain cells are selected first (Attached)

    Dunno how he does it, but snb must write his code on the fly!
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Limit macro to only run if certain cells are selected first (Attached)

    Hi Stephen, thanks for that code, the error no longer appears but the code doesn’t change anything either!

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Limit macro to only run if certain cells are selected first (Attached)

    @StephenR

    That's correct, your analysis of my code as well as your observation.
    That's why my thoughts are in the next code line while my fingers are are still struggling in the actual. Thank you for filling the gaps.

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Limit macro to only run if certain cells are selected first (Attached)

    Did you loop through the code step-by-step ( F8 ) ?

    This will do the trick:
    Please Login or Register  to view this content.
    cl.value =
    Last edited by snb; 09-01-2010 at 12:08 PM.

  12. #12
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Limit macro to only run if certain cells are selected first (Attached)

    I'm lost but is this the problem?
    If c02 = 0 Then
    Your last code works if the selected cell is in column D but not if the selected cell is in column C
    Last edited by Mayweed; 09-01-2010 at 12:12 PM.

  13. #13
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Limit macro to only run if certain cells are selected first (Attached)

    No, see my last edited post.

  14. #14
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Limit macro to only run if certain cells are selected first (Attached)

    Sorry snb, I have tested your code from the last post (Ctrl+F5 to force a browser refresh) but if I select a cell in column C and then run the VBA it does nothing at all.
    Does the "cl.value =" mean anything at the bottom of your post?
    I have attached your latest code in a workbook but it wont work for me selecting a cell in column C.
    Attached Files Attached Files
    Last edited by Mayweed; 09-01-2010 at 02:01 PM.

  15. #15
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Limit macro to only run if certain cells are selected first (Attached)

    Yes, that's the necessary change I made in the code in the same post.

  16. #16
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Limit macro to only run if certain cells are selected first (Attached)

    I have attached a workbook with your code inside, please try selecting a cell in column C and then running the macro.
    Attached Files Attached Files

  17. #17
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Limit macro to only run if certain cells are selected

    But column C is all text.

  18. #18
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Re: Limit macro to only run if certain cells are selected

    The idea was that you can also select the product/s in column C to alter the cell to the right of it in column D, the code I was using already adjusted column D so I assumed that making it work from column C wouldn’t be too difficult.
    That's the problem with having very little knowledge of VBA, assuming things probably isn’t a good idea.

  19. #19
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Limit macro to only run if certain cells are selected

    snb will see you right.

  20. #20
    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
    48,155

    Re: Limit macro to only run if certain cells are selected

    If you want to be able to select column C to increase the value in column D, you could try this:

    Please Login or Register  to view this content.

    However, note that, if you selected cells in column C and column D on the same row, the increase would be applied twice ... once for the cl in column C and once for the cl in column D (cl.offset(0,1).value).

    To overcome this, you could set a "flag" to indicate the cell has been updated and check this before updating a second time.

    The code is starting to get a little more complex ...

    Please Login or Register  to view this content.

    Hope this does what you want.

    Regards

  21. #21
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Limit macro to only run if certain cells are selected

    or
    Please Login or Register  to view this content.

  22. #22
    Forum Contributor
    Join Date
    08-19-2010
    Location
    UK.
    MS-Off Ver
    Excel 2007
    Posts
    142

    Smile Re: Limit macro to only run if certain cells are selected

    Thank you so much TMShucks, for the initial error fix and your new code that works exactly as I wanted it to, with one exception. I never even thought about the possibility of someone selecting both C & D cells so I guess you anticipated my next question and provided the solution before I even asked it... scary!
    Many thanks also to both Stephen and the genius snb, it obviously takes a certain level of expertise to come up with clever coding like this and your time and patience is very much appreciated.

  23. #23
    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
    48,155

    Re: Limit macro to only run if certain cells are selected

    You're welcome. Thanks for the feedback.

  24. #24
    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
    48,155

    Re: Limit macro to only run if certain cells are selected

    @snb ... neat!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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