gpt4 book ai didi

vba - 创建 Excel 宏以一次删除多行

转载 作者:行者123 更新时间:2023-12-04 21:25:49 26 4
gpt4 key购买 nike

我在网上找到了一个代码,并想对其进行编辑。代码在 VBA 中,我希望宏代码删除多行而不是一行。这是代码:

Sub findDelete()
Dim c As String
Dim Rng As Range

c = InputBox("FIND WHAT?")

Set Rng = Nothing

Set Rng = Range("A:A").Find(what:=c, _
After:=Range("A1"), _
LookIn:=xlFormulas, _
lookat:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

Rng.EntireRow.Delete shift:=xlUp
End Sub

最佳答案

不要使用查找,而是使用 Autofilter并删除 VisibleCells

Sub findDelete()

Dim c As String, Rng As Range, wks as Worksheet

c = InputBox("FIND WHAT?")

Set wks = Sheets(1) '-> change to suit your needs
Set Rng = wks.Range("A:A").Find(c, After:=Range("A1"), LookIn:=xlFormulas, _
lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False)

If Not Rng Is Nothing Then

With wks

.Range(.Range("A1"), .Range("A" & .Rows.Count).End(xlUp)).AutoFilter 1, c
Set Rng = Intersect(.UsedRange, .UsedRange.Offset(1), .Range("A:A")).SpecialCells(xlCellTypeVisible)
Rng.Offset(1).EntireRow.Delete

End With

End If

End Sub

编辑

要将 InputBox 替换为多个值以查找/删除,请执行以下操作:
Option Explicit

Sub FindAndDeleteValues()

Dim strValues() as String

strValues() = Split("these,are,my,values",",")

Dim i as Integer

For i = LBound(strValues()) to UBound(strValues())

Dim c As String, Rng As Range, wks as Worksheet
c = strValues(i)

'.... then continue with code as above ...

Next

End Sub

关于vba - 创建 Excel 宏以一次删除多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13032645/

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