2012-09-04 42 views
0

**這是用於提供M列與排除與這些名稱(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代碼

* *更多詳細解釋我的問題:

所以這些排除SheetNames begain起初然後玉樹其餘ts出現在排除表格之後的最後一個順序,所以當這段代碼在列M中運行時,它會生成一些空行;因爲排除表的順序,因此將SHeetNames從第10行或第11行移動到列M中的第一個空白行???

回答

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