简体   繁体   English

VBA Excel ForEach工作表循环

[英]VBA Excel ForEach Worksheet Loop

First time poster, beginner level VBA scripter here. 第一次张贴,这里是初学者级VBA脚本编写者。 I have looked into the related questions here on StackOverflow and the answer to my question is just assumed, so I ask for some clarification. 我已经在StackOverflow上调查了相关问题,只是假设了我的问题的答案,所以我要求澄清一下。 The following Excel VBA script is intended to remove all empty rows from the tops of a 90-Sheet Workbook. 下面的Excel VBA脚本旨在删除90页工作簿顶部的所有空行。 When I run the script, it only affects the Worksheet I have open. 当我运行脚本时,它只会影响我打开的工作表。 Why won't it loop through all of the Sheets? 为什么不遍历所有图纸?

Sub Macro1()

    Dim j As Long
    Dim ws As Worksheet

    For Each ws In ActiveWorkbook.Worksheets
        For j = 10 To 1 Step -1
            If Cells(j, 3) = "" Then
                Rows(j).Delete
            End If
        Next j
    Next

End Sub

You need to tell it to look at the required worksheet in the If statement, and in the delete command: 您需要告诉它在If语句和delete命令中查看所需的工作表:

Sub Macro1()

Dim j As Long
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    For j = 10 To 1 Step -1
        If ws.Cells(j, 3) = "" Then
            ws.Rows(j).Delete
        End If
    Next j
Next

End Sub
Sub Macro1()

Dim j As Long
Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
ws.Activate
    For j = 10 To 1 Step -1
        If ws.Cells(j, 3) = "" Then
            ws.Rows(j).Delete
        End If
    Next j
Next

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM