首页 > 解决方案 > For循环每个工作簿

问题描述

我正在尝试遍历文件中的每个工作表,如果在工作簿中找到某个单词,请删除该单元格以及下面的其他 11 个单元格。

我放弃。我的代码不起作用。想不通为什么。

有人能帮助我吗?

Sub forEachWs()
Dim ws As Worksheet
Dim find As Range

For Each ws In ActiveWorkbook.Worksheets
    Sheets(ws).Select
    Set find = Cells.find(What:="nieusprawiedliwiona", After:=ActiveCell, LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False)
    If Not find Is Nothing Then find.Activate

    Range(Selection, Selection.Offset(11, 0)).Select
    Selection.EntireRow.Delete

Next ws
End Sub

标签: excelvba

解决方案


好的。我让它工作了。

Sub forEachWs()
Dim ws As Worksheet
Dim find As Range

For Each ws In Worksheets
    MsgBox (ws.Name)
    ws.Select
    Set find = Cells.find(What:="nieusprawiedliwiona", After:=ActiveCell, LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False)
    If Not find Is Nothing Then find.Activate

    Range(Selection, Selection.Offset(11, 0)).Select
    Selection.EntireRow.Delete

Next ws

结束子


推荐阅读