Apply a vba to multiple rows

DCEXC

New Member
Joined
Oct 28, 2019
Messages
14
Hi, im literally brand new to VBA's

i have a worksheet that is using a vlookup formula, and its basically allows me to over write a cell but then restore the formula if i delete the overwrite.

i need to apply the same macro to all the rows and continue the cell references

macro:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
With Target
If .Address(False, False) = "O7" Then
If IsEmpty(.Value) Then
Application.EnableEvents = False
.Formula = "=T7"
Application.EnableEvents = True
End If
End If
End With
End Sub

i need this to continue down to O32 and for the "T" row numbers to follow i.e (o8 = t8 etc). pretty much as if i dragged the formula down.

Could be simple, but i have only found very broad info and cant make sense of it.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi,

The macro you posted is an Event macro ... not a standard one ...

Could you briefly explain your process ...
 
Upvote 0
Hi & welcome to MrExcel.
How about
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Not Intersect(Target, Range("O7:O32")) Is Nothing Then
        If IsEmpty(Target.Value) Then
            Application.EnableEvents = False
            Target.FormulaR1C1 = "=rc20"
            Application.EnableEvents = True
        End If
    End If
End Sub
 
Upvote 0
That is brilliant, thankyou!

but where is the reference to the T column? so if i decide to move the t column do a different one for a different sheet, which part of the code do i change?
 
Upvote 0
Right here...

Code:
Target.FormulaR1C1 = "=rc20"
In the statement c20..."c" =column and 20 = "T"
 
Upvote 0
Found one issue, when i press delete on the cell if i dont want to use my custom input, it doesnt auto bring back the formula like it used to, it just shows =rc20
 
Upvote 0
Try using

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Not Intersect(Target, Range("O[color=red]24[/color]:O32")) Is Nothing Then
        If IsEmpty(Target.Value) Then
            Application.EnableEvents = False
            Target.FormulaR1C1 = "=rc20"
            Application.EnableEvents = True
        End If
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
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