2015-04-07 75 views
0

我想教自己如何使用vba类模块。这是我的班级模块(名为clsWorkingRange)。这里的模块:class property not working excel vba

Option Explicit 

Private pSheetName As String 
Private pColNum As Integer 

Public Property Get SheetName() As String 
    SheetName = pSheetName 
End Property 

Public Property Let SheetName(SheetName As String) 
    pSheetName = SheetName 
End Property 

Public Property Get ColNum() As Integer 
    ColNum = pColNum 
End Property 

Public Property Let ColNum(ColNumb As Integer) 
    pColNum = ColNum 
End Property 

Public Function Get_Rows_Generic(work_sheet_get As String, column_num As Integer) 
'worksheet is the name of a sheet in the form of a string 
    Dim ws As Worksheet:    Set ws = Worksheets(work_sheet_get) 
    Dim rows_count As Long:     rows_count = ws.Cells(rows.Count, column_num).End(xlUp).Row 
    Get_Rows_Generic = rows_count 
End Function 

我想在这里起诉类(在一个单独的模块):

Option Explicit 

Sub test_the_class() 
    Dim first_class_example As New clsWorkingRange 

    first_class_example.SheetName = "agentsFullOutput.csv" 
    first_class_example.ColNum = 1 

     Debug.Print first_class_example.SheetName 
     Debug.Print first_class_example.ColNum 

    Dim row_count_test As Long 
    row_count_test = first_class_example.Get_Rows_Generic(first_class_example.SheetName, first_class_example.ColNum) 

    MsgBox "row count is " & row_count_test 

End Sub 

Debug.Print first_class_example.ColNum打印0。为什么不是财产通过1?

回答

0

只是becase的,你应该改变这种

Public Property Let ColNum(ColNumb As Integer) 
    pColNum = ColNum 
End Property 

这样:

Public Property Let ColNum(ColNumb As Integer) 
    pColNum = ColNumb 
End Property 

)。

+0

我有多愚蠢?谢谢。 – dwstein