簡體   English   中英

For 循環每個工作簿

[英]For loop each workbook

我正在嘗試遍歷文件中的每個工作表,如果在工作簿中找到某個單詞,請刪除該單元格以及下面的其他 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

好的。 我讓它工作了。

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

結束子

嘗試不選擇/激活:

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

    For Each ws In ActiveWorkbook.Worksheets
        
        Set find = ws.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.Resize(12,1).EntireRow.Delete

    Next ws
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM