Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.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
Vba 为不同的工作表运行代码_Vba_Excel - Fatal编程技术网

Vba 为不同的工作表运行代码

Vba 为不同的工作表运行代码,vba,excel,Vba,Excel,有人能告诉我这个代码有什么问题吗?我正在尝试删除任何空白行或第8列中有单词Pending的行 如果修改工作表处于活动状态,则此代码有效,但如果我尝试从另一个工作表运行它,则此代码无效。理想情况下,我希望它在不实际激活或选择工作表的情况下运行。可能吗 谢谢 Sub pending_blank_delete() With Sheets("Modifications") last_mod_row = .Range("A" & .Rows.Count).End(xlUp).Row End

有人能告诉我这个代码有什么问题吗?我正在尝试删除任何空白行或第8列中有单词Pending的行

如果修改工作表处于活动状态,则此代码有效,但如果我尝试从另一个工作表运行它,则此代码无效。理想情况下,我希望它在不实际激活或选择工作表的情况下运行。可能吗

谢谢

Sub pending_blank_delete()

With Sheets("Modifications")
  last_mod_row = .Range("A" & .Rows.Count).End(xlUp).Row
End With

Dim c As Range
Dim SrchRng

    Set SrchRng = Range(Cells(2, 8), Cells(last_mod_row, 8))
    Do
        Set c = SrchRng.find("Pending", LookIn:=xlValues)
        If Not c Is Nothing Then c.EntireRow.Delete
    Loop While Not c Is Nothing

On Error Resume Next
Range(Cells(2, 8), Cells(last_mod_row, 8)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End Sub

这应该对你有用

您的范围没有指定哪张表,因此我将With语句中的所有内容都移动了。当您看到Set SrchRng=Range.Cells2,8、.cellslat\u mod\u row,8时,.Cells2,8表示SheetsModifications.Cells2,8。如果不将其放置在With语句中,它将从活动工作表创建范围。不使用With语句的另一个选项是设置srchrn=RangeSheetsModifications.Cells2,8,SheetsModifications.cellslat\u mod\u行,8


限定对要开始的范围和单元格的引用。您可以始终只传递一个字符串参数进行修改或实际的工作表引用。谢谢@tjb1,这太完美了。你能告诉我我做错了什么吗?更新了答案并提供了更多信息。
Sub pending_blank_delete()

    Dim c As Range
    Dim SrchRng

With Sheets("Modifications")
    last_mod_row = .Range("A" & .Rows.Count).End(xlUp).Row

    Set SrchRng = Range(.Cells(2, 8), .Cells(last_mod_row, 8))

    Do
        Set c = SrchRng.Find("Pending", LookIn:=xlValues)
        If Not c Is Nothing Then c.EntireRow.Delete
    Loop While Not c Is Nothing

    On Error Resume Next

    Range(.Cells(2, 8), .Cells(last_mod_row, 8)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete

    On Error GoTo 0
End With

End Sub