Erreur lors de l'utilisation du complément VBA

J'écris une macro VBA dans Excel 2010 dans un file .xlam.

Lorsque j'essaie de l'exécuter, je reçois cette erreur:

variable d'object ou avec variable de bloc non définie

Il est supposé échanger des colonnes dans une table spécifique, et lorsque je l'exécute comme une macro (pas dans l'add-in), elle fonctionne parfaitement. C'est ma macro:

Sub SwapTable(ByVal control As IRibbonControl) Dim LastCol As Long Dim LastRow As Long Dim Swaps As Long Dim i As Integer Dim DocumentTitle As Ssortingng Dim SearchDetails As Ssortingng LastRow = LastRowInOneColumn() LastCol = LastColumnInOneRow(LastRow) StartTitlesRow = Find_TitlesRow() 'copy title rows With ActiveSheet DocumentTitle = .Cells(StartTitlesRow - 3, 1).Value SearchDetails = .Cells(StartTitlesRow - 2, 1).Value End With 'check how many swaps needed If LastCol Mod 2 = 0 Then Swaps = LastCol / 2 Else Swaps = (LastCol - 1) / 2 End If 'run swap For i = 1 To Swaps Call Swap(i, LastCol - i + 1, LastRow, StartTitlesRow - 1) Next i 'past title rows With ActiveSheet .Cells(StartTitlesRow - 3, 1) = DocumentTitle .Cells(StartTitlesRow - 2, 1) = SearchDetails End With Worksheets(1).Columns("A:EE").AutoFit End Sub Function LastColumnInOneRow(LastRow As Long) As Long 'Find the last used row in a Column: column A in this example Dim LastCol As Long With ActiveSheet LastCol = .Cells(LastRow, .Columns.Count).End(xlToLeft).Column End With LastColumnInOneRow = LastCol End Function Function LastRowInOneColumn() As Long 'Find the last used row in a Column: column A in this example Dim LastRow As Long With ActiveSheet LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With LastRowInOneColumn = LastRow End Function Function Find_TitlesRow() As Long Dim SearchSsortingng As Ssortingng Dim StartTitlesRow As Long SearchSsortingng = "ùåøä" With ActiveSheet Set cl = .Cells.Find(What:=SearchSsortingng, _ After:=.Cells(1, 1), _ LookIn:=xlValues, _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False, _ SearchFormat:=False) If Not cl Is Nothing Then StartTitlesRow = cl.Row Else MsgBox "Could'nt find start row" End If End With Find_TitlesRow = StartTitlesRow End Function Function Swap(Col1 As Integer, Col2 As Integer, LastRow As Long, StartTableRow As Variant) Dim FirstCol As Variant Dim SecondCol As Variant Dim temp As Variant temp = Sheets(1).Range(Cells(StartTableRow, Col1), Cells(LastRow, Col1)).Value Sheets(1).Range(Cells(StartTableRow, Col1), Cells(LastRow, Col1)).Value = Sheets(1).Range(Cells(StartTableRow, Col2), Cells(LastRow, Col2)).Value Sheets(1).Range(Cells(StartTableRow, Col2), Cells(LastRow, Col2)).Value = temp End Function 

Évitez d'utiliser ActiveSheet ! Il ne vous donnera que des problèmes comme celui que vous rencontrez, où vous ne savez pas quelle feuille fait reference. Évitez ActiveWorkbook pendant que vous l'êtes, pour la même raison.

Au lieu de cela, obtenez une reference à la feuille avec laquelle vous souhaitez travailler:

 Dim oWb As Workbook Dim oSheet As Worksheet Set oWb = Workbooks("[WORKBOOKNAME]") '***** or use index like Workbooks(1) If Not oWb Is Nothing Then Set oSheet = oWb.Sheets("[WORKSHEETNAME]") '***** or use index like Sheets(1) End If If Not oSheet Is Nothing Then '***** Do your stuff 'past title rows With oSheet .Cells(StartTitlesRow - 3, 1) = DocumentTitle .Cells(StartTitlesRow - 2, 1) = SearchDetails End With '***** etc End If 

Ou vous pouvez utiliser un index comme vous le faites déjà dans certains endroits, mais vous devez également spécifier un classur pour éviter d'utiliser ActiveWorkbook:

 oWb.Worksheets(1).Columns("A:EE").AutoFit