类属性不工作excel vba

时间:2015-04-07 04:13:38

标签: class excel-vba vba excel

我试图自学如何使用vba类模块。这是我的课程模块(名为clsWorkingRange)。这是模块:

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?

1 个答案:

答案 0 :(得分:1)

只是因为你应该改变这个

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

到此:

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

;)