从另一张表中查看VLOOKUP,每隔第n行应用一个公式

时间:2016-09-13 12:54:38

标签: excel vba vlookup

我正在使用以下公式来处理来自另一张表的Vlookup数据。公式必须放在第14列,每7行,vlookuping第一列值。

Sub test3()
'Vlookuping on Column N 


Dim lastRow As Long
lastRow = Cells(Rows.Count, 14).End(xlUp).Row 'Checks last row with data

Dim cel As Range, rng As Range

Dim sheetName, lookupFrom, myRange           'variables
sheetName = "Plan2" 'the worksheet i want to get data from
lookupFrom = ActiveCell.Offset(0, -14).Address '
myRange = "'" & sheetName & "'!1:1048576"

For i = 3 To lastRow Step 7 '
    Cells(i, 14).Select      'i= first value; step= lines to jump
    ActiveCell.Formula = "=VLOOKUP(" & lookupFrom & ";" & myRange & "; 14; FALSE)"

Next i

End Sub

Example Sheet

我想将公式放在粉红色单元格(N列)上,从另一个工作表上的第一个单元格中查看粉红色值。我的实际公式甚至没有执行。

1 个答案:

答案 0 :(得分:0)

尝试以下代码,但有两个例外:

1.修改" VlookRes "到你的工作表名称 - 你想要的结果。

2.您在A列中有合并单元格(根据您上传的图片),您要合并第2行,直到第A列,这意味着单元格A3的值将为为0 。如果您希望从第三行读取值,请从第3行开始合并(对于A列中的下一个值,很快就会开始合并)。

Option Explicit

Sub test3()

'Vlookuping on Column N

Dim ShtPlan     As Worksheet
Dim ActSht      As Worksheet
Dim lastRow     As Long
Dim sheetName   As String
Dim lookupFrom  As String
Dim myRange     As String
Dim i           As Long

' modify this Sheet Name to your sheet name (where you want to keep your results)
Set ActSht = Sheets("VlookRes")
lastRow = ActSht.Cells(ActSht.Rows.Count, 14).End(xlUp).Row ' Checks last row with data

sheetName = "Plan2" 'the worksheet i want to get data from
Set ShtPlan = Sheets(sheetName)

myRange = "'" & sheetName & "'!1:1048576"

For i = 3 To lastRow Step 7
    lookupFrom = ActSht.Cells(i, 1).Address ' ActiveCell.Offset(0, -14).Address '
    Cells(i, 14).Formula = "=VLOOKUP(" & lookupFrom & "," & myRange & ", 14, FALSE)"
Next i

End Sub