[英]VBA UDF multi-cell reference
I am trying to create a UDF that takes the values from 4 selected cells and performs a function with it.我正在尝试创建一个 UDF,它从 4 个选定单元格中获取值并使用它执行 function。 However I don't know how to write the function to recognize the cell values.但是我不知道如何编写 function 来识别单元格值。 The way I have it currently written I need to input a value.我目前编写的方式需要输入一个值。 Can anyone help me with this relatively simple problem?谁能帮我解决这个相对简单的问题? This is what I have so far.这就是我到目前为止所拥有的。
Function solubility(anion As String, cation As String, carbon1 As String, carbon2 As String)
EDIT编辑
So for example I am trying to select cells A, C, E, and G and they contain values of Tf, Mi, Ch, and Ch.例如,我正在尝试 select 单元格 A、C、E 和 G,它们包含 Tf、Mi、Ch 和 Ch 的值。 I will then use these cells refer to corresponding values in a range to calculate and display a final value.然后,我将使用这些单元格引用范围内的相应值来计算和显示最终值。 THe whole code is provided below.下面提供了整个代码。
Function solubility(anion As String, cation As String, carbon1 As String, carbon2 As String)
Dim sum As Double
Dim ncavalue As Long, nanvalue As Long, ncb1value As Long, ncb2value As Long
Dim nca As Long, nan As Long, ncab As Long
Dim coeff As Range, groups As Range
'solubility groups range
groups = Worksheets("Solubility").Range("A2:A33")
'group coefficients range
coeff = Worksheets("Solubility").Range("B2:B33")
'number of groups for each group
ncavalue = Range("AE" & cation.Row)
nanvalue = Range("AC" & anion.Row)
ncb1value = Range("AG" & carbon1.Row)
ncb2value = Range("AI" & carbon2.Row)
j = 0
For j = 0 To UBound(groups)
If UCase(anion.Value) = UCase(coeff(j).Value) Then
'coefficient value
anvalue = coeff(j).Value
End If
If UCase(cation.Value) = UCase(coeff(j).Value) Then
'coefficient value
cavalue = coeff(j).Value
End If
If cation.Value = "[MIm]" Then
cavalue = Range("B2")
ncb1value = ncb1value.Value + 1
End If
If UCase(carbon1.Value) = UCase(coeff(j).Value) Then
'coefficient value
cb1value = coeff(j).Value
End If
If UCase(carbon2.Value) = UCase(coeff(j).Value) Then
'coefficient value
cb2value = coeff(j).Value
End If
Next j
sum = anvalue * nanvalue + cavalue * ncavalue + cb1value * ncb1value + cb2value * ncb2value
solubility = sum + Range("B34").Value
End Function
Assuming Row 1, call the Function like this:假设第 1 行,像这样调用 Function:
=solubility(A1, C1, E1, G1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.