Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
12
返回列表 发新帖
楼主: 中世纪书生

[已解决]求VBA代码代替VLOOKUP或者用VBA调用VLOOKUP。

[复制链接]
发表于 2012-8-27 17:57 | 显示全部楼层
那你教我做网站不?
回复

使用道具 举报

发表于 2012-8-27 18:02 | 显示全部楼层    本楼为最佳答案   
这种表格,先跑单元格,用字典要麻烦一点,跑单元格容易点,写代码费神
  1. Sub test()
  2.     Dim LastRow, i, sht As Worksheet
  3.     Set sht = Sheets("Sheet1")
  4.     LastRow = sht.Range("A" & Rows.Count).End(xlUp).Row
  5.     With Sheets("选型表")
  6.     For i = 3 To LastRow
  7.         If sht.Cells(i, "A") = "" Then sht.Cells(i, "J") = "" Else sht.Cells(i, "J") = Application.WorksheetFunction.VLookup(sht.Cells(i, "A"), .Range("A2:D3"), 2, 0)
  8.         If sht.Cells(i, "B") = "" Then sht.Cells(i, "K") = "" Else sht.Cells(i, "K") = Application.WorksheetFunction.VLookup(sht.Cells(i, "B"), .Range("A6:C9"), 2, 0)
  9.         If sht.Cells(i, "C") = "" Then sht.Cells(i, "L") = "" Else sht.Cells(i, "L") = Application.WorksheetFunction.VLookup(sht.Cells(i, "C"), .Range("A11:D12"), 2, 0)
  10.         If sht.Cells(i, "D") = "" Then sht.Cells(i, "M") = "" Else sht.Cells(i, "M") = Application.WorksheetFunction.VLookup(sht.Cells(i, "D"), .Range("A15:B17"), 2, 0)
  11.         If sht.Cells(i, "E") = "" Then sht.Cells(i, "N") = "" Else sht.Cells(i, "N") = Application.WorksheetFunction.VLookup(sht.Cells(i, "E"), .Range("A19:D22"), 2, 0)
  12.         If sht.Cells(i, "F") = "" Then sht.Cells(i, "O") = "" Else sht.Cells(i, "O") = Application.WorksheetFunction.VLookup(sht.Cells(i, "F"), .Range("A24:D26"), 2, 0)
  13.         If sht.Cells(i, "G") = "" Then sht.Cells(i, "P") = "" Else sht.Cells(i, "P") = Application.WorksheetFunction.VLookup(sht.Cells(i, "G"), .Range("A28:D29"), 2, 0)
  14.         If sht.Cells(i, "H") = "" Then sht.Cells(i, "Q") = "" Else sht.Cells(i, "Q") = Application.WorksheetFunction.VLookup(sht.Cells(i, "H"), .Range("A31:D31"), 2, 0)
  15.     Next
  16.     End With
  17. End Sub
复制代码
回复

使用道具 举报

发表于 2012-8-27 18:34 | 显示全部楼层
  1. Sub test2()
  2. '先引用  Microsoft Scripting Runtime
  3.     Dim LastRow, i, sht As Worksheet
  4.     Dim arr, brr
  5.     Dim dic1 As New Dictionary
  6.     Dim dic2 As New Dictionary
  7.     Dim dic3 As New Dictionary
  8.     Dim dic4 As New Dictionary
  9.     Dim dic5 As New Dictionary
  10.     Dim dic6 As New Dictionary
  11.     Dim dic7 As New Dictionary
  12.     Dim dic8 As New Dictionary
  13.    
  14.     Set sht = Sheets("Sheet1")
  15.     LastRow = sht.Range("A" & Rows.Count).End(xlUp).Row
  16.     ReDim brr(1 To LastRow - 2, 1 To 8)
  17.     With Sheets("选型表")
  18.         arr = .Range("A2:D3")
  19.         For i = 1 To UBound(arr)
  20.             dic1(arr(i, 1)) = arr(i, 2)
  21.         Next
  22.         
  23.         arr = .Range("A6:C9")
  24.         For i = 1 To UBound(arr)
  25.             dic2(arr(i, 1)) = arr(i, 2)
  26.         Next
  27.         
  28.         arr = .Range("A11:D12")
  29.         For i = 1 To UBound(arr)
  30.             dic3(arr(i, 1)) = arr(i, 2)
  31.         Next
  32.         
  33.         arr = .Range("A15:B17")
  34.         For i = 1 To UBound(arr)
  35.             dic4(arr(i, 1)) = arr(i, 2)
  36.         Next
  37.         
  38.         arr = .Range("A19:D22")
  39.         For i = 1 To UBound(arr)
  40.             dic5(arr(i, 1)) = arr(i, 2)
  41.         Next
  42.         
  43.         arr = .Range("A24:D26")
  44.         For i = 1 To UBound(arr)
  45.             dic6(arr(i, 1)) = arr(i, 2)
  46.         Next
  47.         
  48.         arr = .Range("A28:D29")
  49.         For i = 1 To UBound(arr)
  50.             dic7(arr(i, 1)) = arr(i, 2)
  51.         Next
  52.         
  53.         arr = .Range("A31:D31")
  54.         For i = 1 To UBound(arr)
  55.             dic8(arr(i, 1)) = arr(i, 2)
  56.         Next
  57.     End With
  58.     arr = sht.UsedRange.Offset(2)
  59.     For i = 1 To LastRow - 2
  60.         If arr(i, 1) = "" Then brr(i, 1) = "" Else brr(i, 1) = dic1(arr(i, 1))
  61.         If arr(i, 2) = "" Then brr(i, 2) = "" Else brr(i, 2) = dic2(arr(i, 2))
  62.         If arr(i, 3) = "" Then brr(i, 3) = "" Else brr(i, 3) = dic3(arr(i, 3))
  63.         If arr(i, 4) = "" Then brr(i, 4) = "" Else brr(i, 4) = dic4(arr(i, 4))
  64.         If arr(i, 5) = "" Then brr(i, 5) = "" Else brr(i, 5) = dic5(arr(i, 5))
  65.         If arr(i, 6) = "" Then brr(i, 6) = "" Else brr(i, 6) = dic6(arr(i, 6))
  66.         If arr(i, 7) = "" Then brr(i, 7) = "" Else brr(i, 7) = dic7(arr(i, 7))
  67.         If arr(i, 8) = "" Then brr(i, 8) = "" Else brr(i, 8) = dic8(arr(i, 8))
  68.       Next
  69.    sht.Range("J3").Resize(UBound(brr), 8) = brr
  70. End Sub
复制代码

求VBA代替VLOOKUP.rar

18.6 KB, 下载次数: 133

评分

参与人数 1 +1 收起 理由
中世纪书生 + 1 赞一个!

查看全部评分

回复

使用道具 举报

发表于 2012-8-27 18:36 | 显示全部楼层
如果解决了就给个最佳吧,毕竟挺费时间的
回复

使用道具 举报

 楼主| 发表于 2012-8-27 22:26 | 显示全部楼层
5201314 发表于 2012-8-27 17:57
那你教我做网站不?

我是仪表技术支持,现在在做网站需要的专业数据,不是网站建设,开发之类的。就是说数据库里的数据,是我们专业的给弄出来,在交给开发人员弄。谢谢你。所以我不会做网站。
回复

使用道具 举报

发表于 2012-8-28 09:09 | 显示全部楼层
回复

使用道具 举报

发表于 2012-8-28 13:24 | 显示全部楼层
回复

使用道具 举报

发表于 2013-7-19 23:17 | 显示全部楼层
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-30 15:09 , Processed in 0.461509 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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