Error adding a new sheet to a XLSM Macro Enabled Excel File

前端 未结 2 1490
傲寒
傲寒 2021-01-29 11:29

I want to add a new sheet in a Macro Enabled Excel File using VBA.

It doesn\'t matter the method that I use:

  • Sheets.add
  • Copying and pasting a she
相关标签:
2条回答
  • 2021-01-29 12:28

    Try this code in your xlsm file:

    Dim ws As Worksheet
    Set ws = Sheets.Add(After:=Sheets(Sheets.Count))
    

    And let me know if this doesn't work.

    0 讨论(0)
  • 2021-01-29 12:33

    I was experiencing the same issue, on a Windows 7 computer with Excel version 16.0.10730.20264 32-bit, the code ran fine without issue. However, on a Windows 10 computer with the same Excel install version, the macro would immediately stop execution following the Sheets.Add or Worksheets.Add line. I found that this was only happening where I was attempting to add a sheet to a workbook that contained VBA code. The issue is caused by the macro security settings on the computer. If you set Automation Security to Low before opening the workbook, you should no longer get the error: Application.AutomationSecurity = msoAutomationSecurityLow

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