简体   繁体   English

将数据传输到特定的 Excel 表

[英]Transfer Data to specific Excel Sheet

I am trying to transfer the data to specific sheet at last row but not able to data transferring but after some empty cells in sheet b c de help needed to solve data is transferring in each sheet but not at next empty cell:我正在尝试将数据传输到最后一行的特定工作表,但无法进行数据传输,但是在工作表 b c 中的一些空单元格之后,解决数据所需的帮助正在每个工作表中传输,但不能在下一个空单元格中传输:

 Sub transferData()
      Application.ScreenUpdating = False
      'Dim myData As Worksheet, ItemA As Worksheet, ItemB As Worksheet, ItemC As Worksheet, ItemD As 
       Worksheet, ItemE As Worksheet
      Dim ItemName As String
      Dim price As Long, qty As Long

      Dim r1 As Long, r2 As Long, erow As Long, lastrow As Long
      r1 = 2
      r2 = 2
      Sheets(Array("Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6")).Select
      Sheets("Sheet2").Activate
      Cells.Select
      Selection.ClearContents
      Sheets(Array("Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6")).Select
      Sheets("Sheet2").Activate
      range("A1").Select
      ActiveCell.Value = "Item"
      range("B1").Select
      ActiveCell.Value = "Price"
      range("C1").Select
      ActiveCell.Value = "Quantity"

       MYDATA.Activate

      Do While Cells(r2, 1) <> ""
      ItemName = Cells(r2, 1).Value
      price = Cells(r2, 2).Value

      qty = Cells(r2, 3).Value

       p = Worksheets.Count
       For q = 1 To p
       If ActiveWorkbook.Worksheets(q).CodeName = UCase(ItemName) Then
       Worksheets(q).Activate
       erow = Worksheets(q).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
       Cells(r2, 1).Value = ItemName
      Cells(r2, 2).Value = price
       Cells(r2, 3).Value = qty
       r2 = r2 + 1


        enter code here

       End If
       Next q
       MYDATA.Activate
        Loop
       Application.ScreenUpdating = True
    End Sub

A few comment:几点评论:

  1. Adding some indentation will make your code more understandable添加一些缩进将使您的代码更易于理解
  2. Avoid using Range().Select then ActiveCell.Value=xxx , it will be very slow.避免使用Range().Select然后ActiveCell.Value=xxx ,它会很慢。 Simply use Range().Value = xxx .只需使用Range().Value = xxx This applies to other entities such as worksheets, cells, etc.这适用于其他实体,例如工作表、单元格等。
  3. Within your For...Next loop, r2 should be replaced by erow in order to copy your value to the other worksheets.在您的For...Next循环中,应将r2替换为erow以便将您的值复制到其他工作表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA 将单元格数据传输到另一个工作表中的特定部分或范围 - Excel VBA Transfer cell data to specific parts or ranges in another sheet 如何将特定数据从一张 excel 表传输到 MS Excel 中的另一张表? - How to transfer specific data from one excel sheet to another in MS Excel? 使用excel宏求和特定列的数据并将数据传输到sheet2 - using excel macro to sum data of specific column and transfer data to sheet2 如何自动转移另一个Excel工作表中的特定字段? - How to automatically transfer specific fields in another excel sheet? 将数据从R传输到XLSX文件的特定工作表和特定范围 - Transfer data from R to a specific sheet and a specific range of an XLSX file Excel 如何将数据从一张纸传输到另一张纸 - Excel how to transfer data from one sheet to another sheets 将 Excel 附件、Outlook 或 Gmail 中的数据传输到 Google 表格 - Transfer data in Excel attachment, in Outlook or Gmail, to Google sheet Excel VBA:如果找不到,如何将数据传输到另一个工作表? - Excel VBA: How to transfer data to another sheet if not found? 根据唯一 ID 匹配,将数据传输到外部 excel 数据库表 - Transfer data into an external excel database sheet, based on unique ID match 如何将数据从一张 Excel 输入表传输到 Excel 数据库表? (系统设计) - How to transfer data from one Excel input sheet to Excel database sheet? (system design)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM