在 VBA 上使用当前日期重命名工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44244677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:39:08  来源:igfitidea点击:

Rename a Sheet with the current date on VBA

vbaexcel-vbadateexcel

提问by Mauro

I have a code to copy all cells of a sheet and paste it in a new sheets in values with the original format. I want also to rename automatically the new sheet with the current date, I've tried this:

我有一个代码来复制工作表的所有单元格并将其粘贴到具有原始格式的值的新工作表中。我还想用当前日期自动重命名新工作表,我试过这个:

Cells.Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
    SkipBlanks:=False, Transpose:=False
ActiveSheet.Name = szToday

But the last line doesn't work to reach what I want.

但是最后一行无法达到我想要的效果。

Could anyone help me?

有人可以帮助我吗?

Thanks.

谢谢。

采纳答案by Siddharth Rout

A sheet name cannot be blank. Since you have not defined szToday, it is blank.

工作表名称不能为空。由于您还没有定义szToday,所以它是空白的。

Is this what you are trying

这是你正在尝试的吗

Sheets.Add After:=ActiveSheet
szToday = Format(Date, "DD-MM-YY")
ActiveSheet.Name = szToday

Or simply

或者干脆

Sheets.Add After:=ActiveSheet
ActiveSheet.Name = Format(Date, "DD-MM-YY")

or a one liner

或单班轮

Sheets.Add(After:=ActiveSheet).Name = Format(Date, "DD-MM-YY")