Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3009|回复: 2

[已解决]求大神 看看为什么 自定义的函数 总是返回#value

[复制链接]
发表于 2014-11-23 10:40 | 显示全部楼层 |阅读模式
本帖最后由 Ex_EP 于 2014-11-23 13:18 编辑

我 这个代码 用 sub 过程 可以实现 不知道为什么 改成了 function 函数 总是无法实现


要求是:1、表中的  A B  C  D 列 为数据区域  ,F G 为条件 区域 ,如果 数据区域中的 订单号 和 品名都一样   则将 C  D 列的数据 汇总成一句话  ,显示在  H 列   效果 如 H 列 所示  

我的代码 已经写好了  就是不知道 为什么 总是返回#value 这样的错我  请高手赐教
  1. Function lianhua(danhao As Range, pinming As Range)
  2. Dim c As Range, str$, q!
  3. '查找订单号出现的首行和最后一行
  4. With Range("a1:a" & Cells(Rows.Count, 1).End(xlUp).Row)
  5. Set c = .Find(danhao.Value, , xlValues, xlWhole, xlByColumns)
  6. If Not c Is Nothing Then
  7.     firstaddress = c.Address
  8. Do
  9.     lastaddress = c.Address
  10.     Set c = .FindNext(c)
  11. Loop While Not c Is Nothing And firstaddress <> c.Address
  12. End If
  13. End With
  14. '如果查找不到相应的订单号,则进行相应的错误处理
  15. If c Is Nothing Then q = q + 1: GoTo 100
  16. '根据查找到的订单号,设置品名的查找区域
  17. With Range("b" & (Range(firstaddress).Row - 1) & ":b" & Range(lastaddress).Row)
  18. Set c = .Find(pinming.Value, , xlValues, xlWhole, xlByColumns)
  19. '查找品名出现的首行和最后一行
  20. If Not c Is Nothing Then
  21.     firstaddress1 = c.Address
  22. Do
  23.     lastaddress1 = c.Address
  24.     Set c = .FindNext(c)
  25.     If c.Row = Range(firstaddress).Row - 1 Then Exit Do
  26. Loop While Not c Is Nothing And firstaddress1 <> c.Address
  27. End If
  28. End With
  29. '如果查找不到品名,则进行相应的错误处理
  30. If c Is Nothing Then q = q + 1: GoTo 100
  31. '根据查找到的品名的首行和最后一行,设置相应的废板数和质量说明区域
  32. a = Range("c" & Range(firstaddress1).Row & ":d" & Range(lastaddress1).Row).Value
  33. '采用循环的方式将废板数量和质量说明汇总成一句话
  34. For i = 1 To UBound(a)
  35. str = str & a(i, 1) & a(i, 2) & "片,"
  36. Next i
  37. '查找不到订单号或者品名时进行的错误处理
  38. If q <> 0 Then
  39. 100:

  40.     lianhua = ""
  41. End
  42. Else

  43.     lianhua = Left(str, Len(str) - 1)
  44. End If
  45. End Function
复制代码
最佳答案
2014-11-23 12:43
Ex_EP 发表于 2014-11-23 12:25
如果我表述的  大家 看不明白  还可以再问我
  1. Function demo(区域 As Range, 条件 As Range)
  2.     Dim ar, br, d As Object, str As String, tj As String, i As Long
  3.     Set d = CreateObject("scripting.dictionary")
  4.     ar = 区域: br = 条件
  5.     If UBound(br) = 1 And UBound(br, 2) = 2 Then
  6.         tj = br(1, 1) & br(1, 2)
  7.         For i = 1 To UBound(ar)
  8.             str = ar(i, 1) & ar(i, 2)
  9.             d(str) = d(str) & ar(i, 3) & ar(i, 4) & "片,"
  10.         Next
  11.         demo = d(tj)
  12.     End If
  13. End Function
复制代码

废板数量质量说明连成一句话.rar

145.82 KB, 下载次数: 16

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2014-11-23 12:25 | 显示全部楼层
如果我表述的  大家 看不明白  还可以再问我
回复

使用道具 举报

发表于 2014-11-23 12:43 | 显示全部楼层    本楼为最佳答案   
Ex_EP 发表于 2014-11-23 12:25
如果我表述的  大家 看不明白  还可以再问我
  1. Function demo(区域 As Range, 条件 As Range)
  2.     Dim ar, br, d As Object, str As String, tj As String, i As Long
  3.     Set d = CreateObject("scripting.dictionary")
  4.     ar = 区域: br = 条件
  5.     If UBound(br) = 1 And UBound(br, 2) = 2 Then
  6.         tj = br(1, 1) & br(1, 2)
  7.         For i = 1 To UBound(ar)
  8.             str = ar(i, 1) & ar(i, 2)
  9.             d(str) = d(str) & ar(i, 3) & ar(i, 4) & "片,"
  10.         Next
  11.         demo = d(tj)
  12.     End If
  13. End Function
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-15 20:29 , Processed in 0.270423 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表