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

Excel 自动排序特定列中的行并跳过空行,excel,vba,Excel,Vba,**这是为M列提供图纸名称的代码,图纸名称不包括在这些名称中(sht1-sht9): Public子工作表\u Activate() 常量sht1为String=“主测试” Const sht2 As String=“数据列表” 常量sht3为String=“报告” 常量sht4为String=“EmpList” 常量sht5为String=“emp_intface” 常量sht6为String=“sample” 常量sht7为String=“ssss” 常量sht8为String=“log”

**这是为M列提供图纸名称的代码,图纸名称不包括在这些名称中(sht1-sht9):

Public子工作表\u Activate()
常量sht1为String=“主测试”
Const sht2 As String=“数据列表”
常量sht3为String=“报告”
常量sht4为String=“EmpList”
常量sht5为String=“emp_intface”
常量sht6为String=“sample”
常量sht7为String=“ssss”
常量sht8为String=“log”
常量sht9为String=“Report2”
范围(“m:m”)。ClearContents
将S变为整数
对于S=1到工作表。计数
带工作表(“报告2”)
设置ws=工作表
如果ws.Visible=True,则_
(ws.Name sht1)和(ws.Name sht2)以及(ws.Name sht3)和(ws.Name sht5)以及_
(ws.Name sht4)和(ws.Name sht6)以及(ws.Name sht7)和(ws.Name sht8)_
和(ws.Name sht9)然后
.Cells(S,13).Value=ws.Name
如果结束
以
下一个
端接头

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


因此,这些被排除的工作表名称首先开始,然后其他工作表以排除工作表之后的最后一个顺序出现,因此,当此代码运行时,在M列中生成一些空白行;由于已排除了图纸的顺序,那么将图纸名称从第10行或第11行移动到第M列的第一个空行的解决方案是什么?

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

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
S为整数,R为长
R=1
对于S=1到工作表。计数
带工作表(“报告2”)
设置ws=工作表
如果ws.Visible=True和(ws.Name sht1)和(ws.Name sht2)_
和(ws.Name sht3)和(ws.Name sht5)以及(ws.Name sht4)_
和(ws.Name sht6)和(ws.Name sht7)以及(ws.Name sht8)_
和(ws.Name sht9)然后
.Cells(R,13).Value=ws.Name
R=R+1
如果结束
以
下一个
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