自动对特定列中的行进行排序并跳过空行

时间:2012-09-04 03:12:32

标签: excel-vba vba excel

**这是提供列M的代码,其中sheetNames不包含这些名称(sht1-sht9)::

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首先开始,然后其余的工作表在排除的Sheets之后的最后一个订单,所以当这个代码运行时,在列M中制作一些空白行; becoz已排除了工作表的顺序,那么将SHeetNames从第10行或第11行移动到M列的第一个空白行的解决方案是什么?

1 个答案:

答案 0 :(得分: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