Excel,根据单元格中的日期名称查找日期

时间:2017-03-07 07:52:04

标签: excel date

我有桌子:

enter image description here

我正在尝试在特定月份的DATE单元格下方的5个单元格中查找特定日期(此处:星期一)的日期。

例如,三月星期一 - 六月六日,2017年3月13日,2017年3月20日,2017年3月27日,

我正在“玩”它约2小时,尝试week.day,mod,选择和iC可以在谷歌找到的任何东西,但没有运气。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

通过分步可以更轻松地完成这种任务。我做了example sheet。当您在黄色单元格中输入一些值时,输出(由公式计算)将以绿色单元格显示。您真正需要的是weekday功能,此外我还使用了datejoinday等。

enter image description here

当然,这可以通过VBA脚本或Apps脚本来完成,毫无疑问它可以更加优雅。我将脚本添加到上面的表格中。红色单元格由此自定义函数填充。它的逻辑与纸张流程几乎相同。

function getDaysWithNames(year, month, name) {
  name = name.slice(0,3).toUpperCase();
  var t;

  //get the first day
  var day;
  for(day=1; day<8; day++) {
    t = new Date(year, month-1, day).toDateString().slice(0,3).toUpperCase();
    if(t == name) break;
  }

  //first three days are always valid because they are <= 21
  var validDays = [day, day+7, day+14];

  //test the fourth day
  day += 21;
  t = new Date(year, month-1, day).getDate();
  if(t == day) validDays.push(day); else return validDays;

  //test the fifth day
  day += 7;
  t = new Date(year, month-1, day).getDate();
  if(t == day) validDays.push(day);

  return validDays;
}
相关问题