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