Excel:从开始日期和结束日期生成月份数据
我想用excel来跟踪这些年来我每个月有多少实习生 名称 分部 开始日期 结束日期 安娜 A组 2019年6月12日 2020年1月1日 本 B组 2020年10月20日 2020年12月20日 克莱尔 C组 2021年1月13日 2022年1月13日 我正在努力:Excel:从开始日期和结束日期生成月份数据,excel,Excel,我想用excel来跟踪这些年来我每个月有多少实习生 名称 分部 开始日期 结束日期 安娜 A组 2019年6月12日 2020年1月1日 本 B组 2020年10月20日 2020年12月20日 克莱尔 C组 2021年1月13日 2022年1月13日 我正在努力: Option Explicit Option Base 1 Private Const NONSENSE As String = "Nonsense" Public Sub F() Dim I As Date
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
Private Const jussing As String=“jussing”
公共分包F()
日期为
像线一样变暗
Dim SwitchMonth作为布尔值
将M作为整数
日期为D1
日期为D2
D1=“10.10.1990”
D2=“01.12.1991”
如果(D1>D2),则
MsgBox(“失败!”)
出口接头
如果结束
M=月份(D1)
MsgBox(月(米,年(D1)))
SwitchMonth=False
对于I=D1至D2,步骤1
如果我是一个月那么
M=月份(I)
MsgBox(月(米,年(一)))
如果结束
下一个
端接头
公共函数OnMonth(ByVal M作为整数,ByVal Y作为整数)作为字符串
如果(M<1)或(M>12),则
OnMonth=无意义
如果结束
选择案例M
案例1
OnMonth=“一月”
案例2
OnMonth=“二月”
案例3
OnMonth=“三月”
案例4
OnMonth=“四月”
案例5
OnMonth=“五月”
案例6
OnMonth=“六月”
案例7
OnMonth=“七月”
案例8
OnMonth=“八月”
案例9
OnMonth=“九月”
案例10
OnMonth=“十月”
案例11
OnMonth=“11月”
案例12
OnMonth=“十二月”
结束选择
OnMonth=OnMonth&&&Y
端函数
此代码假定原始数据位于表1
中,从A1
开始,并将结果放入H:J列中
所有这些都可以根据需要进行调整
子对象()
Dim arrDataIn作为变体
作为变量输出
将idxRow设置为长
暗淡的碳纳米管
Dim IDX月长为
带有板材(“板材1”)。范围(“A1”)。当前区域
arrDataIn=.Offset(1).Resize(.Rows.Count-1)
ReDim arrDataOut(1到3,1到Application.Max(.Columns(4))-Application.Min(.Columns(3)))
以
对于idxRow=LBound(arrDataIn,1)到UBound(arrDataIn,1)
对于idxMonth=0到DateDiff(“m”,arrDataIn(idxRow,3),arrDataIn(idxRow,4))
cnt=cnt+1
arrDataOut(1,cnt)=格式(DateAdd(“m”,idxMonth,arrDataIn(idxRow,3)),“mmmyyy”)
arrDataOut(2,cnt)=arrDataIn(idxRow,1)
arrDataOut(3,cnt)=arrDataIn(idxRow,2)
下一个IDX月
下一个idxRow
如果cnt>0,则
ReDim数据输出(1到3,1到cnt)
范围(“H1”)。调整大小(,3)。值=数组(“月/名”、“名”、“Div”)
Range(“H2”).Resize(cnt,3).Value=Application.Transpose(arrDataOut)
如果结束
端接头
您还可以使用Windows Excel 2010+和Office 365 Excel中提供的电源查询
获取图片输出
- 在原始表格中选择一些单元格
Data=>Get&Transform=>fromtable/Range
- 当PQ UI打开时,导航到
Home=>Advanced Editor
- 记下代码第2行中的表名
- 将现有代码替换为下面的M代码
- 将粘贴代码第2行中的表名更改为“真实”表名
- 检查所有注释,以及
应用步骤
窗口,以便更好地理解算法和步骤
还要注意#“提取月份名称”
步骤。这将创建您在结果表中看到的文本字符串。如果您希望这是一个“实际日期”,请从“应用步骤”窗口中删除最后一个步骤,并在Excel工作表中设置该列的格式,以获得所需的外观
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"
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"
let
//读入数据
//更改下一行中的表名以反映工作簿中的实际表名
Source=Excel.CurrentWorkbook(){[Name=“Table16”]}[Content],
#“更改的类型”=表.TransformColumnTypes(源、,
{{“名称”,类型text},{“分割”,类型text},{“开始日期”,类型Date},{“结束日期”,类型Date}),
//按月份创建日期列表,中间月份(开始和结束之间)=月份的第一个
#“添加的自定义”=Table.AddColumn(#“更改的类型”、“月/年”,每个
让
开始日期=#日期(日期.年([开始日期]),日期.月([开始日期]),1),
EndDate=[结束日期],
mnthList=List.Generate(
()=>开始日期,
每个uhi Norie,非常感谢!但是我有一个问题。你能为我解释一下这两行VBA代码吗:ReDim arrDataOut(1到单元格(Rows.Count,1)。End(xlUp)。Row-1,1到Application.Max(.Columns(4))-Application.Min(.Columns(3)),End With for idxRow=LBound(arrDataIn,1)到UBound(Arrdatin,1)第一行代码不在我发布的代码中。第二行代码用于在源数据行中循环。我明白了,感谢您的解释。很抱歉,我编辑了第二行代码以使其更具动态性