VBA Code to fill down formulas

bmontoni

New Member
Joined
Mar 1, 2022
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a table in excel where I am trying to write a script that inputs a column name into a cell, enters the formula for that column below, then drags the formula down to the last row of the tableau. This is the script I am using but, once it gets down to row 5, it starts to use the previous row for the lookup value. Do anyone know why this my be hapenning?

1646151980787.png


Range("N3").Select
ActiveCell.FormulaR1C1 = "Merchandise Category"
Range("N4").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-3],MARA!C[-13]:C[-12],2,0)"
lastRow = Cells(Rows.Count, "M").End(xlUp).Row
Range("N5:N" & lastRow).SpecialCells(xlCellTypeBlanks).Formula = Range("N4").Formula


Thanks in advance!
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Welcome to the Board!

You should be able to just use this simplified code (get rid of all those SELECTS!):
VBA Code:
Range("N3").FormulaR1C1 = "Merchandise Category"
lastRow = Cells(Rows.Count, "M").End(xlUp).Row
Range("N4:N" & lastRow).FormulaR1C1 = "=VLOOKUP(RC[-3],MARA!C[-13]:C[-12],2,0)"

Note: I updated your thread title, because what you are showing in VBA code, something VERY DIFFERENT than VBScript code (which is a separate entity all its own, distinct from Excel).
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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