VBA code for Copying relative formula in a cell

ahasiddah

New Member
Joined
May 31, 2017
Messages
5
Dear all,
Good day!
I am trying to write vba code for copying a relative formula in a cell if formula in that cell is deleted by mistake.

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("c14:c1014")) Is Nothing And Target.Count = 1 Then
Application.EnableEvents = False
If IsEmpty(Target) Then
Target.Formula = Target.Offset(r - 1, c).Formula
End If
Application.EnableEvents = True
End If
End Sub

Thank you.

Can anyone help to correct the code as it is not copying the relative formula from one cell down of the cell, where formula is to be copied.

e.g
A B c

14 Formula
15 Copy here relative formula
16 Formula
.
.
1014
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hia
Try this
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("c14:c1014")) Is Nothing And Target.Count = 1 Then
Application.EnableEvents = False
If IsEmpty(Target) Then
Target.FormulaR1C1 = Target.Offset(-1).FormulaR1C1
End If
Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Hia
Try this
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("c14:c1014")) Is Nothing And Target.Count = 1 Then
Application.EnableEvents = False
If IsEmpty(Target) Then
Target.FormulaR1C1 = Target.Offset(-1).FormulaR1C1
End If
Application.EnableEvents = True
End If
End Sub

It solved my problem.
Thank you very much Fluf. May you happy all the time like flowers
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
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