对于每个ws在ActiveWorkbook中

时间:2013-10-19 01:07:13

标签: debugging excel-vba foreach runtime-error vba

我对VBA很新,并尝试调试以下代码。我一直在 - > “运行时错误'13':类型不匹配”。当我调试时,“For Each Ws In ActiveWorkbook.Worksheets”行以黄色突出显示。我只是想创建一个消息框,它给我一个工作簿中所有工作表的名称列表。非常感谢任何帮助!

Sub ListStates()
Dim ws As Worksheets
Dim message As String
message = "Here is a list of states:"
For Each ws In ActiveWorkbook.Worksheets
    message = message & vbCrLf & "ws.Name"
Next
MsgBox message, vbInformation, "State list"

End Sub

2 个答案:

答案 0 :(得分:1)

更改行

Dim ws As Worksheets

Dim ws As Worksheet

Worksheets对象是Worksheet对象的集合。您正试图循环Worksheet

中的每个Worksheets

答案 1 :(得分:0)

另外,删除括号ws.Name - 行应该是:

的引号
message = message & vbCrLf & ws.Name