Apply formula to xlsx file

conditions
1) both  the files are located in same place i.e.    C:\Users\Sholtan\Desktop
2)vba code will be placed in analyser.xlsb
3)Important  xlsx file name may differ
steps
1)open xlsx file
go to U column
apply the formula as shown in the xlsx file
2) go to V column
apply the formula as shown in the xlsx file
and save the xlsx file and close it
Analyser.xlsb
1.xlsx
Sachin SinghAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Please give this a try...

Sub ApplyFormulas()
Dim fso As Object
Dim FolderPath As String
Dim srcFolder As Object
Dim file As Object
Dim wb As Workbook
Dim ws As Worksheet
Dim lr As Long

Application.ScreenUpdating = False

Set fso = CreateObject("Scripting.FileSystemObject")

FolderPath = ThisWorkbook.Path
Set srcFolder = fso.GetFolder(FolderPath)

For Each file In srcFolder.Files
    If fso.GetExtensionName(file) = "xlsx" Then
        Set wb = Workbooks.Open(file)
        Set ws = wb.Sheets(1)
        lr = ws.Cells(Rows.Count, 1).End(xlUp).Row
        ws.Range("U2:U" & lr).Formula = "=IFERROR((H2-I2)/I2,"""")"
        ws.Range("V2:V" & lr).Formula = "=IFERROR((H2-J2)/J2,"""")"
        wb.Close True
    End If
    Set wb = Nothing
    Set ws = Nothing
Next file
Application.ScreenUpdating = True
End Sub

Open in new window

0
Sachin SinghAuthor Commented:
Neeraj Sir this code is perfect  but i need the result in percentage  as shown  in the sample file
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Then try this...

Sub ApplyFormulas()
Dim fso As Object
Dim FolderPath As String
Dim srcFolder As Object
Dim file As Object
Dim wb As Workbook
Dim ws As Worksheet
Dim lr As Long

Application.ScreenUpdating = False

Set fso = CreateObject("Scripting.FileSystemObject")

FolderPath = ThisWorkbook.Path
Set srcFolder = fso.GetFolder(FolderPath)

For Each file In srcFolder.Files
    If fso.GetExtensionName(file) = "xlsx" Then
        Set wb = Workbooks.Open(file)
        Set ws = wb.Sheets(1)
        lr = ws.Cells(Rows.Count, 1).End(xlUp).Row
        ws.Range("U2:U" & lr).Formula = "=IFERROR((H2-I2)/I2,"""")"
        ws.Range("V2:V" & lr).Formula = "=IFERROR((H2-J2)/J2,"""")"
        ws.Range("U2:V" & lr).NumberFormat = "0%"
        wb.Close True
    End If
    Set wb = Nothing
    Set ws = Nothing
Next file
Application.ScreenUpdating = True
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sachin SinghAuthor Commented:
Thnx Neeraj Sir for ur Great Support
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome Sachin!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.