VBA UDF 多单元参考

发布于 2024-11-25 11:23:41 字数 1893 浏览 0 评论 0原文

我正在尝试创建一个 UDF,它从 4 个选定的单元格中获取值并用它执行一个函数。但是我不知道如何编写函数来识别单元格值。按照我目前编写的方式,我需要输入一个值。谁能帮我解决这个相对简单的问题?这是我到目前为止所拥有的。

Function solubility(anion As String, cation As String, carbon1 As String, carbon2 As String)

编辑

例如,我尝试选择单元格 A、C、E 和 G,它们包含 Tf、Mi、Ch 和 Ch 的值。然后,我将使用这些单元格引用范围内的相应值来计算并显示最终值。下面提供了完整的代码。

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

I am trying to create a UDF that takes the values from 4 selected cells and performs a function with it. However I don't know how to write the function to recognize the cell values. 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. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸伞微斜 2024-12-02 11:23:41

假设第 1 行,像这样调用函数:

=solubility(A1, C1, E1, G1)

Assuming Row 1, call the Function like this:

=solubility(A1, C1, E1, G1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文