+ Reply to Thread
Results 1 to 7 of 7

vba, excel, sum different ranges

  1. #1
    Danny
    Guest

    vba, excel, sum different ranges

    Hi,

    Before I posted this question, I researched on similar questions on this NG
    and I didn't get the answer I needed. All I need is a formula that would sum
    numbers above the active cell.

    This is the formula I have.

    ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"

    It works but it is only good if there are four or less numbers above it.

    What I need is a formula for variable numbers to add.

    Thank you.

  2. #2
    Bob Phillips
    Guest

    Re: vba, excel, sum different ranges

    How about

    ActiveCell.FormulaR1C1 = "=SUM(R1C:R[-1]C)"


    --
    HTH

    Bob Phillips

    "Danny" <[email protected]> wrote in message
    news:[email protected]...
    > Hi,
    >
    > Before I posted this question, I researched on similar questions on this

    NG
    > and I didn't get the answer I needed. All I need is a formula that would

    sum
    > numbers above the active cell.
    >
    > This is the formula I have.
    >
    > ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
    >
    > It works but it is only good if there are four or less numbers above it.
    >
    > What I need is a formula for variable numbers to add.
    >
    > Thank you.




  3. #3
    Bernie Deitrick
    Guest

    Re: vba, excel, sum different ranges

    Danny,

    Sub SumAtBottomOfCurrentColumn()
    Dim myCell As Range
    Set myCell = Cells(65536, ActiveCell.Column).End(xlUp)(2)
    'Uncomment this section to sum from a consistent row: in this example, row 2
    'And comment out the lower section of code
    'With myCell
    ' .Formula = "=SUM(" & _
    ' Range(.Offset(-1, 0), _
    ' Cells(2, .Column)).Address(False, False) & ")"
    'End With

    'Use this to sum the block up from the lowest cell
    With myCell
    .Formula = "=SUM(" & _
    Range(.Offset(-1, 0), _
    .Offset(-1, 0).End(xlUp)).Address(False, False) & ")"
    End With
    End Sub


    HTH,
    Bernie
    MS Excel MVP


    "Danny" <[email protected]> wrote in message
    news:[email protected]...
    > Hi,
    >
    > Before I posted this question, I researched on similar questions on this NG
    > and I didn't get the answer I needed. All I need is a formula that would sum
    > numbers above the active cell.
    >
    > This is the formula I have.
    >
    > ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
    >
    > It works but it is only good if there are four or less numbers above it.
    >
    > What I need is a formula for variable numbers to add.
    >
    > Thank you.




  4. #4
    Danny
    Guest

    Re: vba, excel, sum different ranges

    Thank you for the response. Can you please modify to add only to up to last
    cell?
    Let's say the active cell is A15 and the numbers above it is from A10 to
    A14, the formula would only add A10 (A9 is blank) to A14.

    Then if my macro would select A30 as the active cell and there are numbers
    from A21 (A20 is blank)to A29, it would only add A21 to A29.

    The formula you gave adds from A1.

    Thanks again.


    "Bob Phillips" wrote:

    > How about
    >
    > ActiveCell.FormulaR1C1 = "=SUM(R1C:R[-1]C)"
    >
    >
    > --
    > HTH
    >
    > Bob Phillips
    >
    > "Danny" <[email protected]> wrote in message
    > news:[email protected]...
    > > Hi,
    > >
    > > Before I posted this question, I researched on similar questions on this

    > NG
    > > and I didn't get the answer I needed. All I need is a formula that would

    > sum
    > > numbers above the active cell.
    > >
    > > This is the formula I have.
    > >
    > > ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
    > >
    > > It works but it is only good if there are four or less numbers above it.
    > >
    > > What I need is a formula for variable numbers to add.
    > >
    > > Thank you.

    >
    >
    >


  5. #5
    Danny
    Guest

    Re: vba, excel, sum different ranges

    Hi Bernie,

    I tried the formula and sometimes it works. It also jumps one cell below. It
    there a simple way to modify the original formula to make "-4" variable?

    ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"

    "Bernie Deitrick" wrote:

    > Danny,
    >
    > Sub SumAtBottomOfCurrentColumn()
    > Dim myCell As Range
    > Set myCell = Cells(65536, ActiveCell.Column).End(xlUp)(2)
    > 'Uncomment this section to sum from a consistent row: in this example, row 2
    > 'And comment out the lower section of code
    > 'With myCell
    > ' .Formula = "=SUM(" & _
    > ' Range(.Offset(-1, 0), _
    > ' Cells(2, .Column)).Address(False, False) & ")"
    > 'End With
    >
    > 'Use this to sum the block up from the lowest cell
    > With myCell
    > .Formula = "=SUM(" & _
    > Range(.Offset(-1, 0), _
    > .Offset(-1, 0).End(xlUp)).Address(False, False) & ")"
    > End With
    > End Sub
    >
    >
    > HTH,
    > Bernie
    > MS Excel MVP
    >
    >
    > "Danny" <[email protected]> wrote in message
    > news:[email protected]...
    > > Hi,
    > >
    > > Before I posted this question, I researched on similar questions on this NG
    > > and I didn't get the answer I needed. All I need is a formula that would sum
    > > numbers above the active cell.
    > >
    > > This is the formula I have.
    > >
    > > ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
    > >
    > > It works but it is only good if there are four or less numbers above it.
    > >
    > > What I need is a formula for variable numbers to add.
    > >
    > > Thank you.

    >
    >
    >


  6. #6
    Bernie Deitrick
    Guest

    Re: vba, excel, sum different ranges

    Danny,

    Try this version.

    Sub SumInActiveCell()
    With ActiveCell
    .Formula = "=SUM(" & _
    Range(.Offset(-1, 0), _
    .Offset(-1, 0).End(xlUp)).Address(False, False) & ")"
    End With
    End Sub

    HTH,
    Bernie
    MS Excel MVP


    "Danny" <[email protected]> wrote in message
    news:[email protected]...
    > Hi Bernie,
    >
    > I tried the formula and sometimes it works. It also jumps one cell below. It
    > there a simple way to modify the original formula to make "-4" variable?
    >
    > ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
    >
    > "Bernie Deitrick" wrote:
    >
    >> Danny,
    >>
    >> Sub SumAtBottomOfCurrentColumn()
    >> Dim myCell As Range
    >> Set myCell = Cells(65536, ActiveCell.Column).End(xlUp)(2)
    >> 'Uncomment this section to sum from a consistent row: in this example, row 2
    >> 'And comment out the lower section of code
    >> 'With myCell
    >> ' .Formula = "=SUM(" & _
    >> ' Range(.Offset(-1, 0), _
    >> ' Cells(2, .Column)).Address(False, False) & ")"
    >> 'End With
    >>
    >> 'Use this to sum the block up from the lowest cell
    >> With myCell
    >> .Formula = "=SUM(" & _
    >> Range(.Offset(-1, 0), _
    >> .Offset(-1, 0).End(xlUp)).Address(False, False) & ")"
    >> End With
    >> End Sub
    >>
    >>
    >> HTH,
    >> Bernie
    >> MS Excel MVP
    >>
    >>
    >> "Danny" <[email protected]> wrote in message
    >> news:[email protected]...
    >> > Hi,
    >> >
    >> > Before I posted this question, I researched on similar questions on this NG
    >> > and I didn't get the answer I needed. All I need is a formula that would sum
    >> > numbers above the active cell.
    >> >
    >> > This is the formula I have.
    >> >
    >> > ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
    >> >
    >> > It works but it is only good if there are four or less numbers above it.
    >> >
    >> > What I need is a formula for variable numbers to add.
    >> >
    >> > Thank you.

    >>
    >>
    >>




  7. #7
    Danny
    Guest

    Re: vba, excel, sum different ranges

    Bernie,

    Perfect! Thanks a lot!

    "Bernie Deitrick" wrote:

    > Danny,
    >
    > Try this version.
    >
    > Sub SumInActiveCell()
    > With ActiveCell
    > .Formula = "=SUM(" & _
    > Range(.Offset(-1, 0), _
    > .Offset(-1, 0).End(xlUp)).Address(False, False) & ")"
    > End With
    > End Sub
    >
    > HTH,
    > Bernie
    > MS Excel MVP
    >
    >
    > "Danny" <[email protected]> wrote in message
    > news:[email protected]...
    > > Hi Bernie,
    > >
    > > I tried the formula and sometimes it works. It also jumps one cell below. It
    > > there a simple way to modify the original formula to make "-4" variable?
    > >
    > > ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
    > >
    > > "Bernie Deitrick" wrote:
    > >
    > >> Danny,
    > >>
    > >> Sub SumAtBottomOfCurrentColumn()
    > >> Dim myCell As Range
    > >> Set myCell = Cells(65536, ActiveCell.Column).End(xlUp)(2)
    > >> 'Uncomment this section to sum from a consistent row: in this example, row 2
    > >> 'And comment out the lower section of code
    > >> 'With myCell
    > >> ' .Formula = "=SUM(" & _
    > >> ' Range(.Offset(-1, 0), _
    > >> ' Cells(2, .Column)).Address(False, False) & ")"
    > >> 'End With
    > >>
    > >> 'Use this to sum the block up from the lowest cell
    > >> With myCell
    > >> .Formula = "=SUM(" & _
    > >> Range(.Offset(-1, 0), _
    > >> .Offset(-1, 0).End(xlUp)).Address(False, False) & ")"
    > >> End With
    > >> End Sub
    > >>
    > >>
    > >> HTH,
    > >> Bernie
    > >> MS Excel MVP
    > >>
    > >>
    > >> "Danny" <[email protected]> wrote in message
    > >> news:[email protected]...
    > >> > Hi,
    > >> >
    > >> > Before I posted this question, I researched on similar questions on this NG
    > >> > and I didn't get the answer I needed. All I need is a formula that would sum
    > >> > numbers above the active cell.
    > >> >
    > >> > This is the formula I have.
    > >> >
    > >> > ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
    > >> >
    > >> > It works but it is only good if there are four or less numbers above it.
    > >> >
    > >> > What I need is a formula for variable numbers to add.
    > >> >
    > >> > Thank you.
    > >>
    > >>
    > >>

    >
    >
    >


+ 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