将3个单元格从发票复制到销售日志表

时间:2015-11-14 21:02:51

标签: excel vba

我有一个当前的宏代码来保存和创建我的发票副本并更改inv#,TY GOOGLE!

我想为此添加一个额外的功能,这样当我更改inv#,将副本保存到文件夹时,它还会将数据从发票工作表的3个单元格复制到我在同一工作簿中创建的SalesLog工作表< / p>

所以我想从发票单

复制这3个单元格中的数据
J4=Date  
J5=Invoice Number 
K33=Amount

到SalesLog表

A1=Date  
A2=Invoice Number 
A3=Amount

由于我将在某个时刻收到1000张发票,我需要每个新的复制数据不要删除之前的数据

以下是我要添加到

的模块的当前代码
Sub NextInvoice()
    Range("J5").Value = Range("J5").Value + 1
    Range("A9").MergeArea.ClearContents
End Sub

Sub SaveInvWithNewName()
    Dim NewFN As Variant
    ' Copy Invoice to a new workbook
    ActiveSheet.Copy
    NewFN = "C:\Users\Mike\Desktop\Invoices\" & Range("J5").Value & ".xlsx"
    ActiveWorkbook.SaveAs NewFN, FileFormat:=xlOpenXMLWorkbook
    ActiveWorkbook.Close
    NextInvoice
End Sub

2 个答案:

答案 0 :(得分:1)

只需使用指定RangeRange表达式的Worksheet.Range()属性:

Sub SaveToSalesLog()    
    WorkSheets("SalesLogSheet").Range("A1")  = WorkSheets("InvoiceSheet").Range("J4") 
    WorkSheets("SalesLogSheet").Range("A2")  = WorkSheets("InvoiceSheet").Range("J5")
    WorkSheets("SalesLogSheet").Range("A3")  = WorkSheets("InvoiceSheet").Range("K33")
End Sub

答案 1 :(得分:1)

已发布的其他变体:

[Range] [Range] (仅限值)

Sub SaveToSalesLog()    
    With Sheets("SalesLogSheet")
        .[A1]  = Sheets("InvoiceSheet").[J4] 
        .[A2]  = Sheets("InvoiceSheet").[J5]
        .[A3]  = Sheets("InvoiceSheet").[K33]
    End with
End Sub

Array [Range] (仅限值)

Sub test2()
    Dim I As Worksheet: Set I = Sheets("InvoiceSheet")
    Dim S As Worksheet: Set S = Sheets("SalesLogSheet")
    Dim cl As Range, x%: x = 0
    For Each cl In S.[A1:A3]
        cl.Value = Array(I.[J4], I.[J5], I.[K33])(x): x = x + 1
    Next
End Sub

[Range].copy [Range] (包括单元格格式)

Sub test3()
    With Sheets("InvoiceSheet")
        .[J4].Copy Worksheets("SalesLogSheet").[A1]
        .[J5].Copy Worksheets("SalesLogSheet").[A2]
        .[K33].Copy Worksheets("SalesLogSheet").[A3]
    End With
End Sub

[Range].copy [Range] (仅限值)

Sub test4()
    With Sheets("InvoiceSheet")
        .[J4].Copy: Worksheets("SalesLogSheet").[A1].PasteSpecial xlValues
        .[J5].Copy: Worksheets("SalesLogSheet").[A2].PasteSpecial xlValues
        .[K33].Copy: Worksheets("SalesLogSheet").[A3].PasteSpecial xlValues
    End With
End Sub