[英]VBA Excel Script to copy rows with data from one worksheet & paste into another with out overwritting
Excel 2007 Excel 2007
I'm trying to run a VBA script that will copy a list of sales Ledgers from one worksheet titled MSL with the columns DATE, INVOICE NO, COMPANY NAME, TOTAL, SUB-TOTAL, NETT, VAT. 我正在尝试运行一个VBA脚本,该脚本将从一个名为MSL的工作表中复制一个销售分类帐清单,其中包含日期,发票编号,公司名称,总计,小计,内含税,增值税。
1st problem i'm having is i only want to copy rows 2 and onward which contain a record and this number will change each month depending on sales. 我遇到的第一个问题是我只想复制包含记录的第二行及以后的行,并且该数字每个月都会根据销售情况而变化。
eg Jan has 30 rows Feb has 24 rows Mar has 40 rows 例如1月30行2月24行3月40行
Next i need to paste the data in to a new worksheet titled "SalesDB" with the same columns DATE, INVOICE NO, COMPANY NAME, TOTAL, SUB-TOTAL, NETT, VAT. 接下来,我需要将数据粘贴到标题为“ SalesDB”的新工作表中,该工作表具有相同的日期,发票号,公司名称,总计,小计,内含税,增值税。
Next problem im having is the data is overwriting. 下一问题是数据被覆盖。
Thanks for any help will be out for the next hour collecting kids from school 多谢您的协助,我们将在下一个小时为您送上学校的孩子。
Assuming Worksheet MLS
is like the following and Worksheet SalesDB
is same format: 假设Worksheet MLS
如下, Worksheet SalesDB
的格式相同:
A B C D E F G
1 Date Invoice No Company Name Total Sub-Total Nett VAT
2
This code will grab data from MLS
taking account of number of entries and add to SalesDB
and avoid overwriting. 此代码将考虑条目数从MLS
抓取数据,并将其添加到SalesDB
并避免覆盖。
Sub CopyPasteSales
Dim salesData as range, targetRng as range
Set salesData = Worksheets("MLS").Range("A2:G" & Range("A1").end(xlDown).Row)
If Worksheets("SalesDB").Range("A2") = vbNullString Then
Set targetRng = Worksheets("SalesDB").Range("A2") //If no data in SalesDB start in row 2
Else
Set targetRng = Worksheets("SalesDB").Range("A1").end(xlDown).Offset(1,0) //If data already in SalesDB, find next free row
End if
salesData.Copy Destination:= targetRng
End Sub
Short and dynamic: (not tested, may contain typos) 简短而动态:(未经测试,可能包含错别字)
Sub CopyData()
Dim src As Range, dest As Range
'set source, exclude first row
Set src = Worksheets("MLS").Range("A2").CurrentRegion.Offset(1, 0)
'destination is one row below last row
Set dest = Worksheets("SalesDB").Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
src.Copy Destination:=dest
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.