我现在有一个工作宏,它会自动生成我文件中所有工作表的列表:
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
宏工作正常,但如果我有多个要从列表中排除的工作表,我想知道应该怎么做。例如,如果要排除名为"参考数据"的工作表。和"流程指南"。
我尝试做的是添加一个IF语句,该子语句忽略名为&#34的工作表;参考数据"或"流程指南"从生成的列表中。
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
有人可以帮忙纠正上面的代码。
答案 0 :(得分:3)
选择案例非常适合这种情况。
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!
答案 1 :(得分:2)
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
我添加了一个新变量r,这样您的列表中就不会有被排除的工作表名称所在的空白。
答案 2 :(得分:0)
只需使用if worksheet(x).name<>"ref data" and worksheet(x).name<>"Proc guide" then
答案 3 :(得分:0)
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
OR
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