excel - VBA Excel - 删除具有特定关键字的行上方的 2 行

标签 excel vba

我对 VBA 知之甚少,因此我需要有人帮助我编写脚本以实现标题中所述的结果。

我需要从我拥有的日志文件中提取特定行。

excel中实际日志文件的摘录:

=====================================

Encrypted file: C:\[specific path]      
Algorithm picked for decryption: RC4        
Status: Successfully decrypted!     

Encrypted file: C:\[specific path]          
File looks like it is not encrypted. Skipping ...       

Encrypted file: C:\[specific path]      
File could not be decrypted properly. Skipping ...      

=====================================

大约 90k 行是这样的。我需要摆脱所有“状态:成功解密!”上面有 2 行的行。
我需要的最终数据只是具有跳过路径的行,即解密失败。

此日志中的每个文本 block 后面都有空白行。

尝试了以下一种:

子 DeleteRowsBelow()
Dim x As Long
Dim y As Long

x = ActiveSheet.UsedRange.Rows.Count

Cells.Find(What:="Macro", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Select
y = Selection.Row + 1
Rows(x & ":" & y).EntireRow.Delete

结束子

但它会删除某个关键字下方的所有行,所以这对我不起作用。

然后尝试将以下内容合并到上述脚本中:

Rows("1:"& Cells.Find("KEY WORD").Row - 1).Delete

但也运气不佳,它再次删除了某个关键字上方的所有行。

最佳答案

不确定这是否是您正在寻找的,但也许会有所帮助

只需选择列表顶部的单元格并运行宏

Sub DeleteSuccessfulRows()

Application.ScreenUpdating = False
Dim x
For x = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row To ActiveCell.Row Step -1
    If Cells(x, 1) = "Status: Successfully decrypted!" Then 'If we find this text
        Cells(x, 1).EntireRow.Delete      'Delete the entire row
        Cells(x - 1, 1).EntireRow.Delete  'Delete the row above it
        Cells(x - 2, 1).EntireRow.Delete  'Delete the row 2 rows above it
        x = x - 2
    'Delete blank rows
    ElseIf Cells(x, 1) = vbNullString Then Cells(x, 1).EntireRow.Delete
    'Optional delete rows that contain "File looks like ..."
    'ElseIf Cells(x, 1) = "File looks like it is not encrypted. Skipping ..." Then Cells(x, 1).EntireRow.Delete
    'ElseIf Cells(x, 1) = "File could not be decrypted properly. Skipping ..." Then Cells(x, 1).EntireRow.Delete
    End If
Next x
Application.ScreenUpdating = True

End Sub

结果:

After

关于excel - VBA Excel - 删除具有特定关键字的行上方的 2 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28312989/

相关文章:

excel - 如何轻松更改 Excel 中文件的硬编码链接?

excel - 用 Cribbage 手数对

readxl::read_xls 返回 "libxls error: Unable to open file"

vba - Excel VBA 中大约每 10,000 次迭代出现无法解释的类型不匹配错误

forms - 访问2010 : Selected record value from subform

excel - 如何根据标准连接单元格范围

VBA复制表和重命名错误

sql-server-2008 - 如何在 Microsoft Access 2010 中设置与 SQL Server 2008 的 ADODB 连接?

excel - 在 Excel VBA 宏中使用 Azure 翻译器

C#/ASP.NET Oledb - MS Excel 读取 "Unspecified error"