Arrays Excel VBA-确定数组UDF的列或行目标
我有一个简单的excel UDF,用于将质量值数组转换为摩尔分数。大多数情况下,输出将是一个n行乘1列的列数组 在VBA环境中,我如何确定工作表上目标单元格的尺寸,以确保它应以n行乘1列而不是n列乘1行的形式返回Arrays Excel VBA-确定数组UDF的列或行目标,arrays,excel,vba,user-defined-functions,Arrays,Excel,Vba,User Defined Functions,我有一个简单的excel UDF,用于将质量值数组转换为摩尔分数。大多数情况下,输出将是一个n行乘1列的列数组 在VBA环境中,我如何确定工作表上目标单元格的尺寸,以确保它应以n行乘1列而不是n列乘1行的形式返回 Function molPct(chemsAndMassPctsRng As Range) Dim chemsRng As Range Dim massPctsRng As Range Dim molarMasses() Dim molPcts() Set chemsRng = c
Function molPct(chemsAndMassPctsRng As Range)
Dim chemsRng As Range
Dim massPctsRng As Range
Dim molarMasses()
Dim molPcts()
Set chemsRng = chemsAndMassPctsRng.Columns(1)
Set massPctsRng = chemsAndMassPctsRng.Columns(2)
chems = oneDimArrayZeroBasedFromRange(chemsRng)
massPcts = oneDimArrayZeroBasedFromRange(massPctsRng)
'oneDimArrayZeroBasedFromRange is a UDF to return a zero-based array from a range.
ReDim molarMasses(UBound(chems))
ReDim molPcts(UBound(chems))
totMolarMass = 0
For chemNo = LBound(chems) To UBound(chems)
molarMasses(chemNo) = massPcts(chemNo) / mw(chems(chemNo))
totMolarMass = totMolarMass + molarMasses(chemNo)
Next chemNo
For chemNo = LBound(chems) To UBound(chems)
molPcts(chemNo) = Round(molarMasses(chemNo) / totMolarMass, 2)
Next chemNo
molPct = Application.WorksheetFunction.Transpose(molPcts)
End Function
我知道,若并没有其他内容,我可以有一个输入参数来标记返回是否应该作为行数组。我不希望这样做。您应该返回二维数组:n×1表示行,1×n表示列向量 所以你需要任何一个
Redim molPcts(1, Ubound(chems) + 1)
或
要引用它们,您需要使用两个索引:
molPcts(1, chemNo + 1)
或
如果您更喜欢基于0的阵列,则redim应如下所示:
Redim molPcts(0 To 0, 0 To Ubound(chems))
Redim molPcts(0 To Ubound(chems), 0 To 0)
以下是一个UDF的小示例: 接受可变数量的输入范围 提取这些范围内的唯一值 创建合适的输出数组列、行或块 将唯一值转储到该区域
非常感谢。如果我使用二维数组,在VBA中,我如何确定它们需要是1乘n还是n乘1?这是否取决于excel工作表上目标单元格的尺寸?如果是,是否有方法从VBA环境中检测目标单元格的尺寸?在UDF应用程序中。此单元格提供包含公式的范围。可以肯定的是,这也适用于数组公式。调用方类似。您应该能够使用其中一种方法对输出数组进行尺寸标注。非常感谢。应用程序。调用方应该可以工作。此外,我还需要学习更多关于收藏的知识。它们似乎比数组灵活得多。@MichaelJames让数组和集合一起工作很容易。。。。。。。。。。。。。。。。。。。。
molPcts(chemNo + 1, 1)
Redim molPcts(0 To 0, 0 To Ubound(chems))
Redim molPcts(0 To Ubound(chems), 0 To 0)
Public Function ExtractUniques(ParamArray Rng()) As Variant
Dim i As Long, r As Range, c As Collection, OutPut
Dim rr As Range, k As Long, j As Long
Set c = New Collection
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' First grab all the data and make a Collection of uniques
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
On Error Resume Next
For i = LBound(Rng) To UBound(Rng)
Set r = Rng(i)
For Each rr In r
c.Add rr.Value, CStr(rr.Value)
Next rr
Next i
On Error GoTo 0
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' next create an output array the same size and shape
' as the worksheet output area
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
k = 1
With Application.Caller
ReDim OutPut(1 To .Rows.Count, 1 To .Columns.Count)
End With
For i = LBound(OutPut, 1) To UBound(OutPut, 1)
For j = LBound(OutPut, 2) To UBound(OutPut, 2)
If k < c.Count + 1 Then
OutPut(i, j) = c.Item(k)
k = k + 1
Else
OutPut(i, j) = ""
End If
Next j
Next i
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' put the data on the sheet
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
ExtractUniques = OutPut
End Function