Copy formulas down vba

MetLife

Active Member
Joined
Jul 2, 2012
Messages
311
Office Version
  1. 365
Hi,

I have a macro that grabs data from a file, this is a table of 25x150 rows. The rows can vary.

So in my other tab I have formulas that start on Cell A5 and these reference the data in Tab #1.

All I want to do is have the macro copy the formulas in cell A5 down to as many records as was in the file of 25x150 rows. Since the rows varies, what is the best way to do this?

I was thinking of just having a formula "=Counta(A:A)" to count the records and then use offset, but there is probably a better way.

Thanks,
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
You want it to affect the formulas in all columns from A:X? Being that they are all different formulas, you may need VBA to copy them down. The following will work for just col A though:

Excel Formula:
=Sheet1!A5:INDEX(Sheet1!A:A,XMATCH(FALSE,ISBLANK(Sheet1!A:A),0,-1))

Copy and paste into A5, do not copy down.
 
Upvote 0
You want it to affect the formulas in all columns from A:X? Being that they are all different formulas, you may need VBA to copy them down. The following will work for just col A though:

Excel Formula:
=Sheet1!A5:INDEX(Sheet1!A:A,XMATCH(FALSE,ISBLANK(Sheet1!A:A),0,-1))

Copy and paste into A5, do not copy down.
I want to do this in vba
 
Upvote 0
Your formulas in row 5 would need be such that copying them down won't impact the references ie your VLookup "B5 = VLOOKUP(A1, Sheet1!A5:D100,10,false)" won't be correct if copied down.

Let's start with this:
VBA Code:
Sub CopyFormulas()

    Dim shtSrc As Worksheet, shtDest As Worksheet
    Dim rowLastSrc As Long, colLastSrc As Long
    Dim rngSrc As Range, rngDest As Range
    
    Set shtSrc = Worksheets("Sheet1")
    Set shtDest = Worksheets("Sheet2")
    
    With shtSrc
        rowLastSrc = .Cells(Rows.Count, "A").End(xlUp).Row
        colLastSrc = .Cells(1, Columns.Count).End(xlToLeft).Column
        Set rngSrc = .Range(.Cells(5, "A"), .Cells(rowLastSrc, colLastSrc))
    End With
    
    With shtDest
        Set rngDest = .Range("A5").Resize(rngSrc.Rows.Count, rngSrc.Columns.Count)
        rngDest.Rows(1).AutoFill Destination:=rngDest
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,221,531
Messages
6,160,366
Members
451,642
Latest member
mirofa

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top