返回列表 上一主題 發帖

[發問] 請問關於跨工作表vlookup的vba語法

本帖最後由 GBKEE 於 2013-9-20 11:44 編輯

回復 10# e19821223
是這樣嗎?
  1. Option Explicit
  2. Private Sub Worksheet_Change(ByVal Target As Range)
  3.     Dim data As Range
  4.     Application.EnableEvents = False
  5.     If Target.Address(0, 0) = "B1" Then Exit Sub
  6.     With Range("C13:C32")
  7.         If Not .Find(What:="#N/A", LookIn:=xlValues) Is Nothing Then .Cells = ""
  8.     End With
  9.     Set data = ['sheet2'!$A$1:B$1000]
  10.     [b1] = Application.VLookup([a1], data, 2, 0)
  11.     Application.EnableEvents = True
  12. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 11# GBKEE
  1. 非常感謝G大的教導
  2. Option Explicit
  3. Private Sub Worksheet_Change(ByVal Target As Range)
  4.     Dim data As Range
  5.    [color=Red] Dim x As Range
  6.     Dim y As Range[/color]
  7.     Application.EnableEvents = False
  8.     If Target.Address(0, 0) = "[color=Red]B1:B10[/color]" Then Exit Sub
  9.     With Range("[color=Red]B1:B10[/color]")
  10.         If Not .Find(What:="#N/A", LookIn:=xlValues) Is Nothing Then .Cells = ""
  11.     End With
  12.     Set data = ['sheet2'!$A$1:B$1000]
  13.     Y = Application.VLookup(X, data, 2, 0)
  14.     Application.EnableEvents = True
  15. End Sub
複製代碼
測試以上的語法 但 好像還是不行 正在尋思 是否哪裡有衝突

TOP

回復 11# GBKEE
我將檔案上傳了 請G大有空過目一下 謝謝

詢問.rar (13.84 KB)

TOP

回復 13# e19821223
是期望如此嗎?
  1. Option Explicit
  2. Private Sub Worksheet_Change(ByVal Target As Range)
  3.     Dim data As Range, Y As Variant
  4.     Application.EnableEvents = False
  5.     If Not Intersect(Target, [A1:A10]) Is Nothing Then
  6.        ' Set data = ['sheet2'!$A$1:B$25]
  7.         'Y = Application.VLookup(Target, data, 2, 0)
  8.         Y = Application.VLookup(Target, ['sheet2'!$A$1:B$25], 2, 0)
  9.         Target.Offset(, 1) = IIf(IsError(Y), "", Y)
  10.      End If
  11.     Application.EnableEvents = True
  12. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

哇! 是的 我就是要呈現如此 感謝G大的幫忙!
現在比較不懂的
If Not Intersect(Target, [A1:A10]) Is Nothing Then
Target.Offset(, 1) = IIf(IsError(Y), "", Y)
以上兩段的意思
不知是否能再煩請G大的解惑
抱歉 我知道我煩了點...

TOP

回復 15# e19821223
vba的說明中有範例
  1. Intersect 方法
  2. 請參閱套用至範例特定傳回 Range 物件,此物件代表兩個或多個範圍重疊的矩形範圍。

  3. IIf 函數
  4. 根據某運算式的值,來傳回兩部份中的其中一個。
  5. 語法
  6. IIf(expr, truepart, falsepart)
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

G大 非常謝謝你!
我再多爬一下文 再次感謝您提供的資訊

TOP

回復 16# GBKEE
G大..你說的"VBA 說明"是在哪裡@@ 爬了好一段時間 依然沒找到
抱歉 能否在指引一下明路 謝謝

TOP

回復 18# e19821223

感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復 19# GBKEE
呃...我是典型的捨近求遠...我一直還在論壇爬文 都沒看到類似的 原來在電腦說明裡

還有 我想問一下 若說 用相同的語法 但 放在不同的SHEET裡 參造的範圍也不一樣 是否可行呢
比方說 原本放在SHEET1裡的是G大所提供的
  1. Option Explicit
  2. Private Sub Worksheet_Change(ByVal Target As Range)
  3.     Dim data As Range, Y As Variant
  4.     Application.EnableEvents = False
  5.     If Not Intersect(Target, [A1:A10]) Is Nothing Then
  6.        ' Set data = ['sheet2'!$A$1:B$25]
  7.         'Y = Application.VLookup(Target, data, 2, 0)
  8.         Y = Application.VLookup(Target, ['sheet2'!$A$1:B$25], 2, 0)
  9.         Target.Offset(, 1) = IIf(IsError(Y), "", Y)
  10.      End If
  11.     Application.EnableEvents = True
  12. End Sub
複製代碼
但 我要再想要在SHEET3(D1:D10) 參造SHEET4 (C1:D25) 就修正成
  1. Option Explicit
  2. Private Sub Worksheet_Change(ByVal Target As Range)
  3.     Dim data As Range, Y As Variant
  4.     Application.EnableEvents = False
  5.     If Not Intersect(Target, [D1:D10]) Is Nothing Then
  6.       
  7.         Y = Application.VLookup(Target, ['sheet4'!$C$1:D$25], 2, 0)
  8.         Target.Offset(, 1) = IIf(IsError(Y), "", Y)
  9.      End If
  10.     Application.EnableEvents = True
  11. End Sub
複製代碼
但 我在測試時 在SHEET3並無動作(我是有將語法放到SHEET3裡了)
位移數也相同 要的條件也相同 只是參造的SHEET不一樣而已 卻無法執行 不知到底是為什麼

TOP

        靜思自在 : 信心、毅力、勇氣三者具備,則天下沒有做不成的事。
返回列表 上一主題