VLookup VBA自动填充活动列

时间:2014-12-14 19:38:07

标签: excel vba excel-vba

我是VBA的新手,我正试图想办法让这个公式根据A列中的查找值自动填充活动列,或者在activecell中返回公式,这样我就可以自动填充它了,因为在那一刻它只返回结果

Private Sub CommandButton_d_Click()
    Dim filename As String 
    Dim fullRangeString As String
    Dim returnValue As Variant 
    Dim wb As Workbook 
    Dim ws As Worksheet
    Dim rng As Range 
    Dim article As Range 
    Dim rng1 As Range 
    Dim acell As Range

    On Error Resume Next
    Set acell = ActiveCell
    Set article = ActiveSheet.Cells(ActiveCell.Row, 1)
    filename = Application.GetOpenFilename( _
      FileFilter:="Excel Files (*.xlsx), *.xls", _
      Title:="Please select a file")
    Set wb = Application.Workbooks.Open(filename)
    Set ws = wb.Worksheets("Sheet1")
    Set rng = ws.Range("a1", _
      Range("a1").End(xlDown).End(xlToRight))
    Set rng1 = ws.Range("a2", "av2")
    acell = Application.WorksheetFunction.VLookup(article, rng, _
      Application.WorksheetFunction.Match(Me.ComboBox1, rng1, 0), False)
    wb.Close False
    UserForm1.Hide
End Sub

2 个答案:

答案 0 :(得分:0)

至少应该为VLOOKUP使用 VBA工作表功能
要完成您的需要,您需要使用范围对象公式属性

尝试类似:

acell.Formula = "=VLOOKUP(" & article.Value & "," & rng.Address(, , , True) & "," & _
    Application.WorksheetFunction.Match(Me.ComboBox1, rng1, 0) & ",FALSE)"

这不包括自动填充,因为我不确定你想要做什么。 HTH虽然。

答案 1 :(得分:0)

您好我找到了解决方案。

这是最终的代码。

acell.Formula =" = VLOOKUP("& article.Address(False,False)&"," _     &安培; "' [" &安培; rng.Parent.Parent.Name& "]" &安培; rng.Parent.Name& "'!" &安培; rng.Address& "" _     &安培; Application.WorksheetFunction.Match(Me.ComboBox1,rng1,0)& "" _     &安培; "假)"