+ Reply to Thread
Results 1 to 7 of 7

Code to copy down formulas and formatting

  1. #1
    Registered User
    Join Date
    05-01-2020
    Location
    Massachusetts
    MS-Off Ver
    Excel for Mac 16.92 (Microsoft 365)
    Posts
    94

    Code to copy down formulas and formatting

    I have a personal project I'm doing with Excel that some VBA code would help with, but I'm not very skilled at writing VBA code. Hoping someone might be able to whip something together quickly for me.

    My spreadsheets uses a FILTER function to pull data from external workbooks, after which the data is converted to text. Cells outside the pull range have formulas that act on each row of data ... these formulas are stored in the first row. Once the data is imported via the FILTER function the calculation cells have to be brought down to each row of resulting data.

    Because the source data can vary in how many of rows it has, the code has to be dynamic -- copy formula to row x when row x is the last row of data. Obviously, knowing which row is the last row requires looking in a different column.

    FWIW, the spreadsheet has 16 worksheets all which pull data from different sources, so the code has to handle all of them. I'm not sure if that means the code has to be specifically built for each worksheet, or if a generic code can be written that can be applied to each worksheet as necessary.

    Thanks for any help.

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,080

    Re: Code to copy down formulas and formatting

    It would be easier to help and test possible solutions if you could attach a copy of your file. Explain in detail what you want to do referring to specific cells, rows, columns and sheets using a few examples from your data (de-sensitized if necessary). See the yellow banner at the top of this page for instructions to attach a file. We wouldn't need all the sheets or all the rows. Two or three sheets each with a dozen rows would be fine.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Registered User
    Join Date
    05-01-2020
    Location
    Massachusetts
    MS-Off Ver
    Excel for Mac 16.92 (Microsoft 365)
    Posts
    94

    Re: Code to copy down formulas and formatting

    A super simple file is attached. Remember, on each specific pull the number of rows may be different.
    Attached Files Attached Files

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,080

    Re: Code to copy down formulas and formatting

    Try:
    Please Login or Register  to view this content.

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

    Cool Try this !


    According to the attachment a VBA demonstration as a beginner starter :

    PHP Code: 
    Sub Demo1()
             
    Dim Ws As Worksheet
        
    For Each Ws In Worksheets
           Range
    (Ws.[D5], Ws.[A4].End(xlDown)(14)).FillDown
        Next
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  6. #6
    Registered User
    Join Date
    05-01-2020
    Location
    Massachusetts
    MS-Off Ver
    Excel for Mac 16.92 (Microsoft 365)
    Posts
    94

    Re: Code to copy down formulas and formatting

    OK guys, I get it. I should have just loaded the actual file itself. The code you've given me certainly will start to get the job done, but I'm not sure I can easily adapt it to my actual file because:

    1. The code needs to impact most of the worksheets, but not all (16 of 18)
    2. The table structure of each worksheet is not consistent (the columns containing the formulas may be different in each worksheet, as could be the number of columns with formulas).
    3. It's more likely to want to apply the code for a group of worksheets (or even a single one) rather than the entire set all at once.

    Given this, it seems that I'm going to need to have a module for each worksheet, which are primarily copies of each other with slight modifications. So I'm guessing that:

    1. I'll need to strike the For Each ws command and replace with something worksheet specific
    2. I'll need to specify the range of columns (which may or may not be contiguous) for which the drop down should occur

    The actual file is attached (without its source data) -- if we modify the VBA code you provided to work for the "Civil" tab, I can adapt to the others. In the Civil tab, columns A-C, AA-AD, and AF-AH have the formulas that need to be copied down based on how many rows the dynamic FILTER function in $E4 retrieves (once the filter has done it's job, this data will be copied and pasted as values).

    If I'm not overstretching, copying and pasting that data is a nice add-on to the VBA, but it must do so by first copying the formatting of column F (its a conditional format). I figured if I could automate the formula copy down, I'd be OK manually locking the data pulled by the FILTER formula. But if its easy to code....

    (Note that any formula that pulls data from an external workbook starts with "xSOURCE". This formula is "activated" by replacing "xSOURCE" with "=" which allows control over when the formula retrieves the data as well as how keeping the size of the file reasonable while in template mode.)
    Attached Files Attached Files

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

    Arrow Re: Code to copy down formulas and formatting


    So just apply FillDown to the range to update in a single codeline …
    Last edited by Marc L; 01-19-2024 at 07:50 PM.

+ 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: 1
    Last Post: 11-30-2023, 02:58 PM
  2. How do you copy and paste everything, formulas, non-formulas, formatting?
    By taylorsm in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-05-2018, 10:53 AM
  3. Replies: 3
    Last Post: 11-16-2017, 08:47 AM
  4. Copy of conditional formatting's formulas
    By Skuimkop in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-17-2016, 02:56 PM
  5. Tool or code to copy VBA code to OneNote or Word while preserving VBA formatting
    By gregersdk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-05-2015, 04:07 PM
  6. Add a row, copy formulas, and formatting
    By sp135 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-19-2010, 10:32 AM
  7. Copy Row's formatting and formulas but not values
    By CRIMEDOG in forum Excel General
    Replies: 6
    Last Post: 07-07-2009, 05:51 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