+ Reply to Thread
Results 1 to 24 of 24

Absolute value

  1. #1
    Forum Contributor
    Join Date
    12-21-2011
    Location
    MS, USA
    MS-Off Ver
    Excel 2024
    Posts
    121

    Absolute value

    Hi is there anyway I could generate a macro that says if column A contains cats and dogs then column c is a a dollar value change it into negative value? For example A2 = dog and C= $10.00. So if dog exist in column A then the value in column C change to -$10.00. Make sense?

  2. #2
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Absolute value

    you can do that with a formula if you wanted to use an extra column like this

    =IF(A2="Dog",-C2,C2)

    but with a macro you can do it with the worksheet_change event like this

    Please Login or Register  to view this content.
    you could add a second check to see if Column C contains a number as well.
    Last edited by DGagnon; 03-27-2012 at 09:44 PM.
    If you liked my solution, please click on the Star -- to add to my reputation

    If your issue as been resolved, please clearly state so and mark the thread as [SOLVED] using the thread tools just above the first post.

  3. #3
    Forum Contributor
    Join Date
    12-21-2011
    Location
    MS, USA
    MS-Off Ver
    Excel 2024
    Posts
    121

    Re: Absolute value

    Great thanks that was fast. I prefer the macro. If I want to use more than one word like Cat, I will just enter "and "cat" " after the "dog" correct? I think this will work. Will try it first thing in the morning. Thanks!

  4. #4
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Absolute value

    are you looking for the word "Cat and Dog" or are you looking for "Cat" or "Dog"?

  5. #5
    Forum Contributor
    Join Date
    12-21-2011
    Location
    MS, USA
    MS-Off Ver
    Excel 2024
    Posts
    121

    Re: Absolute value

    Both cat and dog.

  6. #6
    Forum Contributor
    Join Date
    12-21-2011
    Location
    MS, USA
    MS-Off Ver
    Excel 2024
    Posts
    121

    Re: Absolute value

    I see what you mean. You are right. I think I would go with or because if only one existed then it change the value. You got me thinking.

  7. #7
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Absolute value

    so the sell must contain both words?

    would they be the only words in the cell? you mean need to do a string search.

  8. #8
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Absolute value

    to find either value you could use this:

    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    12-21-2011
    Location
    MS, USA
    MS-Off Ver
    Excel 2024
    Posts
    121

    Re: Absolute value

    Thank you! I'm trying to give you a star not sure if I did it right. Again thank u so much.

  10. #10
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Absolute value

    You did, i see it, thank you. I am glad i was able to help.

  11. #11
    Forum Contributor
    Join Date
    12-21-2011
    Location
    MS, USA
    MS-Off Ver
    Excel 2024
    Posts
    121

    Re: Absolute value

    Hi I tried to test it this morning but cant see the macro on the Macro dialog box to run it. how do I run a Private Sub?

  12. #12
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Absolute value

    you need to enter it into the worksheet module, it shoudl automaticaly run from there.

  13. #13
    Forum Contributor
    Join Date
    12-21-2011
    Location
    MS, USA
    MS-Off Ver
    Excel 2024
    Posts
    121

    Re: Absolute value

    I have it in the worksheet module as a master Micro enable workbook. so if I want to run this function on another workbooks I just open the master Micro enable workbook and run it from there.

    I see what you mean, i also copied to the original data file and it seems to attempt to run automatically but there is an issue with the Type mismatch on the (Target, "A;A")

  14. #14
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Absolute value

    ya this macro is designed to run on the sheet, it has to be loaded into each sheet that you want to use it on.

  15. #15
    Forum Contributor
    Join Date
    12-21-2011
    Location
    MS, USA
    MS-Off Ver
    Excel 2024
    Posts
    121

    Re: Absolute value

    there is a mismatch on the (Target, "A:A")

  16. #16
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Absolute value

    Test this attachment out and see if you can replicate this into your book
    Attached Files Attached Files

  17. #17
    Forum Contributor
    Join Date
    12-21-2011
    Location
    MS, USA
    MS-Off Ver
    Excel 2024
    Posts
    121

    Re: Absolute value

    It works great. the only issue is if I paste the data on top of that sheet which has 300 rows worth of data it gives me the overflow warning. then i had to double click on each row of Column A to update the column C individually.

  18. #18
    Forum Contributor
    Join Date
    12-21-2011
    Location
    MS, USA
    MS-Off Ver
    Excel 2024
    Posts
    121

    Re: Absolute value

    "If Target.Columns.Count > 1 Then Exit Sub" this solved the overflow issue. but still need to double click on column A to get the C adjusted.

  19. #19
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Absolute value

    it is designed to run when the cell changes, you could change it so that it will run when the cell is selected if you want. the question is when do you want the macro to trigger?

  20. #20
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Absolute value

    note: i did make a change to the code about 4 min after posting the initial solution, this is the most updated version i posted

    Please Login or Register  to view this content.

  21. #21
    Forum Contributor
    Join Date
    12-21-2011
    Location
    MS, USA
    MS-Off Ver
    Excel 2024
    Posts
    121

    Re: Absolute value

    I would like the Macro to trigger when I paste the data to that sheet then change the entire column.

  22. #22
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Absolute value

    Try this macro, slight modification to acomidate a range

    Please Login or Register  to view this content.

  23. #23
    Forum Contributor
    Join Date
    12-21-2011
    Location
    MS, USA
    MS-Off Ver
    Excel 2024
    Posts
    121

    Re: Absolute value

    PERFECT! Thank you!

  24. #24
    Forum Expert DGagnon's Avatar
    Join Date
    02-23-2012
    Location
    Ontario, Canada
    MS-Off Ver
    Excel 2003, 2007
    Posts
    1,645

    Re: Absolute value

    no problem, Glad i could help.

+ 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