Autofill formulas

CP11

New Member
Joined
Jun 8, 2022
Messages
11
Office Version
  1. 2021
Platform
  1. Windows
I have the below VBA which:

1. Makes a copy of an existing sheet
2. Inserts extra columns
3. Adds column headers
4. Adds a formula to the new columns in row 2
5. Auto-fills the formula down to the last row (based on column A)
6. Repeats the above for a number of other columns.

It works, however takes quite a long time. My data set has thousands of row and 10 new columns to add and fill with formulas.

Is there a more efficient way to do this?


VBA Code:
Sheets("Sheet1").Copy after:=Sheets(Sheets.Count)
ActiveSheet.Name = "Sheet2"

lastRow = Range("A" & Rows.Count).End(xlUp).Row


Columns("F:G").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("F1").Value = "Header 1"
Range("G1").Value = "Header2"

Range("F2").FormulaR1C1 = "=VLOOKUP(RC[-1],Sheet3!C[-1],1,FALSE)"
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)

Range("G2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-2],Sheet4!C[-2],1,FALSE),""N/A"")"
Range("G2").AutoFill Destination:=Range("G2:G" & lastRow)

'Repeat to add  X more new columns and formulas
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
You should probably post more of your code so we can get a better idea of what your code looks like.
 
Upvote 0
It works, however takes quite a long time. My data set has thousands of row and 10 new columns to add and fill with formulas.

Is there a more efficient way to do this?
I suspect that, largely speaking, the time issue is likely to do with the time taken to calculate the results of all your new formulas rather than the efficiency of the code.

However, if your inserted columns are adjacent to each other like the sample, then you can at least deal with the whole group of columns together rather that each one individually.

So, instead of this
VBA Code:
Range("F1").Value = "Header 1"
Range("G1").Value = "Header2"

Range("F2").FormulaR1C1 = "=VLOOKUP(RC[-1],Sheet3!C[-1],1,FALSE)"
Range("F2").AutoFill Destination:=Range("F2:F" & lastRow)

Range("G2").FormulaR1C1 = "=IFERROR(VLOOKUP(RC[-2],Sheet4!C[-2],1,FALSE),""N/A"")"
Range("G2").AutoFill Destination:=Range("G2:G" & lastRow)

.. you can do this sort of thing.

VBA Code:
Range("F1:G1").Value = Array("Header 1", "Header2")
Range("F2:G2").FormulaR1C1 = Array("=VLOOKUP(RC[-1],Sheet3!C[-1],1,FALSE)", "=IFERROR(VLOOKUP(RC[-2],Sheet4!C[-2],1,FALSE),""N/A"")")
Range("F2:G" & lastRow).FillDown
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,130
Members
453,021
Latest member
Justyna P

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