0

Hope someone can help me :)

I have data in sheet A, and formula's in sheet B. The formula's in sheet B are only in row 2.

So if sheet A has 33 rows filled with data, I switch to sheet B and drag the data in row 2 down to 33 to match it with sheet A.

Is it possible to do this with VBA?

I also tried just copying them over but in sheet B, but they are in a different order, so that's not really a solution for me.

What I tried is:

Private Sub Worksheet_Activate()
Range("A2:N2").Select
Selection.AutoFill Destination:=Range("A2:N" & Lastrow), Type:=xlFillDefault
End Sub

1 Answers1

0

No need to use AutoFill

LOGIC:

  1. Find the last row in Sheet A
  2. Fill the formula using .Formula in the entire range in one go.

CODE:

Is this what you are trying?

Option Explicit

Sub Sample()
    Dim wsA As Worksheet
    Dim wsB As Worksheet
    Dim lRow As Long
    
    '~~> Set these to relevant sheets
    Set wsA = Sheet1
    Set wsB = Sheet2
    
    '~~> Find last row in Sheet A
    lRow = wsA.Range("A" & wsA.Rows.Count).End(xlUp).Row
    
    '~~> Update formulas in Sheet B till relevant rows
    wsB.Range("A2:N" & lRow).Formula = wsB.Range("A2:N2").Formula
End Sub

NOTE:

Also you may want to read up on How to avoid using Select in Excel VBA

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250