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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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,237
Messages
6,123,811
Members
449,127
Latest member
Cyko

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