Code to Edit Formula in Many workbooks

proctk

Well-known Member
Joined
Dec 24, 2004
Messages
839
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
 

Some videos you may like

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,494
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
839
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,494
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
839
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
 

Watch MrExcel Video

Forum statistics

Threads
1,119,139
Messages
5,576,308
Members
412,716
Latest member
thviid
Top