简体   繁体   English

VBA Excel脚本从一个工作表复制包含数据的行并粘贴到另一个工作表中而不会覆盖

[英]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.

相关问题 Excel-VBA将数据从一个工作表复制到另一工作表并粘贴到新行 - Excel-VBA Copy data from one worksheet to another worksheet and paste in new row Excel VBA,将数据从一个工作表复制并粘贴到另一个工作表,然后删除复制数据源 - Excel VBA, Copy and paste data from one worksheet to another then delete copy data source 将行从一个行复制并粘贴到另一个工作表VBA - Copy and paste rows from one to another worksheet VBA Excel:将多个行和列从一个工作表复制并粘贴到另一个工作表 - Excel : Copy and paste multiple rows and columns from one worksheet to another Excel VBA-通过循环将数据从一个工作表复制到另一个工作表 - Excel VBA - Copy data from one worksheet to another via loop Excel使用VBA将数据从一张纸复制到工作表上的另一张 - Excel copy data from one sheet to another on worksheet refresh with vba VBA代码可从一个工作表复制数据并将其粘贴到另一工作表的最后一行下方 - VBA code to copy data from one worksheet & paste below last row of another worksheet 如何有条件地将数据行从一个Excel工作表复制到另一个 - How to conditionally copy the data rows from one Excel Worksheet to another Excel VBA。 从多个工作簿复制数据并粘贴到一个工作簿的同一工作表中 - Excel VBA. Copy data from multiple workbooks and paste in one workbook same worksheet Excel VBA将数据从一个工作表复制到另一个工作表,而不会在另一工作表上进行用户输入 - Excel VBA copy data from one worksheet to another off of user input on another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM