+ Reply to Thread
Results 1 to 5 of 5

Automatically filling newly created rows

  1. #1
    Registered User
    Join Date
    02-14-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    2

    Automatically filling newly created rows

    Hey everybody,

    I have a large database in excel. Sometimes I will add rows to this database.
    I want to reduce my manual labour by making a vba code that will automatically copy the formula of the cell above the newly created empty cells into the new cells. So for instance if cell B1 has the formula "=A1" and I create a new second row, I want that the formula in newly created cell B2 becomes: "=A2"

    What I made is the following:

    Sub refresh()
    Range("BS65536").End(xlUp).Select
    Range("BP3", ActiveCell).Select
    Selection.SpecialCells(xlCellTypeBlanks).Select
    Selection.FormulaR1C1 = "=R[-1]C"
    End Sub

    My problem now is that, continuing on my example, the cell B2 will have the formula "=B1" instead of "=A2", anyone one knows how to fix this?
    Thanks for your help!

    Jeroen

  2. #2
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Automatically filling newly created rows

    Unless I am reading it wrong don't reference the formula, reference the cell you want. From what I can see you just want the formula to reference the cell to the left.
    The code below will do that for every blank cell from B2 up to the last cell with data in Column B.

    Please Login or Register  to view this content.
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  3. #3
    Registered User
    Join Date
    02-14-2013
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Automatically filling newly created rows

    Hey!

    Thank you for your fast reply and my apologies for my poor English which made you indeed misinterpret my question.

    I will try to explain it better.

    The excelsheet I have is a customer file. Now of course, sometimes I need to add new customers. These are ordered on surname, so I will need to create a new row to add them in the right order of descending first letter. Some columns of the new row I need to fill in manually (think of name, address, purchases etc) but some others (think of sum of purchases etc) are just some formula's that use data from that particular row.

    I now have to copy these formula's into the newly created cells (so for instance again, cell BP4 might be "=SUM(A4:BB4)" every time when I add a new row to the file. I now wish to create a macro that will automatically copy these formulas.

    What I therefore did was to make a macro that first selects the range of the columns that include these cells containing these kind of formula's (thus for instance the sum of purchases), find the empty cells in these colums (this will be the cells of the newly added rows) and then somehow copy the formula that was above it into the new cell. So again for instance if the cell above the empty cell (lets say BP4 again) was "=SUM(A4:BB4)", the new cell should be filled with "=SUM(A5:BB5)". However with the macro I have written it will now be filled with "=BP4", which is of course not what I want, because it will give me the sum of purchases of the old customer and not the new one.

    I hope this clarifies things, thanks again for your help!

  4. #4
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Automatically filling newly created rows

    Basically you used the right word "copy"
    That is what you need to do (or autofill). For instance if you have your formula in B4 put something in cell B20 and run the code below, you will see that the formula will fill in the way you want.

    Please Login or Register  to view this content.
    What you really need is some selection.change code but I am just knocking off for the night and so I won't be able to look at it now (hopefully someone might jump in or I will have a look at it tomorrow night)

  5. #5
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    883

    Re: Automatically filling newly created rows

    I had a think about this and actually I don't think worksheet.change is the best way for this, I think a simple button is a better option.

    Place the code below in a regular module.

    Then insert a button on the sheet (a Forms control button not an ActiveX button), when you create the button the assign a macro dialogue should automatically open (if it doesn't right the button and click assign macro).

    Select the "xvxi" macro and ok.

    To use, click any cell in the row currently holding the formula and click the button.

    Things to do/note

    Make sure you "fix" the start row number in the original formula i.e.
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    not
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Change the letters in red to the Column letter of the cells you want filled automatically.
    Please Login or Register  to view this content.

+ 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. [SOLVED] Insert Row based on Condition and add data in the newly created rows
    By Omariss in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-16-2013, 04:27 AM
  2. A formula that will automatically include newly added rows?
    By dbwoods11 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-09-2013, 03:53 AM
  3. Replies: 0
    Last Post: 05-29-2009, 09:42 AM
  4. macro that will automatically link a newly created worksheet to a summary sheet
    By liquidblack in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-18-2009, 03:22 PM
  5. Replies: 1
    Last Post: 07-20-2008, 02:22 PM

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