Add Formula to a range via VBA

PankajMaheshwari

New Member
Joined
Sep 7, 2020
Messages
26
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
  3. Mobile
Hi Experts,

I have this below data set:
Data set.png



In the Data Set, If any cell in the range C2:C6 is blank, I want to use this formula =IF(B2="","",XLOOKUP(B2,A:A,D:D)) in those cells. Where row number of B2 is variable depending upon the row we are putting this formula via VBA.

If any cell in the range C2:C6 has value, I want to use that value without any formula. And if someone deletes the value and the cell becomes blank, VBA will add above formula to that cell.

Currently in the screenshot above, all the cells in range C2:C6 has above formula.

I hope I made sense. If this is not doable, it's okay. I can always use a helper column. But I think VBA would be a more cleaner way for my Dashboard.

Many Thanks in Advance.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hope some experts will look into this.
 
Upvote 0
Hi Mehidy1437, your solution works but what I'm trying to do is automate this. So I don't want every time to go and run this code. It should run automatically. I'm not sure if that's possible or not.
 
Upvote 0
This is my final code which seems to be working. Thanks for the help though.

VBA Code:
Private Sub InsertFormula()

Dim mwRng As Range
    Set mwRng = Range("C2:C250")
    Dim d As Range
    For Each d In mwRng
        If d.Value = "" Then
      d.Formula = "=IF(RC[-1]="""",""-"",INDEX(C[1],MATCH(RC[-1],C[-2],0)))"
    End If
Next d

End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Me.Range("C2:C250")) Is Nothing Then
        Application.EnableEvents = False
        Call InsertFormula
        Application.EnableEvents = True
    End If
End Sub
 
Upvote 0
My code is not good enough for the task, that's why I have removed.
Some experts will look into this for sure.
 
Upvote 0

Forum statistics

Threads
1,214,813
Messages
6,121,706
Members
449,048
Latest member
81jamesacct

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