Error adding a new sheet to a XLSM Macro Excel Fil

2019-03-07 03:19发布

问题:

I've found a problem when I add a new sheet in a Macro Excel File using VBA. It doesn't matter the method that I use:

  • Sheets.add
  • Copying and pasting a sheet to create a new one

The new sheet is created (or copied) but the Macro stops immediately, without any message error, and the rest of the code is not executed.

I've tried to convert the file to XLSX and now, when I run the same code, the sheet is created (like before) and the code execution continues perfectly.

That's the reason why I think that it is related with the Macro Excel type of file.

Has anybody found a similar error?

Thanks in advance for any help.

Alberto


UPDATE TO MY PREVIOUS QUESTION

I've made a new verification, testing exactly the same code in other PC and Excel installation and... it runs perfectly.

I don't know why, but now the new sheet is created and the code goes on. The only difference between the two equipments is the Excel 2016 version: Standard in one and Profesional in the other. But, obviously, this cannot be the explanation.

In any case, I must investigate what's going on in one of this installation that is stopping the code execution.

回答1:

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.