VBA Loop only executes on first file.

BenGee

Board Regular
Joined
Mar 5, 2016
Messages
195
Hi

The below only executes on the first file within the applicable folder and am wondering if someone could help me with this please kindly?

I need it to loop through all open files and apply the stated formulas.

VBA Code:
Dim MyFolder As String
Dim MyFile As String
Dim WB As Workbook

Application.ScreenUpdating = False

MyFolder = "\\Test\Data\" & Range("A1")
MyFile = Dir(MyFolder & "\*.xls")
Do While MyFile <> ""
Workbooks.Open filename:=MyFolder & "\" & MyFile
MyFile = Dir
Loop

For Each WB In Workbooks
    If WB.Name <> "AHT" And WB.Name <> "0005*" Then
        Lastrow = Range("C" & Rows.Count).End(xlUp).Row
        Range("H9").Formula = "=E9-D9"
        Range("H9").AutoFill Destination:=Range("H9:H" & Lastrow)
        Range("H9:H" & Lastrow).NumberFormat = "hh:mm:ss"
        Range("I9").Formula = "=SUMIF(C9:C1000,""CALL FOLLOW UP"",H9:H1000)"
        Range("I9").NumberFormat = "hh:mm:ss"
    End If
Next

Application.ScreenUpdating = True

Thanks
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Which sheet within each workbook should the formulas be put on?
 
Upvote 0
In that case try
VBA Code:
For Each wb In Workbooks
    If wb.Name <> "AHT" And wb.Name <> "0005*" Then
      With wb.Sheets(1)
        LastRow = .Range("C" & .Rows.Count).End(xlUp).Row
        .Range("H9:H" & LastRow).Formula = "=E9-D9"
        .Range("H9:H" & LastRow).NumberFormat = "hh:mm:ss"
        .Range("I9").Formula = "=SUMIF(C9:C1000,""CALL FOLLOW UP"",H9:H1000)"
        .Range("I9").NumberFormat = "hh:mm:ss"
      End With
    End If
Next
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,255
Messages
6,123,896
Members
449,132
Latest member
Rosie14

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