将公式添加到复制范围

时间:2013-12-27 15:12:00

标签: excel vba formula

我正在尝试在发票VBA中插入Vlookup公式。 复制的范围转到“A”列,我想将我的公式插入列“B”。 这是我现在的位置..尽快需要帮助

这是我文件的链接 https://drive.google.com/file/d/0By_oZy042nKWSGRNcU14T3g4SXc/edit?usp=sharing

谢谢!

Private Sub CommandButton1_Click()
    Dim wsInvoice As Worksheet, wsRange As Worksheet

    With ThisWorkbook
        Set wsInvoice = .Worksheets("Invoice")
        Set wsRange = .Worksheets("Range")
        nr = wsInvoice.Cells(Rows.Count, 1).End(xlUp).Row + 1
    End With

    Select Case Me.ComboBox1
        Case "Paper"
            wsRange.Range("Paper").Copy wsInvoice.Cells(nr, 1)
        Case "Pen"
            wsRange.Range("Pen").Copy wsInvoice.Cells(nr, 1)
        Case "Sticker"
            wsRange.Range("Sticker").Copy wsInvoice.Cells(nr, 1)
    End Select
End Sub

3 个答案:

答案 0 :(得分:1)

现在你有了这个:

    Select Case Me.ComboBox1
        Case "Paper"
            wsRange.Range("Paper").Copy wsInvoice.Cells(nr, 1)
        Case "Pen"
            wsRange.Range("Pen").Copy wsInvoice.Cells(nr, 1)
        Case "Sticker"
            wsRange.Range("Sticker").Copy wsInvoice.Cells(nr, 1)
    End Select

要向其添加Vlookup,只需添加worksheetfunction.vlookup,如下所示:

Select Case Me.ComboBox1
    Case "Paper"
        wsRange.Range("Paper").Copy wsInvoice.Cells(nr, 1)
        wsInvoice.Cells(nr, 2) = Application.WorksheetFunction.VLookup(Cells(nr, 1), wsPrice.Range("A:B"), 2, 0)
    Case "Pen"
        wsRange.Range("Pen").Copy wsInvoice.Cells(nr, 1)
        wsInvoice.Cells(nr, 2) = Application.WorksheetFunction.VLookup(Cells(nr, 1), wsPrice.Range("A:B"), 2, 0)
    Case "Sticker"
        wsRange.Range("Sticker").Copy wsInvoice.Cells(nr, 1)
        wsInvoice.Cells(nr, 2) = Application.WorksheetFunction.VLookup(Cells(nr, 1), wsPrice.Range("A:B"), 2, 0)
End Select

我将数组命名为“paperprice”,“penprice”等......并假设价格直接位于项目右侧的列中。它使用与常规Vlookup相同的公式,因此您不应该太难以根据需要进行修改。

此外,在您设置所有工作表的位置添加此行:

Set wsPrice = .worksheet("Price")

编辑:感谢您链接到工作簿的副本。

试试这个:

Private Sub CommandButton1_Click()
Dim wsInvoice As Worksheet, wsRange As Worksheet, wsPrice As Worksheet
Dim nr As Integer, lr As Integer
With ThisWorkbook
    Set wsInvoice = .Worksheets("Invoice")
    Set wsRange = .Worksheets("Range")
    Set wsPrice = .Worksheets("Price")
End With
nr = wsInvoice.Cells(Rows.Count, 1).End(xlUp).Row + 1
Select Case Me.ComboBox1
    Case "Paper"
        wsRange.Range("Paper").Copy wsInvoice.Cells(nr, 1)
        lr = wsInvoice.Cells(Rows.Count, 1).End(xlUp).Row
        For i = nr To lr
            wsInvoice.Cells(i, 2) = Application.VLookup(Cells(i, 1), wsPrice.Range("A:B"), 2, 0)
        Next i
    Case "Pen"
        wsRange.Range("B2:B100").Copy wsInvoice.Cells(nr, 1)
        lr = wsInvoice.Cells(Rows.Count, 1).End(xlUp).Row
        For i = nr To lr
            wsInvoice.Cells(i, 2) = Application.VLookup(Cells(i, 1), wsPrice.Range("A:B"), 2, 0)
        Next i
    Case "Sticker"
        wsRange.Range("C2:c100").Copy wsInvoice.Cells(nr, 1)
        lr = wsInvoice.Cells(Rows.Count, 1).End(xlUp).Row
        For i = nr To lr
            wsInvoice.Cells(i, 2) = Application.VLookup(Cells(i, 1), wsPrice.Range("A:B"), 2, 0)
        Next i
End Select
End Sub

注意:如果您在价格表中没有#N / A错误,则会在单元格中出现#N / A错误。

答案 1 :(得分:0)

假设您刚刚将一些值复制到单元格MyCell中。然后,如果要将公式放入右侧的下一个单元格中,则应使用以下代码:

MyCell.Offset(0, 1).Formula = "=VLOOKUP(" & MyCell.Address & ",$C:$D,2,false)" 

答案 2 :(得分:0)

excelWbk.Sheets(Sheet1).Select
lrow = ActiveSheet.Range("A65536").End(xlUp).Row

excelWbk.Sheets(Sheet11).Select
lrow_sheet2 = ActiveSheet.Range("A65536").End(xlUp).Row


excelWbk.Sheets(Sheet1).Select
With excelWbk.ActiveSheet
For i = 2 To lrow

.Cells(i, 3).Formula = "=VLOOKUP(A" & i & ",Sheet11!$A$1:$B$" & lrow_sheet2 & ",2,0)"

        Next i
End With


With excelWbk.ActiveSheet
    .Range("C" & lrow).Select
Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
  End With