Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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 删除包含特定值VBA的行_Excel_Vba_Delete Row - Fatal编程技术网

Excel 删除包含特定值VBA的行

Excel 删除包含特定值VBA的行,excel,vba,delete-row,Excel,Vba,Delete Row,我希望创建一个简单的过程,搜索“In Shout”列,搜索该列中的所有“#N/a”并删除这些行 当我运行下面的程序时,它不会删除行,我也不知道为什么。有人知道为什么吗 Sub DeleteBadRows() Dim InShout As Long Dim NA As String NA = "#N/A" 'Declaring year value of 1 month & 2 month 'This is impo

我希望创建一个简单的过程,搜索“In Shout”列,搜索该列中的所有“#N/a”并删除这些行

当我运行下面的程序时,它不会删除行,我也不知道为什么。有人知道为什么吗

Sub DeleteBadRows()

    Dim InShout As Long
    Dim NA As String
    
    NA = "#N/A"

    'Declaring year value of 1 month & 2 month
    'This is important to compare datasets from 2 months ago & last month
    Year_2M = Format(Date - 57, "YYYY")

    'Declaring month value of 1 month & 2 month
    'This is important to compare datasets from 2 months ago & last month
    Month_2M = Format(Date - 57, "MM")

    'This translates the current month from number to character format
    MonthChar_2 = MonthName(Month_2M, False)

    sheet = "MASTERFILE_" & Year_2M & Month_2M
    
    'setting string values so that we can identify open workbooks
    myFile = "Dataset"
    otherFile = "Monthly Reporting Tool"
    shoutFile = "Copy of Daily Shout"
    
        'if tool wb is open, declare it as MonthlyRepTool
    For Each wb In Application.Workbooks
        If wb.Name Like otherFile & "*" Then
           Set MonthlyRepTool = Workbooks(wb.Name)
        End If
    Next wb
    
        With MonthlyRepTool.Worksheets(sheet).Rows(1)
            Set e = .Find("In Shout?", LookIn:=xlValues)
            InShout = e.Column
        End With

    lastRow = MonthlyRepTool.Worksheets(sheet).Cells(Rows.count, "A").End(xlUp).Row

    For i = 2 To lastRow Step -1

        If MonthlyRepTool.Worksheets(sheet).Cells(i, InShout).value = NA Then
            MonthlyRepTool.Worksheets(sheet).Rows(i).EntireRow.Delete
        End If
        
    Next i

End Sub

当您应该测试错误值时,您正在使用文本“N/a”的测试。如果您修改下面的代码,我认为这可能会解决您的问题。另外,(正如蒂姆·威廉姆斯指出的),你走错了方向。他的回答说明了一个问题。下面的内容从下到下变化。在过程结束时删除行也更有效。我包含了实现这一点的代码

Dim KillRange As Range

'change 
For i = 2 To lastRow

If IsError(MonthlyRepTool.Worksheets(Sheet).Cells(i, InShout).Value) Then
    If KillRange Is Nothing Then
        Set KillRange = MonthlyRepTool.Worksheets(Sheet).Rows(i).EntireRow
    Else
        Set KillRange = Union(KillRange, MonthlyRepTool.Worksheets(Sheet).Rows(i).EntireRow)
    
    End If
    
End If

Next i

'Delete after the loop
If Not KillRange Is Nothing Then
    KillRange.Delete
End If

当您应该测试错误值时,您正在使用文本“N/a”的测试。如果您修改下面的代码,我认为这可能会解决您的问题。另外,(正如蒂姆·威廉姆斯指出的),你走错了方向。他的回答说明了一个问题。下面的内容从下到下变化。在过程结束时删除行也更有效。我包含了实现这一点的代码

Dim KillRange As Range

'change 
For i = 2 To lastRow

If IsError(MonthlyRepTool.Worksheets(Sheet).Cells(i, InShout).Value) Then
    If KillRange Is Nothing Then
        Set KillRange = MonthlyRepTool.Worksheets(Sheet).Rows(i).EntireRow
    Else
        Set KillRange = Union(KillRange, MonthlyRepTool.Worksheets(Sheet).Rows(i).EntireRow)
    
    End If
    
End If

Next i

'Delete after the loop
If Not KillRange Is Nothing Then
    KillRange.Delete
End If
应该是

For lastRow  to 2 Step -1
应该是

For lastRow  to 2 Step -1

测试你的代码。两件事:

  • 如果MonthlyRepTool.Worksheets(sheet).Cells(i,InShout).value=NA,则替换为

    如果Application.IsNa(MonthlyRepTool.Worksheets(Sheet).Cells(i,InShout.Value),则使用

  • 对于i=2到最后一行步骤-1,不是

    使用
    将i=lastRow转换为2步骤-1


  • 测试你的代码。两件事:

  • 如果MonthlyRepTool.Worksheets(sheet).Cells(i,InShout).value=NA,则替换为

    如果Application.IsNa(MonthlyRepTool.Worksheets(Sheet).Cells(i,InShout.Value),则使用

  • 对于i=2到最后一行步骤-1,不是

    使用
    将i=lastRow转换为2步骤-1


  • 您的“#N/A”在有公式的单元格中?或者它们只是粘贴值?它们被粘贴为值。
    #N/A
    实际上是一种值,而不是文本。与在单元格中键入
    true
    类似,它不等于键入的单元格
    =“true”
    是格式为文本的“#N/a”单元格还是“常规”?这会有所不同。不需要循环;)只需使用Autofilter过滤
    #N/A
    并删除行即可。会让你开始的。使用autofilter最好的地方是,您不必担心
    #N/A
    是来自公式还是粘贴为文本。单元格的格式是文本还是常规也无关紧要:)您的“#N/A”位于有公式的单元格中?或者它们只是粘贴值?它们被粘贴为值。
    #N/A
    实际上是一种值,而不是文本。与在单元格中键入
    true
    类似,它不等于键入的单元格
    =“true”
    是格式为文本的“#N/a”单元格还是“常规”?这会有所不同。不需要循环;)只需使用Autofilter过滤
    #N/A
    并删除行即可。会让你开始的。使用autofilter最好的地方是,您不必担心
    #N/A
    是来自公式还是粘贴为文本。单元格的格式是文本格式还是常规格式也无关紧要:)哦。。。。是的,可能就是这样。哦。。。。是的,可能就是这样。您可以使用更具体的
    Application.IsNa()
    ,而不是
    IsError()
    。我想指定类似的内容,但我想OP会删除该单元格中出现错误的任何行。不过,您是对的,您的方法将在外科手术上更加具体。您可以使用更加具体的
    Application.IsNa()
    ,而不是
    IsError()
    。我曾想过指定类似的内容,但我想OP会删除该单元格中出现错误的任何行。不过你是对的,你的方法在这个问题上会更加外科化。对于1,没有提到N/A——这是故意的吗?此外,我确实使用OptionExplicit。每个未提及的变量都已在不同的模块中公开声明,因为我在我的大多数项目中都使用它们。很抱歉,我将删除第3条语句。我对代码进行了编辑,专门解释了NA错误。以前我使用了
    IsError
    ,它可以捕获所有类型的错误。非常感谢ZygD。对于1,没有提到N/A-这是故意的吗?此外,我确实使用OptionExplicit。每个未提及的变量都已在不同的模块中公开声明,因为我在我的大多数项目中都使用它们。很抱歉,我将删除第3条语句。我对代码进行了编辑,专门解释了NA错误。以前我使用了
    IsError
    ,它可以捕获所有类型的错误。非常感谢ZygD。