For Each ws Loop Not Working

BrittKnee

Board Regular
Joined
Dec 4, 2017
Messages
82
Back again. I am trying to use For Each ws to loop a formatting macro through all worksheets except one. The issue is, it seems that the macro is looping the formatting code through only the first worksheet (it's not actually moving on to the next). I have no idea where I am going wrong. Below is the code that I am using. TIA

Code:
Option Explicit

Sub Combine_Exports()

Dim FolderPath As String
Dim Filename As String
Dim ws As Worksheet
Dim wkb As Workbook
Dim WS_Count As Integer
Dim I As Integer


'Copy worksheets to master file

Application.ScreenUpdating = False
Application.DisplayAlerts = False

FolderPath = "C:\Documents\"
Filename = Dir(FolderPath & "*.xlsx*")
Do While Filename <> ""
 Workbooks.Open Filename:=FolderPath & Filename, ReadOnly:=True
 For Each ws In ActiveWorkbook.Sheets
 ws.Copy After:=ThisWorkbook.Sheets(1)
 Next ws
 Workbooks(Filename).Close
 Filename = Dir()
Loop

'Format Worksheet Data

For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Macro" Then
    
    Call Format_WS
    
End If

Next ws

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

Option Explicit

Sub Format_WS()

Dim ws As Sheets
Dim wkb As Workbook
Dim Rng As Range
Dim I As Long
Dim FinalRow As Long
Dim LRow As Long


Application.ScreenUpdating = False
Application.DisplayAlerts = False


'Delete Rows

Range("A1:A4").EntireRow.Delete

'Format Fields

Range("C1") = Range("C1") & " " & Range("C2")
Range("F1") = "Grade"
Range("G1") = Range("G1") & " " & Range("G2")
Range("H1") = Range("H1") & " " & Range("H2")
Range("J1") = Range("J1") & " " & Range("J2")
Range("K1") = Range("K1") & " " & Range("K2")
Range("L1") = Range("L1") & " " & Range("L2")


Range("A1:L2").Cut Destination:=Range("B1")

Range("A2").EntireRow.Delete

Range("A1").Select
ActiveCell.Value = "Unit #"

Range("M:N").EntireColumn.Delete

'Delete Cells with Text in Column A (Unit #)

FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
 
For I = FinalRow To 2 Step -1
 
    If IsEmpty(Cells(I, 4)) = True Then
 
        Cells(I, 4).EntireRow.Delete
    End If
 
Next I


       
Application.ScreenUpdating = True

Application.DisplayAlerts = True

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I think the reason it is not working is becasu your format_ws macro is operating on the active worksheet, yet your loop round the workhseet never changes the active workhseet so try this modification :
VBA Code:
For Each ws In ThisWorkbook.Worksheets
    If ws.Name <> "Macro" Then
      ws.Select                          ' ADD THIS LINE
      Call Format_WS
     End If
Next ws
 
Upvote 0
Solution

Forum statistics

Threads
1,214,823
Messages
6,121,779
Members
449,049
Latest member
greyangel23

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