Macro for adding IF statements to the existing formula

HariHaran R S

New Member
Joined
Feb 6, 2024
Messages
3
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
  9. 2003 or older
Platform
  1. Windows
  2. Web
Hi All,

is there a Macro for adding If Formula to all the cells that are present in a selected range. For example I have a main datasheet where the data's are cell reference to sub data sheet. Now I want to add IF statement to all the cells instructing it to give a blank cell if the value present in the cell referenced is zero and give the value in the cell if the cell referenced has a value in it ? Attached is a image of the formula that should look like when if statements get added
1707372355849.png
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
You could try this:
VBA Code:
Sub ChangeFormula()
    Const clngCol As Long = 4
    Dim lngRow As Long
    For lngRow = 10 To 17
        With ActiveSheet.Cells(lngRow, clngCol)
            If .HasFormula Then
                .FormulaR1C1 = "=LET(x," & Mid(.FormulaR1C1, 2) & ",IF(x=0,"""",x))"
            End If
        End With
    Next
End Sub
Note: you have to change the clngCol value to the number of the column, and the "For lngRow = 10 to 17" to reflect your row numbers.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,672
Members
452,937
Latest member
Bhg1984

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