?
Solved

Excel VBA to copy formulas

Posted on 2015-01-22
5
Medium Priority
?
106 Views
Last Modified: 2015-01-22
Hello,

i need help with a VBA code.

where when i run it in activesheet.  it will look for all cells in second row and if formula is found then copy and paste the formula to the last row of each column that have formula in second row.

thanks.
0
Comment
Question by:Flora
  • 2
  • 2
5 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40564052
I wouldn't use VBA code; it's redundant. If you use a Table (Ctrl+L or Insert - Tables - Table), then formulas can auto-repeat downwards automatically. See https://support.office.com/en-gb/article/Create-an-Excel-table-33d08e18-fa0f-40fe-8af3-5683b034ec42 for more details.
0
 
LVL 54

Accepted Solution

by:
Rgonzo1971 earned 2000 total points
ID: 40564053
if VBA

Sub Macro()

For Each c In Range(Range("A2"), Cells(2, Cells.Columns.Count).End(xlToLeft))
    If c.HasFormula Then
        Cells(Cells.Rows.Count, c.Column).End(xlUp).Offset(1).FormulaR1C1 = c.FormulaR1C1
    End If
Next
End Sub

Open in new window

Regards
0
 
LVL 6

Author Comment

by:Flora
ID: 40564105
Rgonzo1971

the above code works.

but it has one issue.

it goes one below the last row.   for example if my formulas end at row 10 then it goes one below. how do i stop it?
0
 
LVL 54

Expert Comment

by:Rgonzo1971
ID: 40564239
then change to

Sub Macro()

For Each c In Range(Range("A2"), Cells(2, Cells.Columns.Count).End(xlToLeft))
    If c.HasFormula Then
        Cells(Cells.Rows.Count, c.Column).End(xlUp).FormulaR1C1 = c.FormulaR1C1
    End If
Next
End Sub

Open in new window

0
 
LVL 6

Author Comment

by:Flora
ID: 40564247
thanks Rgonzo1971
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question