仅数字部分单元格的平均值,避免使用任何字母



我正在尝试使用一个公式来告诉我包含数字和字母的单元格的平均值。

Price
$486.5/mt,
$287/mt,
$286.75/mt,
$286.5/mt,
$286.75/mt,

因此,每个单元格都包含上述一项,例如单元格 F2 包含"486.5 美元/吨"。我的问题是我可以使用什么公式来仅返回数字平均值。目前,平均公式甚至不起作用。我尝试使用"LEFT"公式,但这不起作用,因为 $ 符号位于值之前。

请帮忙!

谢谢

您可以在没有 VBA 的情况下执行此操作,使用如下所示的数组公式:

=AVERAGE(VALUE(MID(K18:K22,2,LEN(K18:K22)-5)))  

(K18:K22 中的值(

您可以使用此函数:

Public Function NumbersOnly(str As String) As Single
Dim c As String
Dim nIndex As Integer
Dim sResult As String
For nIndex = 1 To Len(str)
If IsNumeric(Mid$(str, nIndex, 1)) Or Mid$(str, nIndex, 1) = "." Then
sResult = sResult & Mid$(str, nIndex, 1)
End If
Next
NumbersOnly = Val(sResult)
End Function

然后单元格F3的公式将是:

=NumbersOnly(F2)

采用与braX类似的方法,此函数将显式转换预定义格式的字符串($....../...(,这将防止格式不正确(或其他无效(字符串的任何问题。这样可以更好地控制退货。

Public Function GetAmountFromString(ByVal InputString As String) As Variant
If InputString Like "$*/*" Then
Dim ProcessedString As String
ProcessedString = Replace(InputString, "$", vbNullString)
Dim SplitString As Variant
SplitString = Split(ProcessedString, "/")
' Explicitly convert String to Currency.
GetAmountFromString = CCur(SplitString(0))
Else
GetAmountFromString = vbNullString
End If
End Function

此函数将首先检查格式。只要输入格式正确,它就会用空字符串替换"$"符号。最后,它将使用"/"将字符串分成两部分。它将在"/"符号之前返回值的Currency版本。

如果输入不正确,它将返回一个空字符串。这可以根据需要进行自定义(您可以将vbNullString替换为"ThisIsAnInvalidInput"并且它仍然可以正常工作。

若要使用此函数,请将其添加到 VBProject,然后使用工作表中=GetAmountFromString(Cell)的函数。

您还可以从其他子例程中使用此函数(例如,如果要在仅平均有效值的子例程中使用该函数(。

相关内容

最新更新