【问题标题】:Macro sheet rename based on cell基于单元格的宏表重命名
【发布时间】:2021-12-30 11:22:03
【问题描述】:

我应该每天运行 3 次报告,该报告将选项卡从“基本”文件复制到简单的 excel 工作簿中。选项卡的名称应使用实际日期重新命名。例如。 2021 年 11 月 19 日,第二份报告应重命名为 19/11/2021_v2,第三份报告应重命名为 19/11/2021_v3。 过去的所有选项卡都需要在文件中,因此宏应该检查哪些选项卡存在,并根据历史记录重命名实际工作表。 这是关于文件的图片:

正如您现在所见,概览选项卡应重命名为 19-11_v3。

这是我的代码,它只能重命名第一个和第二个日期,但 v3 不起作用。

我的错在哪里?

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

【问题讨论】:

  • 您的宏只复制了一份“概览”表。

标签: excel vba rename


【解决方案1】:

同时,我可以解决问题。 这是工作代码:

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

【讨论】:

    猜你喜欢
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多