+ Reply to Thread
Results 1 to 7 of 7

Pasting a formula between two active cells - one active cell varies

  1. #1
    Registered User
    Join Date
    07-20-2015
    Location
    New York
    MS-Off Ver
    2305 - Enterprise 365
    Posts
    147

    Pasting a formula between two active cells - one active cell varies

    Hello All,
    I want to paste a formula between two active cells. However, the lower active cell varies from month to month. Therefore, the range to which I want to paste my formula also varies. I have attached an example. In the example, the formula needs to be copied from cell C5 and pasted until the cell one row above "Grand Total" in column C.
    My problem is that the row (not the column) for the cell that contains "Grand Total" will vary from month to month. I would like to take this variation into account in the vba code. The formula will always be pasted in column C between cell C5 and "Grand Total", which is in column C. The other "Grand Total" in column B is not pertinent to my question. If someone could provide some code, I would be most appreciative.

    Best,
    Andrew
    Attached Files Attached Files

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Pasting a formula between two active cells - one active cell varies

    Hi Andrew,

    Welcome to the Forum:

    Here's some code for your task:

    Please Login or Register  to view this content.
    Directions for running the routine(s) just supplied

    If you haven't used macros before you'll need to go to:
    File- options - trust center -trust center settings - macro settings ,
    the second option down (disable all macros with notification)

    Then - Copy the code to the clipboard

    Open your Workbook

    Press ALT + F11 to open the Visual Basic Editor.

    Select "Module" from the Insert menu

    Type "Option Explicit" then paste the code under it

    With the cursor between Sub and End Sub press F5 (F8 to Single Step)

    OR

    Press ALT + Q to close the code window.

    Press ALT + F8 then double click on the macro name
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Re: Pasting a formula between two active cells - one active cell varies


    Hi !

    Andrew, according to your sample workbook, you can use Excel basics
    (beginner level ! Just use Macro recorder and / or read inner VBA help …)

    either Copy

    PHP Code: 
    Sub Demo1()
        
    With Sheet1.[A4].CurrentRegion.Columns(3)
            .
    Cells(2).Copy .Cells(3).Resize(.Rows.Count 3)
            .
    AutoFit
        End With
    End Sub 
    ... or AutoFill

    PHP Code: 
    Sub Demo2()
        
    With Sheet1.[A4].CurrentRegion.Columns(3)
            .
    Cells(2).AutoFill .Cells(2).Resize(.Rows.Count 2)
            .
    AutoFit
        End With
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  4. #4
    Registered User
    Join Date
    07-20-2015
    Location
    New York
    MS-Off Ver
    2305 - Enterprise 365
    Posts
    147

    Re: Pasting a formula between two active cells - one active cell varies

    Dear xladept, I just tried your macro and it works perfectly. Thanks so much for your help!

    Best,
    Andrew

  5. #5
    Registered User
    Join Date
    07-20-2015
    Location
    New York
    MS-Off Ver
    2305 - Enterprise 365
    Posts
    147

    Re: Pasting a formula between two active cells - one active cell varies

    Dear xladept, I just tried your macro and it works perfectly. Thanks so much for your help!

    Best,
    Andrew

  6. #6
    Registered User
    Join Date
    07-20-2015
    Location
    New York
    MS-Off Ver
    2305 - Enterprise 365
    Posts
    147

    Re: Pasting a formula between two active cells - one active cell varies

    Dear Marc L., your macros work great as well. Thank you both (Marc L. and xladept) for your quick and extremely helpful responses!

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Pasting a formula between two active cells - one active cell varies

    Hi Andrew,

    You're welcome and thanks for the rep!

+ 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. Replies: 4
    Last Post: 07-03-2015, 04:27 AM
  2. Problems with autofilling formula down the active column to the end row using active cell
    By sl_riches2179 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-27-2013, 09:30 AM
  3. copying particular cells and pasting into active worksheet
    By bradpeh in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-13-2013, 03:23 AM
  4. Change active cell when pasting date from another workbook
    By JBlam in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-12-2013, 04:33 AM
  5. Replies: 1
    Last Post: 07-08-2012, 08:47 AM
  6. Replies: 4
    Last Post: 02-22-2011, 10:56 AM
  7. [SOLVED] pasting textbox value to active cell
    By ASU in forum Excel General
    Replies: 2
    Last Post: 06-23-2006, 11:10 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