[英]Macro sheet rename based on cell
I should run 3 times a day a report which is copying the tab from "base" file into the simple excel workbook.我应该每天运行 3 次报告,该报告将选项卡从“基本”文件复制到简单的 excel 工作簿中。 The names of the tabs should be re-named with actual date.
选项卡的名称应使用实际日期重新命名。 Eg 19/11/2021, second report should be rename 19/11/2021_v2 and the third one 19/11/2021_v3.
例如,2021 年 11 月 19 日,第二份报告应重命名为 19/11/2021_v2,第三份报告应重命名为 19/11/2021_v3。 All of the tabs from the past need to be in the file so the macro should check which tabs is existing and based on the history should be rename the actual sheet.
过去的所有选项卡都需要在文件中,因此宏应检查哪些选项卡存在,并根据历史记录重命名实际工作表。 Here it is a picture about the file:
这是关于文件的图片:
As you can see now the overview tab should be renamed as 19-11_v3.如您现在所见,概览选项卡应重命名为 19-11_v3。
Here it is my code which can rename only the first and second dates but the v3 is not working.这是我的代码,它只能重命名第一个和第二个日期,但 v3 不起作用。
Where did my mistake?我的错在哪里?
Sub RenameSheet()
Dim Sht As Worksheet
Dim NewSht As Worksheet
Dim VBA_BlankBidSheet As Worksheet
Dim newShtName As String
Set Target = Range("d11")
Set VBA_BlankBidSheet = Sheets("Overview")
Application.DisplayAlerts = False
VBA_BlankBidSheet.Copy After:=ActiveSheet
Set NewSht = ActiveSheet
newShtName = Target
For Each Sht In ThisWorkbook.Sheets
If Sht.Name = Target Then
newShtName = Target & "_v2"
ElseIf Sht.Name = Target & "_v2" Then
newShtName = Target & "_v3"
End If
Next Sht
NewSht.Name = newShtName
Application.DisplayAlerts = False
Sheets("Overview").delete
End Sub
Try the following:尝试以下操作:
Sub RenameSheet()
Dim Sht As Worksheet
Dim NewSht As Worksheet
Dim VBA_BlankBidSheet As Worksheet
Dim newShtName As String
Set Target = Range("d11")
Set VBA_BlankBidSheet = Sheets("Overview")
Application.DisplayAlerts = False
Set VBA_BlankBidSheet = ActiveSheet
VBA_BlankBidSheet.Copy After:=VBA_BlankBidSheet
Set NewSht = ActiveSheet
NewSht.Name = Target & "v3"
VBA_BlankBidSheet.Copy After:=VBA_BlankBidSheet
Set NewSht = ActiveSheet
NewSht.Name = Target & "v2"
Application.DisplayAlerts = True
Sheets("Overview").delete
End Sub
Meanwhile, I could solve the problem.同时,我可以解决问题。 Here it is the working code:
这是工作代码:
Sub RenameSheet()
Dim Sht As Worksheet
Dim NewSht As Worksheet
Dim VBA_BlankBidSheet As Worksheet
Dim newShtName As String
Set target = Range("d11")
' modify to your sheet's name
Set VBA_BlankBidSheet = Sheets("overview")
Set NewSht = ActiveSheet
' you can change it to your needs, or add an InputBox to select the Sheet's name
newShtName = target
For Each Sht In ThisWorkbook.Sheets
If Sht.Name = target Then
newShtName = target & "_v2"
End If
If Sht.Name = target & "_v2" Then
newShtName = target & "_v3"
End If
Next Sht
NewSht.Name = newShtName
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.