简体   繁体   English

根据列中的日期将行中的特定单元格复制到月工作表

[英]Copy specific cells in a row to month worksheet based on date in column

I want to copy specific columns in a row based on the date to sheets with the corresponding month attached.我想根据日期将一行中的特定列复制到附有相应月份的工作表中。 Like if column H in the Main sheet has Jan 15, 2020, I want that row to copy in the Jan-20 worksheet.就像如果主工作表中的 H 列有 2020 年 1 月 15 日,我希望该行复制到 1 月 20 日工作表中。 And only specific columns from that row, just column c,d,g, and h to copy to the corresponding b,c,d, and f in the correct month sheet.并且仅该行中的特定列,仅列 c、d、g 和 h 复制到正确月份表中相应的 b、c、d 和 f。 I have searched and from what I am getting, to solve the specific cells in the row, I need an array.我已经搜索过,从我得到的信息来看,要解决行中的特定单元格,我需要一个数组。 But I am confused about getting it to go to the different months (Jan-20-Dec-20).但我对让它去不同的月份(1 月 20 日至 12 月 20 日)感到困惑。

Thank you for your time.感谢您的时间。

Construct the tab name from the date parts and use set ws = sheets(tab name) like this从日期部分构造选项卡名称并像这样使用 set ws = sheet(tab name)

Sub copyRows()

  Const COL_DATE As String = "H"
  Const ROW1 As Long = 2

  Dim mth
  mth = Array("", "Jan", "Feb", "Mar", "Apr", "May", "Jun", _
        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")

  Dim wb As Workbook, wsMain As Worksheet, ws As Worksheet
  Dim iLast As Long, iRow As Long, iTarget As Long
  Dim dDate As Date, sTab As String

  Set wb = ThisWorkbook
  Set wsMain = wb.Sheets("Sheet1")

  iLast = wsMain.Range(COL_DATE & Rows.Count).End(xlUp).Row

  For iRow = ROW1 To iLast

      dDate = wsMain.Range(COL_DATE & iRow).Value
      sTab = mth(Month(dDate)) & "-" & Format(dDate, "yy")

      Set ws = wb.Sheets(sTab)
      iTarget = 1 + ws.Range("F" & Rows.Count).End(xlUp).Row

      'c,d,g, and h to b,c,d, and f
      With wsMain
         .Cells(iRow, "C").Copy ws.Cells(iTarget, "B")
         .Cells(iRow, "D").Copy ws.Cells(iTarget, "C")
         .Cells(iRow, "G").Copy ws.Cells(iTarget, "D")
         .Cells(iRow, "H").Copy ws.Cells(iTarget, "F")
      End With

      Debug.Print dDate, sTab
  Next
End Sub

暂无
暂无

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

相关问题 根据条件将单元格从特定列复制到另一个工作表 - Copy cells from a specific column to another worksheet based on criteria Excel/VBA - 根据日期将数据复制并粘贴到特定行中的工作表中 - Excel/VBA - Copy and paste data into a worksheet in a specific row based on date 在不同工作表的列中查找匹配项,然后将特定单元格复制粘贴到 VBA 中的匹配行 - Find match within a column in a different worksheet, then copy paste specific cells to matched row in VBA 根据不同工作表的 A 列将一个工作表的整行复制到另一个工作表 - Copy entire row of one worksheet to another based on column A of different worksheet 如果发现文本,则在另一工作表中复制同一行的特定单元格 - Copy specific cells of same row in another worksheet if text found 根据特定的单词自动将单元格复制到另一个工作表 - Automatically Copy Cells to Another Worksheet Based on Specific Words 按行将包含文本的单元格复制到另一个工作表上的列 - Copy cells by row containing text to column on another worksheet 如何基于特定列而不是可变行中单元格的值命名工作表,同时创建/复制工作表? - How can I create/copy a worksheet while naming it based on the value of a cell in a specific column but variable row? 根据工作表名称将单元格复制到其他工作表 - copy cells to different worksheet based on worksheet name 根据第一张工作表中列的非空单元格,将特定的行范围复制到另一张工作表的特定范围 - Copy specific row range to another sheet's specific range based on non empty cells of a column in first sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM