【问题标题】:Value used in formula of wrong data type错误数据类型的公式中使用的值
【发布时间】:2014-07-26 00:10:50
【问题描述】:

过去几天我一直在尝试解决这个错误,但没有成功。我希望你们中的一个能够提供帮助。我得到“在错误数据类型的公式中使用的值。

快速解释: 将这样的函数转换为相应的文本 (20054/18393)*100.0 5 位数字是指问题的字段 ID。

身份证问题 20054 你一年工作多少天 18393 一年有多少天的假期

我想要得到的结果是(你一年中有多少天工作/你一年有多少天假期)*100.0 如果只是一只手,它可以很容易地手动完成。我有超过 2600 个公式需要转换。 我在下面创建了这个函数,这导致了标题中提到的错误。任何帮助将不胜感激

这是我的功能

Function Test(sInput As String) As String
Dim i As Long
Dim num As String
Dim Text, a, str, shortname As String

For i = 1 To Len(sInput)
    a = Mid(sInput, i, 1)
    If IsNumeric(a) Then
        num = num & a
        Text = ""
    Else
        If a = "." Then
            num = num & a
        Else
            'search for num value in second sheet short name
            shortname = WorksheetFunction.VLookup(WorksheetFunction.Int(num), Worksheets("questionlist").Range("A3:F2537"), 5, False)
            num = ""
        End If

        Text = shortname & a
        shortname = ""
    End If
    str = str & Text
Next

Test = str
End Function

【问题讨论】:

  • 单步执行时哪一行会出现错误?此外,虽然它不会导致错误,但请注意您的第三个 DIM 语句实际上创建了变体,而不是字符串。您必须将 AS 单独应用于每个变量。
  • 感谢 Lance 的提示,我按照您的建议更改了 DIM 语句。当我完成这一步时,它不会在任何特定行中断。该函数执行并且我在单元格中得到一个#Value。
  • 请编辑您的问题以解释该函数的预期输入和输出!
  • 在逐步执行时,使用局部(或立即)窗口查看变量的值。

标签: function vba excel excel-2010


【解决方案1】:

引发错误是因为您在行中将空白值传递给INT Function

 WorksheetFunction.VLookup(WorksheetFunction.Int(num), Worksheets("questionlist").Range("A3:F2537"), 5, False)

重现错误 在任何单元格中键入=INT("")

修复这个句柄空白值

更新答案:

Function Formula2Text(ByRef myCell As Range) As String
Dim QuestionId As Integer
Dim strInput As String

'Get Formula instead of values
strInput = myCell.FormulaR1C1

'Use Regex to Catch all ID's
 Set Regex = CreateObject("VBScript.RegExp")
 Set rnglookup = Worksheets("questionlist").Range("A3:F2537")
  Regex.Global = True
  Regex.Pattern = "\d+"     

For Each Match In Regex.Execute(strInput)

    'Skip if the ID is 100
    If (Match.Value <> 100) Then QuestionId = Match.Value

    'Lookup ID in the rnglookup,Make sure the Ids are sorted in asc in the questionlist sheet
    Qntxt = Application.VLookup(QuestionId, rnglookup, 5, False)

    If IsError(Qntxt) Then Qntxt = "Missing Lookup"

   'Replace the ID with the lookup
    strInput = Replace(strInput, QuestionId, Qntxt)

Next
Formula2Text = strInput

End Function

用法:在公式旁边的单元格中,通过引用公式来使用函数

 =Formula2Text(A1)

【讨论】:

  • 说实话。我不知道该怎么做。我对vba很陌生。您提供的任何指导将不胜感激。
  • 我仍然无法弄清楚您要做什么,请编辑您的问题以强调问题而不是错误,然后有人可以提供帮助!
  • 我必须在 Excel 表格中处理第一张表格,其中包含这样的公式 (20054/18393)*100.0 第二张表格有一个 ID 列和一个文本列,ID 列对应于上述公式中的 5 位数字。我想要做的是在表 1 上创建一个新列,复制公式,但不是在公式中包含 id,而是在公式中包含与 ID 相对应的文本。这样做会清除一点。
  • 我想我现在明白了!
  • 非常感谢您的帮助。另一件事是并非所有的公式都是相同的。有些公式可能看起来像这样 (11778/(13214+11740+11748+11778))*100
猜你喜欢
  • 1970-01-01
  • 2013-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 1970-01-01
相关资源
最近更新 更多