Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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
Excel 如果在字符串中找到值,如何删除多行?_Excel_Vba - Fatal编程技术网

Excel 如果在字符串中找到值,如何删除多行?

Excel 如果在字符串中找到值,如何删除多行?,excel,vba,Excel,Vba,如果在单元格中找到一个特定的单词(“底部”),我将尝试删除多行。我在A列中的数据如下所示: OMC Hold Value Bottom 13% Advertising and Marketing WPP Sell Momentum | B VGM Bottom 13% Advertising and Marketing AIR Sell Growth Top 9% Aerospace - Defense Equipment (...) …所以我要做的是从A4开始,如果单元格包含单词“Botto

如果在单元格中找到一个特定的单词(“底部”),我将尝试删除多行。我在A列中的数据如下所示:

OMC
Hold
Value
Bottom 13%
Advertising and Marketing
WPP
Sell
Momentum | B VGM
Bottom 13%
Advertising and Marketing
AIR
Sell
Growth
Top 9%
Aerospace - Defense Equipment
(...)
…所以我要做的是从A4开始,如果单元格包含单词“Bottom”,它需要删除单元格A1、A2、A3、A4和A5

我尝试了以下代码:

Sub DeleteRow()

Dim N As Long, i As Long
N = Cells(Rows.Count, "A").End(xlUp).Row
For i = 4 To N
ActiveSheet.Cells(i, 1).Select
If InStr(1, ActiveCell.Value, "Bottom", vbTextCompare) <> 0 Then
ActiveCell.Offset(-3, 0).Delete
ActiveCell.Offset(-3, 0).Delete
ActiveCell.Offset(-3, 0).Delete
ActiveCell.Offset(-3, 0).Delete
ActiveCell.Offset(-3, 0).Delete 


ElseIf InStr(1, ActiveCell.Value, "Bottom", vbTextCompare) = 0 Then
i = i + 4
End If
Next i

End Sub
…我只是不知道如何处理行被删除和上移的情况

我在这里看到了一个潜在的解决方案:


但是我不知道如何实现。

如果我从示例中正确理解了您的问题,那么您确实需要删除数据中最后一行“底线”以上的所有内容,再加上一行

Excel有一个强大的功能,可以在以下范围的“查找”方法中执行此搜索:

Dim SearchData作为范围,LastBottom作为范围 Set SearchData=Range(“A1”,Range(“A1”)。End(xlDown))”假定没有空行

设置lastbooth=SearchData.Find(“底部”,SearchDirection:=xlPrevious) 如果LastBottom不算什么 MsgBox“找不到它” 其他的 范围(行(1),行(LastBottom.Row+1)。删除 如果结束

备注: 如注释所述,当我使用“End(xlDown)”查找数据的结尾时,我假设数据没有空行,而不是电子表格最后一行的“xlUp”

因此,您可以看到Find函数,“xlPrev”搜索方向从底部向上移动,找到最后一个“底部”单元格

如果要删除整行,请使用Row()函数,如果要删除多行,请使用Range(rows(X)、rows(Y))在上运行“.delete”方法,然后在一个命令中删除整行。

尝试:

Dim SearchData as Range, LastBottom as Range Set SearchData = Range("A1",Range("A1").End(xlDown)) ' assumes no blank lines

Set LastBottom= SearchData.Find("Bottom ",SearchDirection:=xlPrevious) If LastBottom IS Nothing Then MsgBox "Couldn't find it" Else Range(Rows(1),Rows(LastBottom.Row+1).Delete End if
选项显式
子测试()
昏暗的最后一排一样长,我一样长
使用此工作簿。工作表(“表1”)
LastRow=.cells(.Rows.Count,“A”).End(xlUp).Row
对于i=最后一行-1到1步骤-5
如果InStr(.Range(“A”和i).Value,“Bottom”)为0,则
.行(i+1)。删除
.第(i)行。删除
.行(i-1)。删除
.行(i-2).删除
.行(i-3).删除
如果结束
接下来我
以
端接头
请尝试使用此代码

此代码使用方法获取包含单词“bottom”的单元格,如果找到,则从上面3行到下面1行的单元格

Option Explicit

Sub test()

    Dim LastRow As Long, i As Long

    With ThisWorkbook.Worksheets("Sheet1")

        LastRow = .cells(.Rows.Count, "A").End(xlUp).Row

        For i = LastRow - 1 To 1 Step -5

            If InStr(.Range("A" & i).Value, "Bottom") <> 0 Then
                .Rows(i + 1).Delete
                .Rows(i).Delete
                .Rows(i - 1).Delete
                .Rows(i - 2).Delete
                .Rows(i - 3).Delete
            End If

        Next i

    End With

End Sub
参考:

最后一行给了我一个错误,但感谢您的输入!用F8逐行检查代码;在最后一行执行之前,找出“LastBottom.Row”的值。它可能没有。脚本中出现错误的一个原因是,如果“Bottom”,LastBottom设置为“Nothing”根本找不到。该行前面可能必须有“If not lastbooth is Nothing Then”的保护,以便在没有可删除内容时不会执行。我已编辑解决方案以解决该问题,现在它将弹出一条消息,如果找不到内容,则不会尝试最后一行。您可能需要检查搜索字符串-是否为“bottom”或者“底部”,诸如此类的东西。可能需要删除尾随空格或类似的内容。
Option Explicit

Sub test()

    Dim LastRow As Long, i As Long

    With ThisWorkbook.Worksheets("Sheet1")

        LastRow = .cells(.Rows.Count, "A").End(xlUp).Row

        For i = LastRow - 1 To 1 Step -5

            If InStr(.Range("A" & i).Value, "Bottom") <> 0 Then
                .Rows(i + 1).Delete
                .Rows(i).Delete
                .Rows(i - 1).Delete
                .Rows(i - 2).Delete
                .Rows(i - 3).Delete
            End If

        Next i

    End With

End Sub
Sub DeleteRow()
  Dim R As Range, cellsToDel As String
  'looking for cells that contain the word 'bottom'
  Set R = ActiveSheet.Range("A:A").Find("Bottom", LookIn:=xlValues)
  'if found
  Do While Not R Is Nothing
    'get cells address to be deleted
    cellsToDel = "A" & R.Row - 3 & ":A" & R.Row + 1
    'delete the cells
    ActiveSheet.Range(cellsToDel).Delete xlShiftUp
    'looking for cells that contain the word 'bottom' again
    Set R = ActiveSheet.Range("A:A").Find("Bottom", LookIn:=xlValues)
  Loop
End Sub