+ Reply to Thread
Results 1 to 11 of 11

Adding a range in VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    05-11-2006
    Posts
    11

    Adding a range in VBA

    I need to Add/Sum a Range(A Column) in Code I know the Top and the Bottom, but do not know how to work with R1C1 Data Here is the code I have all works except the ActiveCell.FormulaR1C1 Line.
    When I try to use my reference points it fails.
    The TOP and BOTTOM will change based on the data inputed.

    How can I use my Top and Bottom Reference Points?

    Range(Top & ":" & Bottom).Select
        Range("H" & Bottom + 4).Activate
        ActiveSheet.Cells(Bottom + 3, 8).Formula = "=Sum(bottom & : & top)"
    
        ActiveCell.FormulaR1C1 = "=SUM(R[-4]C:R[-1]C)"
        Range("H" & Bottom + 4).Select
        Selection.AutoFill Destination:=Range("H" & Bottom + 4 & ":" & "K" & Bottom + 4), Type:=xlFillDefault
        Range("H" & Bottom + 4 & ":" & "K" & Bottom + 4).Select

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    No need to select.

    See if this points you in the right direction

    Dim LastRow As Long
    Dim StartRow As Long
    
    StartRow = 3
    LastRow = 10
    
    With ActiveCell
        .Formula = "=SUM(A" & StartRow & ":A" & LastRow & ")"
        .AutoFill Destination:=Range(ActiveCell.Address, "A" & LastRow + 6), Type:=xlFillDefault
    End With
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Registered User
    Join Date
    05-11-2006
    Posts
    11

    Missing something

    I could not get it to work
    Even if I used it like this:
        Range("H6:h47").Select
        With ActiveCell
        .Formula = "=SUM(H6:H47)"
        '.AutoFill Destination:=Range(ActiveCell.Address, "H" & Bottom + 6), Type:=xlFillDefault
        End With
    I did not get any results on the Sheet

  4. #4
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    That the trouble with only giving a bit of the code. You get a bit of an answer.

    I notice a
    '
    in the autofill line which will stop that line from running

    '.AutoFill
    VBA Noob

  5. #5
    Registered User
    Join Date
    05-11-2006
    Posts
    11

    Still Somewrong

    I did have a ' on that line once removed it wrote Zero's in H6 the TOP all the way down thru H53 Bottom + 6.


    No Sum

    Bill

  6. #6
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Bill,

    You are selecting (which you should also avoid) H6 to H47 and then asking it to insert the formula
    "=SUM(H6:H47)"
    this will return a circular ref.

    If you want help I suggest you post a sample with explaination of what your after

    Range("H6:h47").Select
        With ActiveCell
        .Formula = "=SUM(H6:H47)"
    VBA Noob

+ 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