Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
vba:删除筛选的行,但不删除第一行(或存储并在删除后粘贴)_Vba_Excel_Runtime Error_Autofilter - Fatal编程技术网

vba:删除筛选的行,但不删除第一行(或存储并在删除后粘贴)

vba:删除筛选的行,但不删除第一行(或存储并在删除后粘贴),vba,excel,runtime-error,autofilter,Vba,Excel,Runtime Error,Autofilter,在Excel 2013中使用Microsoft Visual Basic应用程序7.1版 数据位于从A到D的列上,行数随时间而变化。我想删除列B的值不以LCR开头的所有行(而且我不想用for…next循环)。 比如: Columns("B:B").AutoFilter Field:=1, Criteria1:="<>LCR*" Selection.Delete 列(“B:B”)。自动筛选字段:=1,标准1:=“LCR*” 选择。删除 不幸的是,这段代码删除了标题行(行号1),我不

在Excel 2013中使用Microsoft Visual Basic应用程序7.1版 数据位于从A到D的列上,行数随时间而变化。我想删除列B的值不以LCR开头的所有行(而且我不想用for…next循环)。 比如:

Columns("B:B").AutoFilter Field:=1, Criteria1:="<>LCR*"
Selection.Delete
列(“B:B”)。自动筛选字段:=1,标准1:=“LCR*”
选择。删除
不幸的是,这段代码删除了标题行(行号1),我不想这样做。 我试图将行号1存储在范围变量的其他位置,但它不起作用(运行时错误“424”)

设置r1=范围(“A1:D1”)
r1.副本
列(“B:B”)。自动筛选字段:=1,标准1:=“LCR*”
选择。删除
带范围(“A1:D1”)
.插入移位:=xlDown
.选择
.Value=r1.Value
以
如何告诉筛选器从第二行开始(或者如何正确存储第一行的内容以便在筛选器删除后粘贴)?
提前感谢您的帮助

将您的删除范围定义为
范围(单元格(2,2),单元格(ActiveSheet.UsedRange.Rows.Count,2))
(替换
选择
调用)。这将删除除列中第一个单元格之外的所有内容


编辑以避免excel提示:
范围(单元格(2,2),单元格(ActiveSheet.UsedRange.Rows.Count,2)).EntireRow

以一种更完整的方式,您可以这样做

Sub main()

    With Worksheets("MyWantedSheet") '<--| always specify full worksheet reference (change "MyWantedSheet" with your actual sheet name)
        With .Columns("B:B") '.Resize(.Cells(.Rows.Count, "B").End(xlUp).Row) '<--| refer to wanted column range down to its last non empty cell
            .AutoFilter '<--| remove possible preeeding autofilter filtering
            .AutoFilter Field:=1, Criteria1:="<>LCR*" '<--| apply current filtering
            If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then '<--| if there are visible cells other than the "header" one
                .Resize(.Parent.Cells(.Parent.Rows.Count, "B").End(xlUp).Row - 1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete '<--|delete visible rows other than the first ("headers") one
            End If
        End With
        .AutoFilterMode = False '<--| remove drop-down arrows
    End With

End Sub
Sub-main()

对于工作表(“MyWantedSheet”),它首先提示“删除整行”,然后在“确定”之后工作。你知道任何内置的Ok或“请不要麻烦”吗?我的意思是,任何与应用程序不同的东西。显示警报=False@user3664452如果您有它,可能会选择整个范围,因此
范围(单元格(2,2),单元格(ActiveSheet.UsedRange.Rows.count,2.EntireRow)
。诚然,我从未遇到过vba提示删除的问题。我认为EntireRow调用应该可以解决这个问题(我在删除代码时总是习惯性地使用它,这样回顾我的代码,我就可以清楚地知道删除了什么),如果这不起作用,我相信唯一的其他方法是DisplayAlerts=False。您不想在代码中使用该方法有什么原因吗?如果不希望命令影响代码的其他部分,则始终可以将命令括起来(即删除前为False,删除后返回True)。entirerow解决方案工作且不提示范围(单元格(2,2),单元格(ActiveSheet.UsedRange.Rows.Count,2))。entirerow。Delete@user3664452很高兴听到:)如果你不介意的话,将我的答案标记为已接受,以将其从未回答的问题中删除。是。区别在于对所有可能数据“结构”的处理,即其在工作表中的“绝对”位置和相对于同一工作表中其他数据的“相对”位置。
Sub main()

    With Worksheets("MyWantedSheet") '<--| always specify full worksheet reference (change "MyWantedSheet" with your actual sheet name)
        With .Columns("B:B") '.Resize(.Cells(.Rows.Count, "B").End(xlUp).Row) '<--| refer to wanted column range down to its last non empty cell
            .AutoFilter '<--| remove possible preeeding autofilter filtering
            .AutoFilter Field:=1, Criteria1:="<>LCR*" '<--| apply current filtering
            If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then '<--| if there are visible cells other than the "header" one
                .Resize(.Parent.Cells(.Parent.Rows.Count, "B").End(xlUp).Row - 1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete '<--|delete visible rows other than the first ("headers") one
            End If
        End With
        .AutoFilterMode = False '<--| remove drop-down arrows
    End With

End Sub