Macro to Insert Formula

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have data columms AA to AG from Sheet BTR ERT-ATREW to the last sheet


I have the folowing formulaa in Col B:D from row 11 onwards

Code:
 ColB                                                 Col C                                             Col D
=IF(AA2="","",AA2)                            =IF(AD2="","",AD2)                  =IF(AB2="","",AB2)


When running the formula below., it insert the folowing formula

Code:
=IF(#REF!="","",#REF!)

Code:
 Sub Formula_Branches()
Dim I As Long, LR As Long
Application.ScreenUpdating = False
For I = Sheets("BTR ERT-ATREW").Index To Worksheets.Count
With Sheets(I)
 LR = .Cells(.Rows.Count, "AA").End(xlUp).Row
     .Range("B11:B" & LR).FormulaR1C1 = "=IF(R[-9]C[25]="""","""",R[-9]C[25])"
    .Range("C11:C" & LR).FormulaR1C1 = "=IF(R[-9]C[27]="""","""",R[-9]C[27])"
    .Range("D11:D" & LR).FormulaR1C1 = "=IF(R[-9]C[24]="""","""",R[-9]C[24])"
End With
Next I
Application.ScreenUpdating = False
End Sub


It would be appreciated if someone could amend my formula
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi howard,

running the given code on a sample workbook brings up this for me:

Cell Formulas
RangeFormula
B11:B21B11=IF(AA2="","",AA2)
C11:C21C11=IF(AD2="","",AD2)
D11:D21D11=IF(AB2="","",AB2)


All checks (Formatting, Locked, Merged) just show the same result for me.

Holger
 
Upvote 0
Hi howard,

aside from getting information that there is a problem with the contents of the workbook, there are links to external data, and after letting Excel restore the workbook the last filled row on sheet BTR ERT-ATREW in Column AA is 2, for sheet BGH ERT-ATREW there is no data in Column AA and the broken references are located in Row 10 (while your code starts with Row 11).

Since the result will only be shown if there is a value in the corresponding cell try

VBA Code:
Sub Formula_Branches()
Dim I As Long
Application.ScreenUpdating = False
For I = Sheets("BTR ERT-ATREW").Index To Worksheets.Count
  With Sheets(I)
    .Range("B11:B23").FormulaR1C1 = "=IF(R[-9]C[25]="""","""",R[-9]C[25])"
    .Range("C11:C23").FormulaR1C1 = "=IF(R[-9]C[27]="""","""",R[-9]C[27])"
    .Range("D11:D23").FormulaR1C1 = "=IF(R[-9]C[24]="""","""",R[-9]C[24])"
  End With
Next I
Application.ScreenUpdating = False
End Sub

Holger
 
Upvote 0
Solution
Hi Holger


Many thanks for your input and help

Code works 100%
 
Upvote 0

Forum statistics

Threads
1,215,007
Messages
6,122,670
Members
449,091
Latest member
peppernaut

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