+ Reply to Thread
Results 1 to 17 of 17

Macro or function that changes value of selected range or ranges of cells

  1. #1
    Registered User
    Join Date
    08-08-2007
    Location
    Vancouver
    MS-Off Ver
    Excel 2002, Excel 2007, Excel for Mac 2011
    Posts
    38

    Talking Macro or function that changes value of selected range or ranges of cells

    I don't know if anyone has done this before. I am looking for a macro or function (VBA) that will modifiy the value of the selected cell or cells. the code should support selection of one cell, a range or multiple ranges.

    I envision the user making his range selection(s). Activating a function or clicking a button that would pop open a modal window. The user would have the option to either adjust the values by a % change (i.e. up or down 7%), or incremental change (i.e. up olr down 100 units). The function would overite the value in the cells.

    Has anyone ever done something similar? Is it hard to program?

    Your thoughts appreciated.
    Y.
    Last edited by ychartra01; 05-06-2009 at 06:47 PM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Macro or function that changes value of selected range or ranges of cells

    This is essentially to mimic the existing Paste Special: Operation method, yes ?

    Does something like the below do what you want ?

    Please Login or Register  to view this content.
    The awkward bit really is how you would determine whether you were altering by Unit or % without using an additional dialog... ie some percentages could exceed 1/-1 and similar some Unit increases could be between -1/1 so based on a numerical adjustment alone one can't determine the nature of the adjustment.

  3. #3
    Registered User
    Join Date
    08-08-2007
    Location
    Vancouver
    MS-Off Ver
    Excel 2002, Excel 2007, Excel for Mac 2011
    Posts
    38

    Re: Macro or function that changes value of selected range or ranges of cells

    That is what I am looking for. Thank you for the assistance. I do have one small problem. I am not very experienced in writing code. I did paste your code in a module and ran it. I tried in Excel 2007 and 2003.

    Each time I run the code I get an error:
    Run-time error '13': type mismatch

    on the following line of code
    rngArea.Value = IIf(strType = "P", rngArea.Value * (1 + (dblInput / 100)), rngArea.Value + dblInput)

    do you or anyone else have any suggestions?

    Thanx,

  4. #4
    Registered User
    Join Date
    08-08-2007
    Location
    Vancouver
    MS-Off Ver
    Excel 2002, Excel 2007, Excel for Mac 2011
    Posts
    38

    Re: Macro or function that changes value of selected range or ranges of cells

    That is what I am looking for. Thank you for the assistance. I do have one small problem. I am not very experienced in writing code. I did paste your code in a module and ran it. I tried in Excel 2007 and 2003.

    Each time I run the code I get an error:
    Please Login or Register  to view this content.
    on the following line of code
    Please Login or Register  to view this content.
    do you or anyone else have any suggestions?

    Thanx,

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Macro or function that changes value of selected range or ranges of cells

    Please post a sample file illustrating the code failure in action.... you would get an error if no numbers were found in selected range but that's not the error in this case.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Macro or function that changes value of selected range or ranges of cells

    DO, methinks if rngArea contains multi-cell areas, the IIF statement will fail.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Macro or function that changes value of selected range or ranges of cells

    Not sure I follow shg... can you elaborate ?
    EDIT: do you mean merged cells ? If so yes I agree... merged cells will cause debug.

    I did notice that if the selection were a single cell the code seemingly applies itself to use the Used Range which is, to me, weird... could you please explain to me why that is ?

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Macro or function that changes value of selected range or ranges of cells

    Type mismatch error:
    Please Login or Register  to view this content.

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Macro or function that changes value of selected range or ranges of cells

    doh, yes, it should be cell orientated as opposed to area.... though I suspect you could extend Area approach via Evaluate ... do you have an elegant approach in mind shg

    thanks

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Macro or function that changes value of selected range or ranges of cells

    do you have an elegant approach in mind shg
    Nope. Richard Schollar is very good with the use of Evaluate in lieu of loops.

  11. #11
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Macro or function that changes value of selected range or ranges of cells

    For the benefit of others ...

    Bill Jelen has just a published a book which illustrates the concept (with additional pointers from ExcelForum's very own: lecxe), ie using Evaluate as an alternative to a Loop.

    ie to add 1 to a range of cells without looping...

    Please Login or Register  to view this content.
    obviously things are a little more complex in the real case though I suspect that the volume of cells to be iterated (w/SpecialCells restriction) means a Loop on an individual cell by cell basis won't be too painful...however...:

    Please Login or Register  to view this content.
    Last edited by DonkeyOte; 05-05-2009 at 09:51 AM.

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Macro or function that changes value of selected range or ranges of cells

    Very nice.

  13. #13
    Registered User
    Join Date
    08-08-2007
    Location
    Vancouver
    MS-Off Ver
    Excel 2002, Excel 2007, Excel for Mac 2011
    Posts
    38

    Re: Macro or function that changes value of selected range or ranges of cells

    Quote Originally Posted by DonkeyOte View Post
    Please post a sample file illustrating the code failure in action.... you would get an error if no numbers were found in selected range but that's not the error in this case.
    See attached file. Column G is the data I want to change using the function.
    the button is tied to the function.

    Ideally it should work with selecting one range, multiple ranges or multiple cells.

    I have not read through all the posts yet I will read through and re-post.

    thanx, everyone.
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    08-08-2007
    Location
    Vancouver
    MS-Off Ver
    Excel 2002, Excel 2007, Excel for Mac 2011
    Posts
    38

    Re: Macro or function that changes value of selected range or ranges of cells

    Thank you DonkeyOte and shg. this solution is working great. Problem solved.

    Y

  15. #15
    Registered User
    Join Date
    08-08-2007
    Location
    Vancouver
    MS-Off Ver
    Excel 2002, Excel 2007, Excel for Mac 2011
    Posts
    38

    Re: Macro or function that changes value of selected range or ranges of cells

    My sales guys love it. But as usual they alway find areas for improvement.

    They are asking if we could make this work with filters. The real file will have over 600 records. They would like the ability to apply filters. So for example all legumes that are not black or white.

    Could we filter, select cells from the filtered list and have the function do it's magic only on the cells that are selected and visible? I've tried it with filters and it does the math on all hidden cells between the first cell and the last cell.

    Your thoughts. thank you.
    Attached Files Attached Files

  16. #16
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Macro or function that changes value of selected range or ranges of cells

    I wonder if perhaps the below will work for you ?
    (additional code in red)

    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    08-08-2007
    Location
    Vancouver
    MS-Off Ver
    Excel 2002, Excel 2007, Excel for Mac 2011
    Posts
    38

    Thumbs up Re: Macro or function that changes value of selected range or ranges of cells

    It's working perfectly.

    Thank you for your help. Problem solved

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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