vba vbscript 使用特定条件删除 Excel 工作表中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19164830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:38:00  来源:igfitidea点击:

vbscript to delete rows in an excel sheet using a specific condition

vbaexcel-vbavbscriptexcel

提问by kedarnath

I have an excel sheet with a columncontaining data as follows and there is data in other columns.

我有一个 Excel 工作表,其中一包含如下数据,其他列中有数据。

Here there are blank cells between the rows. Between 1014,1027 there is 1 blank Cell. There are 3 blank Cells between 1027 and other 1027 and so on..

这里的行之间有空白单元格。在 1014,1027 之间有 1 个空白单元格。1027和其他1027之间有3个空白单元格等等..

Script should delete the rows with more than one blank cell in "column A" between the rows that contains data in "column A".

脚本应删除包含“A 列”数据的行之间的“A 列”中包含多个空白单元格的行。

Eg: Script should delete the rows 1027(starting row always has a value) and next three empty rows, so it should not delete the rows if the gap between two non-empty values is 1. The same process should be done for the entire sheet.

例如:脚本应该删除第 1027 行(起始行总是有一个值)和接下来的三个空行,所以如果两个非空值之间的差距为 1,它就不应该删除这些行。 整个过程应该做同样的过程床单。

There are more than one empty cell between the following values.

以下值之间有多个空单元格。

Code

代码

1014

1014

1027

1027

1027

1027

1033

1033

1033

1033

1033

1033

1020

1020

1033

1033

1008

1008

Please suggest me on this.

请给我建议。

回答by Gary's Student

Give this a try:

试试这个:

Sub rowKiller()
    Dim N As Long, i As Long

    N = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 2 To N - 1
        If Cells(i - 1, 1) <> "" And Cells(i, 1) = "" And Cells(i + 1, 1) <> "" Then
            Cells(i, 1).Value = "XXX"
        End If
    Next i

    Range("A:A").Cells.SpecialCells(xlCellTypeBlanks).EntireRow.Delete

    Range("A:A").Replace "XXX", ""

End Sub