Change Sheet code name

前端 未结 1 504
傲寒
傲寒 2021-01-05 06:12

For some reason the below code only works when run in the vbe, when run in the event handling code, or by macro list the codename remains sheet1.

Please can someone

相关标签:
1条回答
  • 2021-01-05 06:53
    Sub change_code_name()
      Dim wbk As Object, sheet As Object
      ActiveWorkbook.VBProject.Name = "VBAProject"
      Set wbk = ActiveWorkbook.VBProject.VBComponents(ActiveWorkbook.CodeName)
      wbk.Name = "wbk_code_name"
      Set sheet = ActiveWorkbook.VBProject.VBComponents(ActiveWorkbook.Sheets(1).CodeName)
      sheet.Name = "sheet_code_name"
    End Sub
    

    Also the access to the VBAProject is required: see "Macro settings" and set "Trust access to the VBA object model". To programmatically change the settings, look here.

    0 讨论(0)
提交回复
热议问题