如何在Excel中创建自动执行宏

时间:2016-02-24 21:47:36

标签: vba imacros

我有一个电子表格,其中包含多个用户输入的数据。 我希望为用户打开电子表格时,在每个月的15日和30日将电子表格中的所有值设置为自动执行宏。 请帮忙。

1 个答案:

答案 0 :(得分:0)

这应该做你以后的事。

DT[ , grp_i := 1:.N, by = .(A, D)]
setkey(DT, grp_i)

#since the number of obs. per group varies,
#  will help to basically find out which groups still exist
#  at each index
remain <- lapply(is <- 1L:DT[ , max(grp_i)], function(n) unique(DT[.(n), .(A, D)]))

upd_cols <- c("Se", "Sc", "NQe", "NQc")
pre_cols <- c("NQe", "NQc")

setkey(DT, A, D, grp_i)
DT[.(remain[[1L]][ , grp_i := 1L]),
   (upd_cols)  := 
     .(Se <- pmin(Qe, De), Sc <- pmin(Qc, Dc),
       Qe - (del <- Se - Sc), Qc + del)]

for (ii in is[-1L]){
  rem <- remain[[ii]]
  prev <- DT[.(rem[ , grp_i := ii - 1L]), pre_cols, with = FALSE]
  DT[.(rem[ , grp_i := ii]), (upd_cols) :=
       .(Se <- pmin(TQe <- prev$NQe, De), Sc <- pmin(TQc <- prev$NQc, Dc),
         TQe - (del <- Se - Sc), TQc + del)]
}

如果有什么不清楚,请告诉我。