Autofill Column with Formula

Simonc64

Active Member
Joined
Feb 15, 2007
Messages
251
Office Version
  1. 365
Hi Guys

Hopefully a very simple VBA solution to this.

I have a vlookup formula in say cell "F2" that is dependent upon a value appearing in cell "E2".


What I am after is some VBA code that, when a value appears in cell "E3", the vlookup formula in cell "F2" 'copies' itself to cell "E3" and so on, maybe all the way to the end of the column.

Simple as that !!

I am aware that i could just copy "F2" down say 20k rows, but im more concerned with understanding the VBA coding

Thanks in advance
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Put in the following code in the events of your sheet.


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("E:E")) Is Nothing Then
    If Target.Row < 3 Then Exit Sub
    Dim c As Range
    For Each c In Target
      Range("F2").Copy c.Offset(, 1)
    Next
  End If
End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.
 
Upvote 0
Put in the following code in the events of your sheet.


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("E:E")) Is Nothing Then
    If Target.Row < 3 Then Exit Sub
    Dim c As Range
    For Each c In Target
      Range("F2").Copy c.Offset(, 1)
    Next
  End If
End Sub

SHEET EVENT
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.

Hi Dante

Thanks for this; it seems to work to a point; it fills out column F but also overwrites the formulae in the next 3 columns as well?
 
Upvote 0
Try this

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Not Intersect(Target, Range("E:E")) Is Nothing Then
    If Target.Row < 3 Then Exit Sub
    Dim c As Range
    For Each c In Target
      If c.Column = Columns("E").Column Then
        Range("F2").Copy c.Offset(, 1)
      End If
    Next
  End If
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,537
Members
449,088
Latest member
RandomExceller01

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