【问题标题】:VBA Excel - Delete 2 rows above the row with a specific keywordVBA Excel - 删除具有特定关键字的行上方的 2 行
【发布时间】:2015-02-04 03:24:53
【问题描述】:

我对 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 行的行。 我需要的最终数据只是具有跳过路径的行,即解密失败。

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

尝试了以下一种:

Sub 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

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

【问题讨论】:

  • 帮我写一个脚本通常意味着您到目前为止已经完成了一些工作。请展示你到目前为止所做的尝试。有大量关于读取文本文件/等的基本 I/O 函数的文档。在网络上:)
  • 使用尝试的脚本编辑帖子

标签: excel vba


【解决方案1】:

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

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

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

结果:

【讨论】:

  • 工作就像一个魅力!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多