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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
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,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

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