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