【问题标题】:Data Type Mismatch Error Even Though Data Types Are the Same即使数据类型相同,也会出现数据类型不匹配错误
【发布时间】:2015-11-03 15:43:46
【问题描述】:

谁能告诉我为什么在下面的代码中出现“运行时错误 13:类型不匹配”? cell2 和lookupvalues 变量都是变体数据类型,所以我不确定问题出在哪里。

Sub MULTVLOOKUP()

    Dim lookupvalues As Variant
    Dim lookuprange As Range
    Dim returnrange As Range
    Dim cell As Variant
    Dim cell2 As Variant

    lookupvalues = Application.InputBox(Prompt:="What value do you want to      search for?", Title:="lookupvalue", Type:=8)
    Set lookuprange = Application.InputBox("What range do you want to search for this value in?", "lookuprange", Type:=8)
    Set returnrange = Application.InputBox("What cell do you want the results to begin being returned at?", "returnrange", Type:=8)

    For Each cell In lookupvalues
        For Each cell2 In lookuprange
            If InStr(1, cell2, lookupvalues) > 0 Then
                returnrange = cell2
                Set returnrange = returnrange.Offset(1, 0)
            End If
        Next
    Next

End Sub

仅供参考,我希望此宏从用户定义的范围(查找范围)返回满足用户还定义的多个条件之一的单元格值(查找值)。然后将从用户定义的范围(返回范围)开始返回适用的单元格值。所有这些用户输入都是通过输入框完成的。当用户只选择一个标准时,我已经让宏工作,但我一直在尝试修改代码以便可以选择多个标准/单元格。此行发生错误:

If InStr(1, cell2, lookupvalues) > 0 Then

非常感谢您的帮助!!

【问题讨论】:

  • lookupvalues 是范围还是变体?您将其视为两者。我的猜测是它是一个范围,需要更改为这样。
  • If InStr(1, cell2, cell) > 0 Then ?

标签: excel runtime-error vba


【解决方案1】:

根据我的 cmets,这是我对您正在尝试的最佳猜测

Sub MULTVLOOKUP()

    Dim lookupvalues As Range
    Dim lookuprange As Range
    Dim returnrange As Range
    Dim cell As Variant
    Dim cell2 As Variant

    Set lookupvalues = Application.InputBox(Prompt:="What value do you want to      search for?", Title:="lookupvalue", Type:=8)
    Set lookuprange = Application.InputBox("What range do you want to search for this value in?", "lookuprange", Type:=8)
    Set returnrange = Application.InputBox("What cell do you want the results to begin being returned at?", "returnrange", Type:=8)

    For Each cell In lookupvalues
        For Each cell2 In lookuprange
            If InStr(1, cell2, cell.Value) > 0 Then
                returnrange = cell2
                Set returnrange = returnrange.Offset(1, 0)
            End If
        Next
    Next

End Sub

【讨论】:

  • @christopheralan88 反馈总是有帮助的。这有帮助吗?如果不是,什么对你不起作用?如果它确实认为标记为正确。
猜你喜欢
  • 1970-01-01
  • 2023-04-01
  • 2020-03-27
  • 2010-10-23
  • 2019-05-24
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多