We help IT Professionals succeed at work.

For Each wsWorksheet code not working

mato01
mato01 asked
on
THi Experts,

The below code is only working on first worksheet in Workbook.  It is not looping through all.


Sub RemoveBegin()

Dim wsWorksheet As Worksheet
Dim wbWorkbook As Workbook

For Each wsWorksheet In ActiveWorkbook.Worksheets
     
            Columns("A:K").Select
            Application.CutCopyMode = False
            Selection.Copy
            Columns("P:P").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
            Columns("A:O").Select
            Application.CutCopyMode = False
            Selection.Delete Shift:=xlToLeft
            Rows("1:5").Select
            Selection.Delete Shift:=xlUp
            Range("A1").Select
                 
    Next wsWorksheet
End Sub

Thanks in Advance
Sub RemoveBegin()

Dim wsWorksheet As Worksheet
Dim wbWorkbook As Workbook

For Each wsWorksheet In ActiveWorkbook.Worksheets
      
            Columns("A:K").Select
            Application.CutCopyMode = False
            Selection.Copy
            Columns("P:P").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
            Columns("A:O").Select
            Application.CutCopyMode = False
            Selection.Delete Shift:=xlToLeft
            Rows("1:5").Select
            Selection.Delete Shift:=xlUp
            Range("A1").Select
                 
    Next wsWorksheet
End Sub

Open in new window

Comment
Watch Question

Owner
Commented:
You need to activate the worksheet first, or the Active Worksheet remains the one where you started. Just add wsWorksheet.Activate to the start of your loop (above the Columns("A:K")... statement.