0

**这是为列 M 提供这些名称(sht1-sht9)排除的 sheetNames 的代码::

Public Sub Worksheet_Activate() Const sht1 As String = "Main-test" Const sht2 As String = "Data-list" Const sht3 As String = "Report" Const sht4 As String = "EmpList" Const sht5 As String = "emp_intface" Const sht6 As String = "sample" Const sht7 As String = "SSSSSS" Const sht8 As String = "log" Const sht9 As String = "Report2" Range("m:m").ClearContents Dim S As Integer For S = 1 To Worksheets.Count With Worksheets("Report2") Set ws = Worksheets(S) If ws.Visible = True And _ (ws.Name <> sht1) And (ws.Name <> sht2) And (ws.Name <> sht3) And (ws.Name <> sht5) And _ (ws.Name <> sht4) And (ws.Name <> sht6) And (ws.Name <> sht7) And (ws.Name <> sht8) _ And (ws.Name <> sht9) Then .Cells(S, 13).Value = ws.Name End If End With Next End Sub

* *更多详细信息来解释我的问题:

所以这些排除的SheetNames首先开始,然后其余的工作表在排除的工作表之后的最后一个顺序出现,所以当这段代码运行时,它会在M列中创建一些空白行;因为工作表的顺序已被排除,那么解决方案是什么将 SHeetNames 从第 10 行或第 11 行移动到 M 列中的第一个空白行???`

4

1 回答 1

0

不要使用S变量来增加行,否则如果IF条件不满足,您将得到空白。尝试这个

Dim S As Integer, R As Long

R = 1

For S = 1 To Worksheets.Count
    With Worksheets("Report2")
        Set ws = Worksheets(S)
        If ws.Visible = True And (ws.Name <> sht1) And (ws.Name <> sht2) _
        And (ws.Name <> sht3) And (ws.Name <> sht5) And (ws.Name <> sht4) _
        And (ws.Name <> sht6) And (ws.Name <> sht7) And (ws.Name <> sht8) _
        And (ws.Name <> sht9) Then
            .Cells(R, 13).Value = ws.Name
            R = R + 1
        End If
    End With
Next
于 2012-09-04T07:48:58.017 回答