2015-11-03 92 views
0

任何人都可以告诉我为什么我在下面的代码中出现“运行时错误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 

仅供参考,我想此宏从一系列返回的单元格值满足的多个标准,该用户还限定(lookupvalues)一个用户定义(lookuprange)。然后可以从用户定义的范围(返回范围)开始返回适用的单元格值。所有这些用户输入都是通过输入框完成的。当用户只选择一个标准时,我已经获得了宏的工作方式,但我一直在尝试修改代码,以便可以选择多个标准/单元格。错误发生在这条线上:

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

非常感谢您的帮助!

+0

是lookupvalues范围或变体?你把它当作两者来对待。我的猜测是这是一个范围,需要改变。 –

+1

'如果InStr(1,cell2,cell)> 0 Then'? – omegastripes

回答

0

按我的意见,这是我最好的猜测,你正在尝试

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 
+1

@ christopheralan88反馈总是有帮助的。这有帮助吗?如果不是什么不适合你?如果它确实认为标记是正确的。 –