Consulting

Results 1 to 14 of 14

Thread: About Subtract Function

  1. #1

    About Subtract Function

    Dear all,

    How to write the VBA codes of Subtract Formula ,
    -->A3 minus A2,
    A4 minus A3,
    ..... in col A?

    Many thanks!

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    What do you mean?
    Answer = Range("A3").Value-Range("A2).Value
     
    Range("X1").Formula = "=A3-A2")

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try

    Sub Minus()
    ActiveCell.FormulaR1C1 = "=R[-1]C-R[-2]C"
    ActiveCell.Offset(0, 1).Select
    End Sub

    Assign it to a shortcut key and it will insert the subtract formula in any location. Change the Offset to suit yourself.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Sorry for misleading ...

    I want to let the value of A3 = B4-B3
    A4 = B4-B3
    A5 = B5-B4
    ...............etc.

    How to write this? Thanks a lot!

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    OK, Try

    Sub Minus()
    ActiveCell.FormulaR1C1 = "=RC[1]-R[-1]C[1]"
    ActiveCell.Offset(1).Select
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    I'm still unsure what you mean.
    Range("A3") = Range("B4")-Range("B3")

  7. #7
    Dear all, thx for help!

    Sorry for my poor explaination as my mother lanuage is not english...:P

    Hi mdmackillop,

    Yes, you can get what I mean... How can I do it further by looping it?
    so that I can fill row A with the formula by one bottom click?

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This assumes no gaps in column B
    Sub Minus() 
    Do
    ActiveCell.FormulaR1C1 = "=RC[1]-R[-1]C[1]"
    ActiveCell.Offset(1).Select
    Loop Until ActiveCell.Offset(0, 1) = ""
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Newbie
    Joined
    Jun 2005
    Location
    Paris
    Posts
    1
    Location

    What about a function

    As an example

    Function minus(num1 As Long, num2 As Long)
    minus = num1 - num2
    End Function
    Franck

  10. #10
    HI,

    I've got around 20000 rows of data, it seems the looping is quite low...

  11. #11
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Take a look at this link. It tells you how to add a button and assign a macro to it. MD's example works for me providing there is no blanks in col B which would result in an abort of the macro.

    http://www.mrexcel.com/tip068.shtml
    Peace of mind is found in some of the strangest places.

  12. #12

  13. #13
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Snoopies,
    I wouldn't do a loop for 20,000 rows. The following is much faster (by about 580 times on my PC). If you can more fully explain your requirements in the original question, we can get to the solution faster as well.


    Sub Minus()
        ActiveCell.FormulaR1C1 = "=RC[1]-R[-1]C[1]"
        Range(ActiveCell.Address & ":A" & [B65536].End(xlUp).Row).FillDown
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  14. #14
    Hi MD,
    This one is really useful! Thanks! ^^

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •