我有一个抛出错误的片段,我假设因为变量s未初始化。我如何声明变量s?
Dim X As Integer
Dim WS As Worksheet
'Look for existing sheets named "For Export
'If found, delete existing sheet
For Each s In ActiveWorkbook.Sheets
If s.Name = "For Export" Then
Application.DisplayAlerts = False
' s.Delete
End If
Next s
答案 0 :(得分:0)
最简单的方法可能是在代码中搜索Option Explicit
并删除它。然后你不会被迫声明每个变量。但是,如果你不喜欢写丑陋的&脏代码尝试使用以下内容:
Dim s as Sheet
通常,最好使用Worksheets
集合,而不是Sheets
。因此:
Dim s as Worksheet
For each s in ActiveWorkbook.Worksheets
Sheets
集合包含Charts
和Worksheets
,因此最好更具体。