Copy formulas down vba

MetLife

Active Member
Joined
Jul 2, 2012
Messages
330
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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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