编译错误语法错误Vba

问题描述 投票:1回答:2

我编写的脚本用于运行一系列行,如果在其中找不到某些字符串,则删除每一行。我遇到的错误是,当我运行脚本时,我得到了错误

“编译错误语法错误”

并且Instr行突出显示为有错误。

With ActiveSheet
    .Select
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    .DisplayPageBreaks = False
    Firstrow = .UsedRange.Cells(1).Row
    Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
    For Lrow = Lastrow To Firstrow Step -1

        If InStr(.Cells(Lrow,"I").Value,"Removal") = 0 and _
           InStr(.Cells(Lrow,"J").Value,"removal") = 0 and _
           InStr(.Cells(Lrow,"J").Value,"removed") = 0 Then.Rows(Lrow).Delete

    Next Lrow

End With

如果有人知道为什么会这样或有解决方案,那将非常感谢谢谢。

excel vba excel-vba
2个回答
0
投票

你的代码看起来很好。完成对您的帖子的编辑后,您没有看到错误。但我能够复制您发布的原始代码。您的问题是使用换行符“_”而不会破坏该行。 “和”字未被编辑器自动大写的原因是因为这一点。

你的代码:

If InStr(.Cells(Lrow, "I").Value, "Removal") = 0 and _ InStr(.Cells(Lrow, "J").Value, "removal") = 0 and _ InStr(.Cells(Lrow, "J").Value, "removed") = 0 Then .Rows(Lrow).Delete

您可以使用一行:

Sub Testing()
    With ActiveSheet
        .Select
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
        .DisplayPageBreaks = False
        Firstrow = .UsedRange.Cells(1).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
        For Lrow = Lastrow To Firstrow Step -1
        If InStr(.Cells(Lrow, "I").Value, "Removal") = 0 And InStr(.Cells(Lrow, "J").Value, "removal") = 0 And InStr(.Cells(Lrow, "J").Value, "removed") = 0 Then .Rows(Lrow).Delete
        Next Lrow
    End With
End Sub

或者,如果您使用换行符“_”,请务必转到下一行。

Sub Testing()
    With ActiveSheet
        .Select
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
        .DisplayPageBreaks = False
        Firstrow = .UsedRange.Cells(1).Row
        Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
        For Lrow = Lastrow To Firstrow Step -1
        If InStr(.Cells(Lrow, "I").Value, "Removal") = 0 And _
            InStr(.Cells(Lrow, "J").Value, "removal") = 0 And _
            InStr(.Cells(Lrow, "J").Value, "removed") = 0 Then .Rows(Lrow).Delete
        Next Lrow
    End With
End Sub

0
投票

假设您已正确定义了WITH对象,看起来您只是错过了一个空格

Then.Rows(Lrow).Delete

它看起来应该是这样的

Then .Rows(Lrow).Delete
© www.soinside.com 2019 - 2024. All rights reserved.