Code to Edit Formula in Many workbooks

proctk

Well-known Member
Joined
Dec 24, 2004
Messages
840
Hi I have about 30 workbooks in a folder called "Employee Files. In column

F5 to F16 I have this Formula (=IF(B8=0,"0",SUMPRODUCT(--(Results!$A$5:$A$57>=$P$10),--(Results!$A$5:$A$57<=$P$11),Results!D$5:D$57))/$Q$10)

I need to change it to
=IF(D5=0,"",D5/B5)

Is there a way to write some code to do this or do I have to go in and manually do it

Any thoughts

Thankyou
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,526
Hi,

Try this macro:
Code:
Sub ReplaceFormula()
Dim vFN As Variant, V As Variant
Dim WS As Worksheet

vFN = Application.GetOpenFilename(filefilter:="Excel Files (*.xls),*.xls", _
                                MultiSelect:=True)
Application.ScreenUpdating = False
For Each V In vFN
    Workbooks.Open V
    Sheets("Sheet1").Range("F5:F16").FormulaR1C1 = _
            "=IF(R[0]C[-2]=0,"""",R[0]C[-2]/R[0]C[-4])"
    ActiveWorkbook.Save
    ActiveWorkbook.Close
Next V
Application.ScreenUpdating = True
End Sub
 

proctk

Well-known Member
Joined
Dec 24, 2004
Messages
840
Thank you for the replay, I'm learning VB and read through the code, not shure where I should past it. My guess is in a new work book saved in the same directory. IF so, how could I change it so that I can Add it to my master workbook and go to a folder called "EmployeeFiles"
 

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,526
proctk said:
Thank you for the replay, I'm learning VB and read through the code, not shure where I should past it. My guess is in a new work book saved in the same directory. IF so, how could I change it so that I can Add it to my master workbook and go to a folder called "EmployeeFiles"

Hi,

To install the code, [Alt-F11] then Insert / module & paste code into code window.

This can be any workbook (including you master workbook), the code will prompt you for which excel files you want to amend.

To initially point at c:\EmployeeFiles, insert ther command
Chdir "C:\EmployeeFiles"

at the beginning of the macro, before the line starting
"vFN = Application.GetOpenFilename"
 

proctk

Well-known Member
Joined
Dec 24, 2004
Messages
840
HI below is how I Edited the code. When I run it it pops up the open file window and wants me to select the file to update. How can I change it so that it loops through each file

Code:
Sub ReplaceFormula()
Dim vFN As Variant, V As Variant
Dim WS As Worksheet
ChDir "T:\\CRReporting\EmployeeFiles\"
vFN = Application.GetOpenFilename(filefilter:="Excel Files (*.xls),*.xls", _
                                MultiSelect:=True)
Application.ScreenUpdating = False
For Each V In vFN
    Workbooks.Open V
    Sheets("results").Range("F5:F16").FormulaR1C1 = _
            "=IF(R[0]C[-2]=0,"""",R[0]C[-2]/R[0]C[-4])"
    ActiveWorkbook.Save
    ActiveWorkbook.Close
Next V
Application.ScreenUpdating = True
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,627
Messages
5,832,758
Members
430,165
Latest member
Idjaz

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
Top