0

我在 Excel 2010 VBA 中使用 vlookup 时遇到问题

我想遍历空白列中的每个单元格,并从数据源的相邻列中查找一个值。我想循环执行此操作。我看过很多论坛,可以让它正常工作。

这是我的代码示例

Sheets("NPT(hrs)").Activate
Dim NumberRows As Integer
NumberRows = Range("a:a").Find("*", Range("A1"), SearchDirection:=xlPrevious).Row
NumberRows = NumberRows

ActiveCell.Offset(2, 69).Activate
For RowNum = 1 To NumberRows
    If Len(Range("BQ1").Offset(RowNum, 0)) = 0 Then

        ActiveCell.Value = Application.WorksheetFunction.VLookup(Cells(RowNum, 68), "Equipment!A:K", 7, False)

        ActiveCell.Offset(1, 0).Activate
    '' This approach never workedXXX
    'v = WorksheetFunction.VLookup(Cells(RowNum, 68), "Equipment!A:K", 7, False)
           ' ActiveSheet.Cells(RowNum , 69).Formula = "=VLOOKUP(Cells(RowNum,68) ,Equipment!A:K,7,false)"
    'ActiveCell.Text = "v"


    'MsgBox "fin work?"
End If
4

1 回答 1

1

Here is a quick code block that I wrote to replicate what I think you're trying to get at.

I created some sample data. The lookup on the right has unit prices and on the left you have a table with some entries missing in the 'total price' column.

Excel screenshot of sample data

The code block below loops through the cells in column C and runs a vlookup each time it finds a blank cell.

Sub FillInBlanks()

  Dim rng As Range
  Set rng = ActiveSheet.Range("C2:C21")

  For Each cell In rng
    If IsEmpty(cell) Then
        cell.FormulaR1C1 = "=VLOOKUP(RC[-2],R1C7:R11C8,2,FALSE)*RC[-1]"
    End If
  Next cell

End Sub

Running this macro should give you this

Excel output

于 2013-02-06T18:33:39.463 回答