0
我正在Excel 2010中的.xlam文件中編寫VBA宏。運行VBA加載項時出錯
當我嘗試運行它,我得到這個錯誤:
object variable or with block variable not set
它應該換入特定的表列,當我運行它只是一個宏(不加載項),它完美的作品。 這是我的宏:使用ActiveSheet
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
什麼行會給出錯誤?作爲加載項運行時,什麼是「ActiveSheet」? –
它不顯示至極線!而'ActiveSheet'則是表格中的表格。 – ALEXM
這很奇怪。通常,當宏斷開時的光標位置會給你一個關於哪一行的想法。重複:當您將它作爲加載項運行時,什麼是「ActiveSheet」? –