如果要让单元格值与工作表名称匹配,请保留,否则请删除整行
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
答案 0 :(得分:1)
循环If Cells(irow, 1) <> newtemp
中没有引用循环变量。
养成声明变量并使用Option Explicit的习惯。
不需要激活每张纸。
由于不确定所有细节,因此有一些猜测。
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
答案 1 :(得分:0)
尝试:
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