gpt4 book ai didi

vba - 保留包含特定文本的所有行并删除其余行

转载 作者:行者123 更新时间:2023-12-04 21:34:02 27 4
gpt4 key购买 nike

我正在使用此代码删除所有工作表中具有特定文本的所有行。有什么方法可以保留所有带有特定文本的行并删除其余行?非常感谢您的帮助。

Sub WorksheetLoop()

Dim c As Integer
Dim n As Integer
c = ActiveWorkbook.Worksheets.Count
For n = 1 To c Step 1
Last = Worksheets(n).Cells(Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
If (Worksheets(n).Cells(i, "A").Value) = "Oakville" Then
Worksheets(n).Cells(i, "A").EntireRow.Delete
End If
Next i
Next n
End Sub

最佳答案

例如,如果您想要带有 'Oakville' 的行,那么只需在此代码本身中将 = 更改为 <>

Sub WorksheetLoop()

Dim c As Integer
Dim n As Integer
c = ActiveWorkbook.Worksheets.Count
For n = 1 To c Step 1
Last = Worksheets(n).Cells(Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
If (Worksheets(n).Cells(i, "A").Value) <> "Oakville" Then
Worksheets(n).Cells(i, "A").EntireRow.Delete
End If
Next i
Next n
End Sub

关于vba - 保留包含特定文本的所有行并删除其余行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43765943/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com