2016-11-13 27 views
1

我有一個VBA腳本象下面這樣:VBA Sub或功能,而在一類叫公共/私有子沒有定義錯誤

Sub button_click() 
' 
' < some code > 
' 
    call FindStrings (strfolder, Nothing) 
End Sub 

Public Sub FindStrings(strFolder As String, Optional wksSheet As Worksheet = Nothing) 
' 
' < some code> 
' 
    Call ProcessFolder(strFolder, strIndent, varStrings, varMatchesFound, varFileNames, lngFolderCount, lngFileCount) 

End sub 

Private Sub ProcessFolder(strFolder As String, ByRef strIndent As String, ByRef varStrings As Variant, ByRef varMatchesFound As Variant, ByRef varFileNames As Variant, ByRef lngFolderCount As Long, lngFileCount As Long) 
' 
' < some code> 
' 
Call ProcessFile(objFile.Path, strIndent, varStrings, varMatchesFound, varFileNames) 

End sub 

Private Sub ProcessFile(strFullPath As String, ByRef strIndent As String, ByRef varStrings As Variant, ByRef varMatchesFound As Variant, ByRef varFileNames As Variant) 
' 
' < some code> 
' 
End sub 

目前,button_clickFindStrings處於ModuleProcessFolderProcessFile在一個Class

當運行button_click子程序中,它引發錯誤:

Sub or function not defined

會出現錯誤在對FindStringsCall ProcessFolder...線。

我已經搜索了很多與錯誤有關的問題子或函數沒有定義,也試圖實現他們的修改建議糾正此錯誤,但它不起作用。

有關這個錯誤是什麼以及如何糾正它的任何幫助,將不勝感激。

+1

是那些都在同一模塊? –

+0

不,我在標準模塊ProcessFolder()和ProcessFile()中放置了button_click()和FindStrings(),因爲我發現有人提到它可以解決錯誤,但它也可以工作。它們分爲四個不同的模塊@TimWilliams – S6633d

+1

我將首先將它們全部放在同一個模塊中 –

回答

0

如果要調用從Module內的Class中的方法,那麼你需要實例化New關鍵字的類。然後用點符號表示Class方法。

一般的例子是:

'Module 
Public Sub Foo() 
    Dim o As Class1 
    Set o = New Class1 
    o.Bar 
End Sub 

'Class1 
Public Sub Bar() 
    MsgBox "Hello world" 
End Sub 

您例如這意味着你會怎麼做:

'Module 
Sub button_click() 
'< some code > 
    Call FindStrings (strfolder, Nothing) 
End Sub 

Public Sub FindStrings(strFolder As String, Optional wksSheet As Worksheet = Nothing) 
' < some code> 
    Dim o As YourClass 
    Set o = New YourClass 
    o.ProcessFolder(strFolder, strIndent, varStrings, varMatchesFound, varFileNames, lngFolderCount, lngFileCount) 

End Sub 

'YourClass 
Public Sub ProcessFolder(strFolder As String, ByRef strIndent As String, ByRef varStrings As Variant, ByRef varMatchesFound As Variant, ByRef varFileNames As Variant, ByRef lngFolderCount As Long, lngFileCount As Long) 
' < some code> 
    Call ProcessFile(objFile.Path, strIndent, varStrings, varMatchesFound, varFileNames) 

End Sub 

Private Sub ProcessFile(strFullPath As String, ByRef strIndent As String, ByRef varStrings As Variant, ByRef varMatchesFound As Variant, ByRef varFileNames As Variant) 
' < some code> 
End Sub 

需要注意的是:

  • FindStrings你需要創建一個新的實例YourClass
  • YourClassProcessFolder子程序需要是Public