Excel:从开始和结束日期生成月份数据

时间:2021-05-31 15:12:10

标签: excel

我想用 excel 来跟踪这些年来我每个月有多少实习生。

<头>
姓名 部门 开始日期 结束日期
安娜 分区 A 2019 年 6 月 12 日 2020 年 1 月 1 日
Div B 2020 年 10 月 20 日 2020 年 12 月 20 日
克莱尔 Div C 2021 年 1 月 13 日 2022 年 1 月 13 日

有没有一种方法可以使用 excel 列出每个实习生工作的月数,如下所示,以便我能够将每个月有多少实习生放入图表中

<头>
月/年 姓名 分区
2019 年 6 月 安娜 分区 A
2019 年 7 月 安娜 分区 A
2019 年 8 月 安娜 分区 A
2019 年 9 月 安娜 分区 A
2019 年 10 月 安娜 分区 A
2019 年 11 月 安娜 分区 A
2019 年 12 月 安娜 分区 A
2020 年 1 月 安娜 分区 A
2020 年 10 月 Div B
2020 年 11 月 Div B
2020 年 12 月 Div B
2021 年 1 月 克莱尔 Div C
2021 年 2 月 克莱尔 Div C
2021 年 3 月 克莱尔 Div C
2021 年 4 月 克莱尔 Div C
2021 年 5 月 克莱尔 Div C
2021 年 6 月 克莱尔 Div C
2021 年 7 月 克莱尔 Div C
2021 年 8 月 克莱尔 Div C
2021 年 9 月 克莱尔 Div C
2021 年 10 月 克莱尔 Div C
2021 年 11 月 克莱尔 Div C
2021 年 12 月 克莱尔 Div C
2022 年 1 月 克莱尔 Div C

感谢任何帮助!提前致谢:)

3 个答案:

答案 0 :(得分:0)

我正在尝试:

Option Explicit
Option Base 1

Private Const NONSENSE As String = "Nonsense"

Public Sub F()
Dim I As Date
Dim S As String
Dim SwitchMonth As Boolean
Dim M As Integer
Dim D1 As Date
Dim D2 As Date

   D1 = "10.10.1990"
   D2 = "01.12.1991"

   If (D1 > D2) Then
      MsgBox ("Failure!")
      Exit Sub
   End If

   M = Month(D1)
   MsgBox (OnMonth(M, Year(D1)))
   SwitchMonth = False
   For I = D1 To D2 Step 1
      
      If Month(I) <> M Then
         M = Month(I)
         MsgBox (OnMonth(M, Year(I)))
      End If
   Next

End Sub

Public Function OnMonth(ByVal M As Integer, ByVal Y As Integer) As String

   If (M < 1) Or (M > 12) Then
      OnMonth = NONSENSE
   End If
   
   Select Case M
      Case 1
         OnMonth = "January"
      Case 2
         OnMonth = "February"
      Case 3
         OnMonth = "March"
      Case 4
         OnMonth = "April"
      Case 5
         OnMonth = "May"
      Case 6
         OnMonth = "June"
      Case 7
         OnMonth = "July"
      Case 8
         OnMonth = "August"
      Case 9
         OnMonth = "September"
      Case 10
         OnMonth = "October"
      Case 11
         OnMonth = "November"
      Case 12
         OnMonth = "December"
   End Select
   
   OnMonth = OnMonth & " " & Y

End Function

答案 1 :(得分:0)

此代码假定原始数据位于 Sheet1 上,从 A1 开始,并将结果放在 H:J 列中。

如果需要,所有这些都可以调整。


Sub ExpandThings()
Dim arrDataIn As Variant
Dim arrDataOut As Variant
Dim idxRow As Long
Dim cnt As Long
Dim idxMonth As Long

    With Sheets("Sheet1").Range("A1").CurrentRegion
        arrDataIn = .Offset(1).Resize(.Rows.Count - 1)
        ReDim arrDataOut(1 To 3, 1 To Application.Max(.Columns(4)) - Application.Min(.Columns(3)))
    End With

    For idxRow = LBound(arrDataIn, 1) To UBound(arrDataIn, 1)
        For idxMonth = 0 To DateDiff("m", arrDataIn(idxRow, 3), arrDataIn(idxRow, 4))
            cnt = cnt + 1
            arrDataOut(1, cnt) = Format(DateAdd("m", idxMonth, arrDataIn(idxRow, 3)), "mmm yyyy")
            arrDataOut(2, cnt) = arrDataIn(idxRow, 1)
            arrDataOut(3, cnt) = arrDataIn(idxRow, 2)
        Next idxMonth
    Next idxRow
    
    If cnt > 0 Then
        ReDim Preserve arrDataOut(1 To 3, 1 To cnt)
        Range("H1").Resize(, 3).Value = Array("Month/Name", "Name", "Div")
        Range("H2").Resize(cnt, 3).Value = Application.Transpose(arrDataOut)
        
    End If
    
End Sub

答案 2 :(得分:0)

您还可以使用 Power Query 获取图片输出,可在 Windows Excel 2010+ 和 Office 365 Excel 中使用

  • 选择原始表格中的某个单元格
  • Data => Get&Transform => From Table/Range
  • PQ UI 打开后,导航至 Home => Advanced Editor
  • 记下代码第 2 行中的表名称。
  • 用下面的M-Code替换现有代码
  • 将粘贴代码第 2 行中的表名更改为您的“真实”表名
  • 检查所有注释以及 Applied Steps 窗口,以更好地理解算法和步骤

还要注意 #"Extracted Month Name" 步骤。这将创建您在结果表中看到的文本字符串。如果您希望这是一个“真实日期”,请从“应用步骤”窗口中删除最后一步,并在 Excel 工作表上设置该列的格式,使其具有您想要的外观

M 代码

let

//Read in data
//change table name in next line to reflect actual table name in workbook
    Source = Excel.CurrentWorkbook(){[Name="Table16"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,
        {{"Name", type text}, {"Division", type text}, {"Start Date", type date}, {"End Date", type date}}),

//create list of dates by month with intervening months (between start and end) = first of the months
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Month/Year", each 
        let 
          StartDate = #date(Date.Year([Start Date]),Date.Month([Start Date]),1),
          EndDate = [End Date],
          mnthList = List.Generate(
                        ()=>StartDate, 
                        each _ <= EndDate, 
                        each Date.AddMonths(_, 1)),

//Replace first and last months in the list with the actual date
          replLast = List.ReplaceRange(mnthList,List.Count(mnthList)-1,1,{[End Date]}),
          replFirst = List.ReplaceRange(replLast,0,1,{[Start Date]})
        in
           replFirst),

//Remove unneeded columns and move the Month/Year column to the beginning
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Start Date", "End Date"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Month/Year", "Name", "Division"}),

//expand the month list into new rows -- one row for each month
    #"Expanded mnthList" = Table.ExpandListColumn(#"Reordered Columns", "Month/Year"),

//Extract the month name and year as a string for display
//Can omit these steps if you want actual months in the cells
//    in which case you would format them on the worksheet
    #"Extracted Month Name" = Table.TransformColumns(#"Expanded mnthList", {
      {"Month/Year", each Date.MonthName(_) & " " & Text.From(Date.Year(_)), type text}})
in
    #"Extracted Month Name"

enter image description here

相关问题