我将名为“Info”的公共类型设置为以下
Public Type info
nAtt As Integer
nObs As Integer
rngData As Range
End Type
我写了一个函数,目的是在输入标题的范围时返回3个对象(nAtt:属性数; nObs:观察数; rngData:相应核心数据的范围)
Public Function GetDatasetInfo(rng As Range) As info
Dim sets As info
Dim rowRef1 As Integer
rowRef1 = rng.Row
Dim colRef1 As Integer
colRef1 = rng.Column
Dim colRef2 As Integer
colRef2 = colRef1 + rng.Columns.Count - 1
Dim nAtt As Integer
nAtt = rng.Columns.Count
Dim nObs As Integer
Dim rngOutput As Range
If (Cells(rowRef1 + 1, rng.Column).Value = "") Then
nObs = 0
Set rngOutput = rng
Else
nObs = rng.Offset(1, 0).End(xlDown).Row - rowRef1
Set rngOutput = Range(Cells(rowRef1 + 1, colRef1), _
Cells(rowRef1 + nObs, colRef2))
End If
With sets
.nAtt = nAtt
.nObs = nObs
.rngData = rngOutput
End With
GetDatasetInfo = sets
End Function
以下是数据的用法:
Sub Main()
Dim rng As Range
Set rng = Range("rng1")
Dim v1 As info
v1 = GetDatasetInfo(rng)
MsgBox v1.nObs
v1.rngData.Select
End Sub
代码运行良好,但未在函数中添加.rngData = rngOutput
。并且错误消息表明此行有问题。
请指教。非常感谢你的帮助。