Excel精英培训网

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

[已解决]自定义多表查找的问题

[复制链接]
发表于 2012-4-21 10:06 | 显示全部楼层 |阅读模式
  1. Public Function nvl(cz, co)
  2. '多表精确查找,类似于VLOOKUP功能
  3. 'NVL(查找对象,超找到的内容偏移列数),查找不到返回的是0
  4. Dim rng As Range, sht As Worksheet
  5. On Error Resume Next
  6. For Each sht In Worksheets
  7.     If sht.Name <> ActiveSheet.Name Then
  8.         Set rng = sht.Cells.Find(what:=cz, lookat:=xlWhole).Offset(0, co)
  9.             If Not rng Is Nothing Then
  10.                 nvl = rng.Value
  11.                 Exit For
  12.             End If
  13.     End If
  14. Next
  15. End Function
复制代码


这段自定义函数代码,在查找不到数据的时候,返回的是0.如何实现,像普通函数一样,超找不到的时候返回错误类型?

最佳答案
2012-4-21 11:22
  1. Public Function nvl(cz, co)
  2. '多表精确查找,类似于VLOOKUP功能
  3. 'NVL(查找对象,超找到的内容偏移列数),查找不到返回的是0
  4. Dim rng As Range, sht As Worksheet
  5. On Error Resume Next
  6. For Each sht In Worksheets
  7.     If sht.Name <> ActiveSheet.Name Then
  8.         Set rng = sht.Cells.Find(what:=cz, lookat:=xlWhole).Offset(0, co)
  9.             If Not rng Is Nothing Then
  10.                 nvl = rng.Value
  11.                 Exit For
  12.             End If
  13.     End If
  14. Next
  15.     If rng Is Nothing Then nvl = "查找不到"
  16. End Function
复制代码
可以显示查找不到!
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2012-4-21 11:22 | 显示全部楼层    本楼为最佳答案   
  1. Public Function nvl(cz, co)
  2. '多表精确查找,类似于VLOOKUP功能
  3. 'NVL(查找对象,超找到的内容偏移列数),查找不到返回的是0
  4. Dim rng As Range, sht As Worksheet
  5. On Error Resume Next
  6. For Each sht In Worksheets
  7.     If sht.Name <> ActiveSheet.Name Then
  8.         Set rng = sht.Cells.Find(what:=cz, lookat:=xlWhole).Offset(0, co)
  9.             If Not rng Is Nothing Then
  10.                 nvl = rng.Value
  11.                 Exit For
  12.             End If
  13.     End If
  14. Next
  15.     If rng Is Nothing Then nvl = "查找不到"
  16. End Function
复制代码
可以显示查找不到!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 23:34 , Processed in 0.233521 second(s), 7 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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