Excel VBA how vlookup或获取动态值的替代方法
我花了上个月的时间制作了一个程序,可以找到指定的字符串,复制其下的数据,并从中构建一个Rawdata表。问题是,它所做的只是复制和粘贴,所以如果该值发生更改,Rawdata表中的值将不会更改。我熟悉=vlookup函数,但我不知道如何在VBA中使用它,我甚至不确定在本例中使用的函数是否正确 下面是查找字符串并复制其下数据的代码Excel VBA how vlookup或获取动态值的替代方法,vba,excel,copy,vlookup,Vba,Excel,Copy,Vlookup,我花了上个月的时间制作了一个程序,可以找到指定的字符串,复制其下的数据,并从中构建一个Rawdata表。问题是,它所做的只是复制和粘贴,所以如果该值发生更改,Rawdata表中的值将不会更改。我熟悉=vlookup函数,但我不知道如何在VBA中使用它,我甚至不确定在本例中使用的函数是否正确 下面是查找字符串并复制其下数据的代码 SearchString = "#Regimen" Application.FindFormat.Clear ' loop through all she
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
Tl:DR,我想从一张纸上复制一个动态范围,然后把它放到另一张纸上,这样当原始的改变时,它也会在另一张纸上改变 我不确定您希望目标行从何处开始。您可以设置目标行,但可以使用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
您知道cl的地址,因此不必复制值,只需使目标单元格的公式等于偏移量值的地址即可。通过在DestRow中的列中循环,或者使用FormulaR1C1函数来实现这一点,该函数根据目标单元格的位置定义公式。