生成表单名称的列表,但在列表中排除特定的工作表

我现在有一个工作macros,它会自动生成我的文件中所有工作表的列表:

Sub RefreshStocks() Dim x As Integer Dim count As Integer For x = 4 To Worksheets.count Select Case Worksheets(x).Name Case "Calculator", "Index Composition", "Market Value", "Watch List", "REF DATA" 'Do Nothing Case Else count = count + 1 Cells(count, 2).Value = Worksheets(x).Name End Select Next x End Sub 

macros工作正常,但我想知道我应该做什么,如果我有多个工作表,我想从列表中排除。 例如,如果要排除名为“参考数据”和“stream程指南”的工作表。

我想要做的就是添加一个IF语句,其中子忽略名为“参考数据”或“stream程指南”的工作表从生成的列表中。

 Sub WSNames() Dim x As Integer For x = 4 To Worksheets.Count If Worksheet.Name = "Reference data" Or "Process Guide" Then 'IGNORE this Worksheet and should not be included Else Cells(x, 2).Value = Worksheets(x).Name End If Next x End Sub 

有人可以帮助纠正上面的代码。

在这里输入图像说明

select案例是这种情况的理想select。

 Sub WSNames() Dim x As Integer Dim count as Integer count = 4 For x = 1 To Worksheets.Count Select Case Worksheets(x).Name Case "Reference data", "Process Guide" 'Do Nothing Case Else Cells(count, 2).Value = Worksheets(x).Name count = count + 1 End Select Next x End Sub 

感谢YowE3K!

 Sub WSNames() Dim x As Integer Dim r As Integer r = 3 For x = 4 To Worksheets.Count IF Worksheets(x).name = "Reference data" or _ Worksheets(x).name = "Process Guide" then 'IGNORE this Worksheet and should not be included Else r = r + 1 Cells(r, 2).Value = Worksheets(x).Name End If Next x End Sub 

我join了一个新的variablesr,这样在你的列表中就不会有被排除在外的工作表名字。

只需要使用if worksheet(x).name<>"ref data" and worksheet(x).name<>"Proc guide" then

 Sub WSNames_another() Dim sh As Worksheet For Each sh In ActiveWorkbook.Sheets If sh.Name = "Reference data" Or sh.Name = "Process Guide" Then 'IGNORE this Worksheet and should not be included Else 'Do what every you want End If Next End Sub 

要么

 Sub WSNames() Dim x As Integer For x = 4 To Worksheets.Count If Worksheets(x).Name = "Reference data" Or Worksheets(x).Name = "Process Guide" Then 'IGNORE this Worksheet and should not be included Else Cells(x, 2).Value = Worksheets(x).Name End If Next x End Sub