我们是否有使用VBA搜索Excel工作表的“Exists”函数?
我们是否有使用VBA搜索Excel工作表的“Exists”函数?
我的Excel工作簿的工作表以日期("DD-MMM-YYYY")命名,我需要将每个工作表的数据复制到另一个Excel中。当我使用for循环(从一个日期到另一个日期)时,没有可用工作表的日期会阻碍我的脚本。在VBA中是否有名为"not exists"的东西?
代码:
子获取数据()
Dim fromDate As Date
Dim toDate As Date
fromDate = InputBox("请输入开始日期")
toDate = InputBox("请输入结束日期")
'确保开始日期小于结束日期
If (fromDate < toDate) Then
'选择从开始日期到结束日期的所有数据
For i = fromDate To toDate
dt = Format(i, "d-mmm-yyyy")
'复制并粘贴每天的数据到报表工作表
If Worksheet(dt <> "") Then
Sheets(dt).Select
Range("E9").Select
Selection.Copy
Sheets("报表").Select
Range("E17").Select
ActiveSheet.Paste
Else
MsgBox ("名为" & dt & "的工作表不存在")
End If
Next
Else
MsgBox ("开始日期大于结束日期")
End If
End Sub
我期望的结果是,当工作簿中没有对应日期的工作表时,代码显示消息框"名为""的工作表不存在"。