If you don't use an explicit worksheet reference, Excel uses the active sheet
So Rows(1).Delete would work on Sheet1 the way you think IF Sheet1 is the Activesheet
If Sheet99 is the active sheet, then row 1 on Sheet99 would be deleted
So use Worksheets("Sheet2").Rows(1).Delete or ws.Rows(1).Delete
Marked some places, and made some suggestions (can't help myself
)
Option Explicit
Sub Delete()
Dim ws As Worksheet
Dim lastrow As Long
Dim i As Long
With Application
.Calculation = xlCalculationManual
.EnableEvents = False
.ScreenUpdating = False
End With
For Each ws In ThisWorkbook.Worksheets
'assume any dot-property (e.g. .Name, .Range, etc.) is to the current ws from the loop
With ws
Application.StatusBar = "Cleaning up Worksheet " & .Name
'easy way to avoid a lot or If/Thens
If Not TryMatch(Lookup:=.Name, Lookin:=wsControl.ListObjects("tblTarget").DataBodyRange) Then GoTo NextWS
'consolidated
.Range("A:E,M:N,Q:R").Delete
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
'I think the main problem was not having a 'dot' on many properties so the ActiveSheet (whatever it was) was always used
For i = lastrow To 1 Step -1
If TryMatch(Lookup:=.Cells(i, "A").Interior.Color, _
Lookin:=wsControl.ListObjects("tblColours").ListColumns(2).DataBodyRange) Then
.Rows(i).Delete ' no dot
End If
Next i
.Rows("1:1").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove ' no dot
'no dot
.Range("A1:J1").Value = Array("EE #", "mfg", "Serial #", "Initial Status", "Final Status", "Cost", "Description", "CSN", "CSN Description", "Category")
'easier for me to read
Call FormatWS(.Name)
End With
NextWS:
Next ws
With Application
.StatusBar = False
.Calculation = xlCalculationAutomatic
.EnableEvents = True
.ScreenUpdating = True
End With
End Sub
Private Function getColour(ByRef cell As Range) As Long
Dim intColor As Long
Dim RGB As String
getColour = cell.Interior.Color
End Function
Private Function TryMatch(ByVal Lookup As Variant, ByRef Lookin As Variant) As Boolean
Dim res As Long
On Error Resume Next
res = Application.Match(Lookup, Lookin, 0)
TryMatch = res <> 0
On Error GoTo 0
End Function
Private Sub FormatWS(s As String)
With Worksheets(s)
.Activate
.Cells(1, 1).CurrentRegion.EntireColumn.ColumnWidth = 200
.Cells(1, 1).CurrentRegion.EntireRow.AutoFit
.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit
.Range("A2").Select
End With
With ActiveWindow
.SplitColumn = 0
.SplitRow = 1
.FreezePanes = True
End With
End Sub