I want to match Cell value with Sheet name if true keep else delete the entire row
For Each ws In wb.Worksheets
ws.Activate
index = index + 1
If index < 10 Then
irow = Cells(Rows.Count, 1).End(xlUp).Row
temp = ws.Name
newtemp = Replace(temp, " ", "#0")
For J = 2 To irow
If Cells(irow, 1) <> newtemp Then
ActiveSheet.Cells(J, 1).EntireRow.Delete
In your loop If Cells(irow, 1) <> newtemp
is not referring to the loop variable.
Get in the habit of declaring your variables, and using Option Explicit.
There is no need to activate each sheet.
This has a few guesses as not sure of all the details.
Sub x()
Dim wb As Workbook, ws As Worksheet, Index As Long, irow As Long, newtemp As String, j As Long
Set wb = ThisWorkbook '??
For Each ws In wb.Worksheets
Index = Index + 1
If Index < 10 Then
irow = ws.Cells(Rows.Count, 1).End(xlUp).Row
newtemp = Replace(ws.name, " ", "#0")
For j = irow To 2 Step -1
If ws.Cells(j, 1) <> newtemp Then
ws.Cells(j, 1).EntireRow.Delete
End If
Next j
End If
Next ws
End Sub
Try:
Option Explicit
Sub test()
Dim ws As Worksheet
Dim LastRow As Long, i As Long
For Each ws In ThisWorkbook.Worksheets '<-Loop worksheets
With ws
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row '<- Get the Lastrow of column A. Change if needed
For i = LastRow To 1 Step -1 '<- Loop from Lastrow to row 1
If .Range("A" & i).Value <> Replace(ws.Name, " ", "#0") Then
.Rows(i).EntireRow.Delete
End If
Next i
End With
Next ws
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.