2011-09-07 46 views
1

Excel 2010中的VBA加載項存在問題。VBA加載項問題

我創建了一些解析我的Excel數據的代碼。我把它做成了一個加載項。

但是,當我加載加載項並運行時,會發生錯誤。

錯誤消息說:runtime error 91 object variable or With block variable not set

錯誤指向rowSize = ActiveSheet.Rows.Count

有誰知道如何解決這個錯誤?

下面是代碼,

Private Sub Workbook_Open() 

Dim counter As Long 
Dim rowSize As Long 
Dim userId As String 
Dim answers As String 
Dim vals As String 

Dim i As Integer 

rowSize = ActiveSheet.Rows.Count 
counter = 1 



'Create Column 

ActiveSheet.Cells(1, 7).Value = "Country" 
ActiveSheet.Cells(1, 8).Value = "State" 
ActiveSheet.Cells(1, 9).Value = "Age" 

ActiveSheet.Cells(1, 7).Font.Bold = True 
ActiveSheet.Cells(1, 8).Font.Bold = True 
ActiveSheet.Cells(1, 9).Font.Bold = True 

ActiveSheet.Cells(1, 7).HorizontalAlignment = xlCenter 
ActiveSheet.Cells(1, 8).HorizontalAlignment = xlCenter 
ActiveSheet.Cells(1, 9).HorizontalAlignment = xlCenter 

ActiveSheet.Cells(1, 7).Borders().LineStyle = xlContinuous 
ActiveSheet.Cells(1, 8).Borders().LineStyle = xlContinuous 
ActiveSheet.Cells(1, 9).Borders().LineStyle = xlContinuous 

'Set Value 
Do While counter < rowSize 

If ActiveSheet.Cells(counter, 1).Value = Null Then Exit Do 
If ActiveSheet.Cells(counter, 4).Value = "3" Then 

    userId = ActiveSheet.Cells(counter, 2).Value 
    vals = ActiveSheet.Cells(counter, 6).Value 
    'MsgBox (vals) 

    temp = Split(vals, ",") 
    i = 0 

    Do While i < 10 
     targetCell = counter + i 
     If ActiveSheet.Cells(targetCell, 2).Value = userId Then 
      ActiveSheet.Cells(targetCell, 7).Value = temp(0) 
      ActiveSheet.Cells(targetCell, 8).Value = temp(1) 
      ActiveSheet.Cells(targetCell, 9).Value = temp(2) 

      ActiveSheet.Cells(targetCell, 7).HorizontalAlignment = xlCenter 
      ActiveSheet.Cells(targetCell, 8).HorizontalAlignment = xlCenter 
      ActiveSheet.Cells(targetCell, 9).HorizontalAlignment = xlCenter 

      ActiveSheet.Cells(targetCell, 7).Borders().LineStyle = xlContinuous 
      ActiveSheet.Cells(targetCell, 8).Borders().LineStyle = xlContinuous 
      ActiveSheet.Cells(targetCell, 9).Borders().LineStyle = xlContinuous 
     End If 
     i = i + 1 
    Loop 
    temp = Null 
    'parsing_question_1(vals, userId) 
End If 

counter = counter + 1 
Loop 
End Sub 

謝謝!

+0

如何接受的答案? –

+0

對不起,現在我知道如何接受答案:( –

+0

爲什麼在workbook_open事件處理程序中有這樣的內容?只有當您的外接程序加載時纔會運行。是否想要在Excel中打開的任何工作簿上運行它?如果是這樣,查找「應用程序事件」 –

回答

2

一個插件只是代碼 - 沒有用戶界面。由於沒有用戶界面,技術上在ActiveSheet的插件文件中沒有工作表。實際上,插件中有表單,但它們都不能「活動」。

如果您想在加載項中使用工作表,您需要以不同的方式引用這些表。舉例來說,如果你想在你的外接第一板的工作,你可以像使用

Me.Sheets(1).Rows.Count 

Me關鍵字是指您所在的類代碼。在這種情況下,你在加載項的ThisWorkbook模塊,所以Me引用作爲加載項的Workbook對象。

如果您想要處理不在加載項中的特定工作表,則可以在打開的事件中打開該工作簿並引用該工作表。如

Dim sh As Worksheet 

Set sh = Workbooks.Open("C:\MyPath\MyBook.xls").Sheets(1) 

rowSize = sh.Rows.Count 

最後,如果要在任何打開工作簿運行的代碼,你必須創建一個自定義的類模塊,用於偵聽應用程序級別的事件。首先創建一個自定義類模塊,調用CAppEvents。在自定義類模塊,把這段代碼

Private WithEvents mApp As Application 

Public Property Set App(oApp As Application) 
    Set mApp = oApp 
End Property 

Public Property Get App() As Application 
    Set App = mApp 
End Property 

Private Sub mApp_WorkbookOpen(ByVal wb As Workbook) 
    FormatWorkbook wb 
End Sub 

'or to limit which workbook it runs on - in this example based on the path 
'but you may use some other condition like the existence of a particular 
'custom document property 
Private Sub mApp_WorkbookOpen(ByVal wb As Workbook) 
    If wb.Path = "\\Server1\mypath" Then 
     FormatWorkbook wb 
    End If 
End Sub 

在一個標準的模塊,把這段代碼

Public clsAppEvents As CAppEvents 

Sub Auto_Open() 

    Set clsAppEvents = New CAppEvents 
    Set clsAppEvents.App = Application 

End Sub 

Sub FormatWorkbook(wb As Workbook) 

    Dim sh As Worksheet 

    Set sh = wb.Sheets(1) 

    'do stuff here 

End Sub 
+0

開頭段落不正確:'ActiveSheet'引用當前活動工作表(帶有專注於圖形用戶界面),而不考慮它的工作內容。問題中的代碼錯誤的原因是當AddIn Workbook_Open事件運行時,沒有可打開的可視工作簿,因此也沒有ActiveSheet。如果插件中的其他代碼稍後運行,在另一個工作簿打開並可見後,「ActiveSheet」將返回對該工作簿活動工作表的引用。 (否則的答案很好,所以沒有投票) –

+0

我想運行我的代碼時加載項加載,我該如何設置事件?所以,我需要先打開一個excel文件並運行加載項,然後運行我的代碼。 –

+0

所以我想只在特定的excel文件上運行我的代碼,而不是所有的excel文件。 –

2

由於插件的Woorkbook_Open事件運行的第一個顯示的工作表打開之前,有在那​​個時間點沒有活動板,因此ActiveSheet未設置

正如蒂姆說,你可能不希望這樣代碼的插件_open事件反正

Heres a link that shows how to do Application Events