使用VBA – 根据特定值插入VLOOKUP
我试图使用VLOOKUP函数从另一个文件中检索数据,但这只是根据是否有任何3项数据出现在列8(H)
OLY
OLY – QUO
OLY – PRO
我有以下,知道这是不正确的
Sub BlockAllocationsVlookupAll() Dim x As Long For x = 1 To 65536 If InStr(1, Sheet1.Range("$H$" & x), "OLY") > 0 Then Sheet1.Range("$I$" & x) = Sheet1.Range("$I$" & x) & "sometext" End If Next End Sub
我知道上面的不完全是我所需要的任何人都可以帮助到需要编辑以包括下面的Vlookup
=VLOOKUP(A21,'[001 - Allocations - Blocks.xls]CurrentDayAll'!$1:$65536,9,FALSE)
另一个问题是VLOOKUP首先指向的单元格也会因为报表长度的不同而发生变化
感谢您给予的任何帮助
UPD:
从评论如下,
-
H
列在Allocations.xls工作簿中 - 有一套标准
- 公式应该放在单元格中,只要
H
列中的相应单元格符合任何标准。
工作代码:
Sub BlockAllocationsVlookupAll() Dim x As Long Dim lastrow As Long Dim searchCriterias As String Dim wb As Workbook Dim ws As Worksheet 'specify correct path to your workbook Set wb = Workbooks.Open("C:\Allocations.xls") 'If workbook is already opened use next line 'Set wb = Workbooks("Allocations.xls") Set ws = wb.Worksheets("Current Day") searchCriterias = "|OLY|SVC|SVC-PRO|SVC-QUO|EUR|EUR-PRO|EUR-QUO|" With ws lastrow = .Cells(.Rows.Count, "H").End(xlUp).Row For x = 4 To lastrow If InStr(1, searchCriterias, "|" & .Range("H" & x) & "|") > 0 Then .Range("I" & x).Formula = "=VLOOKUP(A" & x & ",'[001 - Allocations - Blocks.xls]CurrentDayAll'!$A:$I,9,FALSE)" End If Next End With 'Comment next line if you don't want to close wb wb.Close (True) Set wb = Nothing End Sub