尝试创建基于时间戳命名的工作表 [英] Trying to create a sheet that is named based on a timestamp
问题描述
Private Sub ArchiveBox_Click()
Dim CurrentTime As Date
Set CurrentTime = Now()
Sheets.Add.Name = CurrentTime
End Sub
这使我在下面的行中不断给出类型不匹配错误。 我不知道为什么,我已经尝试了一些其他语法来替换Now()并且尝试在没有CurrentTime的情况下完成它,两者都无济于事。 我该怎么做才能解决这个问题,我是否以最优的方式做这个
? 除了常识+谷歌之外,我对VBA一无所知。 提前致谢。
This keeps giving me a type mismatch error on the below line. I'm not sure why, I've tried a couple other syntaxes to replace Now() and also tried doing it without CurrentTime at all, both to no avail. What can I do to fix this, and am I even doing this in the most optimal way? I literally know nothing about VBA except common sense + Google. Thanks in advance.
Set CurrentTime = Now()
推荐答案
1)CurrentTime不是对象,因此不应使用关键字Set。
1) CurrentTime is not an object, so you shouldn't use the keyword Set.
2)工作表名称不能包含字符例如 /和:所以你必须格式化日期/时间。
2) Sheet names cannot contain characters such as / and : so you have to format the date/time.
尝试
Private Sub ArchiveBox_Click()
Sheets.Add.Name = Format(Now, "yyyymmddhhmmss")
End Sub
或
Private Sub ArchiveBox_Click()
Sheets.Add.Name = Format(Now, "yyyymmdd_hhmmss")
End Sub
根据需要更改,但避免使用:和/格式。
Change as desired, but avoid using : and / in the format.
这篇关于尝试创建基于时间戳命名的工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!