The following code is triggered when the user press a button from my add-in tab:
Sub OnActionListaMarkowitz(control As IRibbonControl)
GeneratesWorkbook
End Sub
In a different modules I have a sub to create a new workbook and copy the sheets in my Xlam file to the new workbook.
Sub GeneratesWorkbook()
On Error GoTo LabelErro
Set PlanilhaMarkowitz = Workbooks.Add
Dim currentSheet As Worksheet
Dim sheetIndex As Integer
sheetIndex = 1
'Changes xlam property to false so I can copy Xlam sheets to new wb
ThisWorkbook.IsAddin = False
ThisWorkbook.Activate
'Copy Xlam's sheets to new workbook
Sheets("Hidden").Visible = True 'this workbook was very hidden so I make it visible to copy
Sheets("Hidden").Copy Before:=PlanilhaMarkowitz.Sheets(sheetIndex)
Sheets("Hidden").Visible = xlVeryHidden
Sheets("Calculos").Copy Before:=PlanilhaMarkowitz.Sheets(sheetIndex)
Sheets("Pesos").Copy Before:=PlanilhaMarkowitz.Sheets(sheetIndex)
Sheets("Atributos").Visible = True 'this workbook was very hidden so I make it visible to copy
Sheets("Atributos").Copy Before:=PlanilhaMarkowitz.Sheets(sheetIndex)
Sheets("Atributos").Visible = xlVeryHidden
Sheets("Correl").Copy Before:=PlanilhaMarkowitz.Sheets(sheetIndex)
Sheets("Fronteira").Copy Before:=PlanilhaMarkowitz.Sheets(sheetIndex)
ThisWorkbook.IsAddin = True
Exit Sub
LabelErro:
ThisWorkbook.IsAddin = True
End Sub
The odd part of it is that if I execute this code by pressing F8 (step by step) It works perfectly. But If I run this code normally or using F5 it only copy the worksheet("Hidden").
I've set a breakpoint at
Sheets("Hidden").Visible = True
Pressing F8 till the end of the two subs makes it work. Pressing F5 doesn't.