excel macro vba - 填充数据的代码是什么?

时间:2014-09-26 06:23:59

标签: excel vba excel-vba

我是vba excel宏的新手。通常在excel中,我将双击单元格,然后数据将自动生成,直到最后一行。但在vba如何制作呢?以下是我的代码。目前我正在选择BF2到vlookup。在我获得vlookup的价值之后。我想填充数据直到最后一行。通常以excel方式,我只需双击单元格然后就会填充。那么我们如何在vba宏中做呢?建议请。

Set myValues = Application.InputBox("Please select on the CON2:", 
Default:=Range("BE2").Address(0, 0), Type:=8)
Set myResults = Application.InputBox("Please select previous CON2 to VLOOKUP:", Type:=8)

On Error Resume Next

Set myValues = myValues.Offset
FirstRow = myValues.Row
FinalRow = Cells(65536, myResults.Column).End(xlUp).Row

Range("BF2").Formula = _
"=VLOOKUP(" & Cells(FirstRow, myValues.Column).Address(False, False) & ", " & _
"  " & myResults.Address(External:=True) & " , 1, 0)"

2 个答案:

答案 0 :(得分:1)

在VBA中你会做这样的事情:

Sub AutoFill()
    Dim sRng As Range, fRng As Range

    Set sRng = Worksheets("Sheet1").Range("B2")      '<~~ Range to AutoFill from
    Set fRng = Worksheets("Sheet1").Range("B2:B10")  '<~~ Range to AutoFill

    sRng.AutoFill Destination:=fRng
End Sub

根据单元格AutoFill的内容,此代码将B2:B10范围B2

答案 1 :(得分:0)

我认为这就是你想要的。

Sub AutoFill()
    Dim sRng As Range, fRng As Range

    FirstRow = myValues.Row
    FinalRow = Cells(65536, myResults.Column).End(xlUp).Row

    Range("BF2").Formula = _
    "=VLOOKUP(" & Cells(FirstRow, myValues.Column).Address(False, False) & ", " & _
    "  " & myResults.Address(External:=True) & " , 1, 0)"

    Set sRng = Worksheets("Sheet1").Range("BF2")
    Set fRng = Worksheets("Sheet1").Range("BF2:B" & FinalRow) 

    sRng.AutoFill Destination:=fRng
End Sub