Вопрос

I would like to use this property:

Public Property Get HasNoData() As Boolean

   HasNoData = (numberOfColumns < 2 And numberOfRows < 2)

End Property

Sub test()

Dim numberOfColumns As Long
Dim numberOfRows As Long

numberOfColumns = 5
numberOfRows = 3

If HasNoData Then
MsgBox True
Else:
MsgBox False

End If

End Sub

Everytime I get TRUE, no matter whether the conditions are met or not. I might get the whole idea wrong, so please let me know.

Это было полезно?

Решение

There are a few things that are wrong with your code as it stands now:

  1. Properties can only be members of a class
  2. Variables are subject to what is known as scope, and specifically your two variables numberOfColumns and numberOfRows are only available within the scope of the test() Sub, so they and their values cannot be seen inside the HasNoData() property unless they are passed as parameters or set as members of the class
  3. The VBA Msgbox takes a String as the parameter, whereas you have provided a Boolean

I think what you really want is to make "HasNoData" into a function that takes two parameters, one for rows and one for columns. Try this:

Public Function HasNoData(NumRows, NumCols) As Boolean
   HasNoData = (NumCols < 2 And NumRows < 2)
End Function

Sub test()
    If HasNoData(3, 5) Then
        MsgBox "There is data!"
    Else
        MsgBox "There is no data."
    End If
End Sub
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top