2016-07-05 6 views
0

Excel 2010에서 .xlam 파일로 VBA 매크로를 작성하고 있습니다.VBA 추가 기능을 실행하는 동안 오류가 발생했습니다.

object variable or with block variable not set

특정 테이블에 열을 교환하는데, 나는 (안 추가 기능에서) 그것을처럼 매크로를 실행할 때입니다 :

내가 시도

내가이 오류를 실행합니다 완벽하게 작동합니다.

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 String 
Dim SearchDetails As String 

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 SearchString As String 
Dim StartTitlesRow As Long 

SearchString = "ùåøä" 

With ActiveSheet 
    Set cl = .Cells.Find(What:=SearchString, _ 
     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 
+1

어떤 줄이기시겠습니까? 애드 인으로 실행할 때'ActiveSheet' 란 무엇입니까? –

+0

그게 뭔지 보여 줄게! 'ActiveSheet'는 테이블이있는 시트입니다. – ALEXM

+1

그건 이상합니다. 보통 매크로가 깨질 때 커서 위치는 어떤 선에 대한 아이디어를 줄 것입니다. 반복 : 애드 인으로 실행할 때'ActiveSheet' 란 무엇입니까? –

답변

2

피가 ActiveSheet를 사용하여 : 이 내 매크로입니다! 참조하는 시트가 확실하지 않은 곳과 같은 문제 만 제공합니다. 똑같은 이유로 당신이있는 동안 ActiveWorkbook을 피하십시오.

대신 작업 할 시트에 대한 참조를 가져 :

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 

또는 당신은 이미 일부 지역에서처럼 당신은 인덱스를 사용할 수 있지만 당신이뿐만 아니라, 통합 문서를 지정해야 ActiveWorkbook 사용을 피하십시오.

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