Excel VBA如何使用vlookup或替代方法获得dyanmic值

我花了上个月做一个程序,find指定的string,复制下面的数据,并从它build立一个Rawdata表。 问题是,它所做的只是复制和粘贴,所以如果值改变,Rawdata表中的值就不会。 我熟悉= vlookup函数,但我不知道如何在VBA中使用它,我甚至不知道这是否是在这种情况下正确的function。

这里是查找string和复制下面的数据的代码。

SearchString = "#Regimen" Application.FindFormat.Clear ' loop through all sheets For Each sh In ActiveWorkbook.Worksheets ' Find first instance on sheet Set cl = sh.Cells.Find(What:=SearchString, _ After:=sh.Cells(1, 1), _ LookIn:=xlValues, _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False, _ SearchFormat:=False) If Not cl Is Nothing Then ' If found, remember location FirstFound = cl.Address ' Dim RangeToCopy As Range, DestRow As Long Set RangeToCopy = Range(cl.Offset(1, 0), cl.Offset(numRowsForProducts, 0)) RangeToCopy.Copy DestRow = Sheets("Template").Range("B" & Rows.Count).End(xlUp).Row + 1 Sheets("Template").Range("B" & 3 + iCounter).PasteSpecial xlPasteValues End If Next 

TI:DR,我想从一张纸上复制一个dynamic范围,并将其放到另一张纸上,这样当原稿发生变化时,它也会在另一张纸上变化。

我不确定你想要的目标行开始。 你设置目的地行,但然后使用iCounter。 我像你一样使用了iCounter。 请让我知道,如果你想它粘贴在工作表的底部。

  SearchString = "#Regimen" Application.FindFormat.Clear ' loop through all sheets For Each sh In ActiveWorkbook.Worksheets ' Find first instance on sheet Set cl = sh.Cells.Find(What:=SearchString, _ After:=sh.Cells(1, 1), _ LookIn:=xlValues, _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False, _ SearchFormat:=False) If Not cl Is Nothing Then ' If found, remember location for i = 0 to numRowsForProducts - 1 Sheets("Template").Range("B" & 3 + iCounter + i).formula = "=" & sh.name & "!" & cl.offset(i,0).address next End If Next