如何使用Excel Sheet作为函数?

时间:2016-10-15 20:05:47

标签: excel vba function macros

我正试图找到一种方法来使用一张excel表作为函数。

我的文件中的工作表上有一个宏,我想在另一个工作表中插入宏的结果,以便在第一个工作表中输入多个输入组合。

正如在这个非常古老的问题中提出的那样:Excel: using a worksheet as a function?,我正在尝试使用Excel VBA,通过对我的工作表做同样的事情,但我收到了一个“#VALUE”错误,它会是什么?

这是代码

Function TEXTURE(sand, clay)
Sheets("Texture Calculator").Range("B4").Value = sand
Sheets("Texture Calculator").Range("D4").Value = clay
TEXTURE = Sheets("Texture Calculator").Range("G4").Value
End Function

我是VBA的新手,我只是试图通过旧帖子的试用找到解决方案

Best,Giulio

2 个答案:

答案 0 :(得分:0)

不确定我完全理解你的问题,但听起来你想要建立沙子,淤泥和粘土的输入%以及宏观中随后的土壤类型确定。

我将输入单元格的格式从“百分比”更改为“常规”并输入百分比,使得20为20%,15为15%等。土壤基本分类的工作表(“计算”)上的不等式在土壤纹理三角形之后,在if / elseif语句中对其进行了硬编码。看看这是否有帮助:

Notice inputs are not in percentages

Sub Test()
Dim i As Integer: Dim sand_percent As Long: Dim clay_percent As Long: 
Dim silt_percent As Long:
Dim usda_texture As Variant:

For i = 4 To 7
  With Sheets("Texture Calculator")
      sand_percent = Range("B" & i).Value
      clay_percent = Range("D" & i).Value
      silt_percent = Range("F" & i).Value

      usda_texture = Texture_Determination(sand_percent, clay_percent, silt_percent)
      Range("G" & i).Value = usda_texture
  End With
Next i

End Sub

Function Texture_Determination(Sand As Long, Clay As Long, Silt As Long) As Variant

If Sand + Clay + Silt <> 100 Then
   MsgBox "Please ensure your inputs add up to 100%"
   Exit Function
End If

   'Determine the kind of soil texture using if/elseifs

If (Silt + 1.5 * Clay) < 15 Then
   soil = "Sand"
ElseIf Silt + 1.5 * Clay >= 15 And (Silt + 2 * Clay < 30) Then
   soil = "Loamy Sand"
ElseIf ((Clay >= 7 And Clay < 20) And (Sand > 52) And ((Silt + 2 * Clay) >= 30) Or (Clay < 7 And Silt < 50 And (Silt + 2 * Clay) >= 30)) Then
   soil = "Sandy Loam"
ElseIf ((Clay >= 7 And Clay < 27) And (Silt >= 28 And Silt < 50) And (Sand <= 52)) Then
   soil = "Loam"
ElseIf ((Silt >= 50 And (Clay >= 12 And Clay < 27)) Or ((Silt >= 50 And Silt < 80) And Clay < 12)) Then
   soil = "Silt"
ElseIf ((Clay >= 20 And Clay < 35) And (Silt < 28) And (Sand > 45)) Then
   soil = "Sandy Clay Loam"
ElseIf ((Clay >= 27 And Clay < 40) And (Sand > 20 And Sand <= 45)) Then
   soil = "Clay Loam"
ElseIf ((Clay >= 27 And Clay < 40) And (Sand <= 20)) Then
   soil = "Silty Clay Loam"
ElseIf (Clay >= 35 And Sand > 45) Then
   soil = "Sandy Clay"
ElseIf (Clay >= 40 And Silt >= 40) Then
   soil = "Silty Clay"
ElseIf (Clay >= 40 And Sand <= 45 And Silt < 40) Then
   soil = "Clay"
Else
soil = "Could not determine soil texture"
End If

Texture_Determination = soil

End Function

答案 1 :(得分:0)

正如您已经被告知的那样,UDF无法改变环境。

但是有一些解决方法

例如

  • 按如下方式更改您的功能:

    Function TEXTURE(sand, clay)
        TEXTURE = "Texture Calculator|" & sand & "|" & clay
    End Function
    
  • 将以下代码放在要使用TEXTURE()UDT的工作表代码窗格中:

    Private Sub Worksheet_Change(ByVal Target As Range)
        If InStr(Target.Value, "Texture Calculator|") > 0 Then
            Application.EnableEvents = False
            With Worksheets("Texture Calculator")
                .Range("B4").Value = Split(Target.Value, "|")(1)
                .Range("D4").Value = Split(Target.Value, "|")(2)
                Target.Value = .Range("G4").Value
            End With
            Application.EnableEvents = True
        End If
    End Sub
    

只要您输入=TEXTURE(number1,numnber2)

,这将在单元格中保留原始TEXTURE()函数的结果