+ Reply to Thread
Results 1 to 16 of 16

Button Macro to Subtract values from another cell

  1. #1
    Registered User
    Join Date
    10-22-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2010
    Posts
    36

    Button Macro to Subtract values from another cell

    I'm looking to create a button that contains a macro that will subtract a set number from a value in another cell. I know how I would do this as a formula, but realize that I need to do this in Visual Basic, which I am now well versed in. An example as follows; i would like the macro to subtract 2 from cell B5. once someone is able to show me how to do this, I'll be able to disect the code and learn how to apply it in other areas of my project. (If you must ask, it's for a gigantic players sheet/core rulebook for my RPG table top gaming obsession. Trying to create a resource for myself and my players so our gaming sessions can flow along much faster with less book keeping as i have made the game much more complicated.)

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Button Macro to Subtract values from another cell

    Mynumber= Range("B5")-2

  3. #3
    Registered User
    Join Date
    10-22-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2010
    Posts
    36

    Re: Button Macro to Subtract values from another cell

    Here is the code as I tried it below, but the macro would not attempt to run

    Please Login or Register  to view this content.
    I also tried to modify it and try the following code

    Please Login or Register  to view this content.
    but i get the following message "Run-Time error '13': Type mismatch"

    Could you point out what I'm typing wrong in either macro please?

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Button Macro to Subtract values from another cell

    No, you need to do something with this line.

    ActiveSheet.Range ("B5") - 2 is wrong
    you can select, copy or assign it to another variable-This is what I did. The value is copied in to Mynumber .
    What do you want to do once the code deducts?

  5. #5
    Registered User
    Join Date
    10-22-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2010
    Posts
    36

    Re: Button Macro to Subtract values from another cell

    The number will stay where it's at after the subtration, until at a further time that subtraction would be removed with [Mynumber= Range("B5")+2] I'm assuming. All I want it to do, is simply subtract a given number.

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Button Macro to Subtract values from another cell

    The number is still there(In memory). You can not see it in a sheet as you have not done anything with it.
    This code display the result in a message box.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-22-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2010
    Posts
    36

    Re: Button Macro to Subtract values from another cell

    I understand what you mean when asking "What do you want to do once the code deducts?" Sorry. Cell B5 could have any set of number already there, for example, 20. I want the macro to subtract 2 from cell B5, showing a result of 18. Then i would assign this macro to a button. Hope that clears things up. End result, i want whatever existing number to show in that cell after subtracting 2.

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Button Macro to Subtract values from another cell

    To assign a code to a button: Create a button first, then right click on the button, then choose "Assign a Macro", then you get a menu where the code( Tired) is stored. Click this workbook, then choose "Tired", The code is now attached to a button.
    Is this what you mean?

  9. #9
    Registered User
    Join Date
    10-22-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2010
    Posts
    36

    Re: Button Macro to Subtract values from another cell

    I know how to assign macro's to buttons. Sorry, guess I'm just confusing you with my post.

    Cell B5 could have any set of number already there, for example, 20. I want the macro to subtract 2 from cell B5, showing a result of 18.

    The above line is the result I'm looking for.

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Button Macro to Subtract values from another cell

    Where is showing a result of 18?
    Where do you see or found 18? Is it in a cell, if so which cell? B5 is a cell?
    In other words, how do you found out you have the result 18?

  11. #11
    Registered User
    Join Date
    10-22-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2010
    Posts
    36

    Re: Button Macro to Subtract values from another cell

    Cell B5 could have any set of number already there, for example, 20. I want the macro to subtract 2 from cell B5, showing a result of 18 in B5.

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Button Macro to Subtract values from another cell

    Please Login or Register  to view this content.

  13. #13
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Button Macro to Subtract values from another cell

    OR

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    10-22-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2010
    Posts
    36

    Re: Button Macro to Subtract values from another cell

    both codes return "Compile error: Wrong number of arguments or invalid property assignments"

  15. #15
    Registered User
    Join Date
    10-22-2013
    Location
    Iowa
    MS-Off Ver
    Excel 2010
    Posts
    36

    Re: Button Macro to Subtract values from another cell

    Please Login or Register  to view this content.
    I realized that I needed to put activesheet in the code as i have multiple spreadsheets, above code worked, thanks very much for your help, sorry for making that diffecult. Much Appreciated.

  16. #16
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Button Macro to Subtract values from another cell

    You do not need to put in the active sheet as long as your cursor is on the active sheet. Just choose the sheet first, ie put the cursor on the sheet you want to run the code on and then run the code.

    Since I have not referenced my code with any sheet name, it is assumed that (By default) the code works on any active sheet.

+ 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. Need a macro to subtract cell values of 2 separate workbooks.
    By Cindyberlindeee in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2013, 02:35 AM
  2. [SOLVED] Subtract next cell from the previous cell in a column for a range of values
    By frhling in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 12-12-2012, 10:34 AM
  3. [SOLVED] Using VB code to check for negative values then subtract them from a neighboring cell
    By mick86 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 07-15-2012, 07:23 AM
  4. Replies: 4
    Last Post: 11-13-2011, 06:21 AM
  5. Replies: 3
    Last Post: 04-12-2005, 07:06 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