简体   繁体   English

根据当前月份自动更改Excel单元格值

[英]Automatically change excel cell value depending on current Month

Stock analysis Dashboard. 股票分析仪表板。 I need to compare the stock from today vs the previous 6 months. 我需要比较今天和之前六个月的库存。

My stock data 我的库存数据

Cell A1:R1 - month data example Cell A1= June 2017, B1= 7/2017 ....till R1= December 2018. 单元格A1:R1-月数据示例单元格A1 = 2017年6月,B1 = 7/2017 ....直到R1 = 2018年12月。

Cell A2:R2 Stock numbers data example A2=23, B2=25,........till R2=50. 单元格A2:R2库存编号数据示例A2 = 23,B2 = 25,........直到R2 = 50。

IF I want to see the stock today month which is July 2018= M1 vs six months February 2018 H2 and the stock level calculation will which is M2=26 - H2= 30 =-4 . 如果我想查看今天的库存月(即2018年7月= M1与六个月的2018年2月H2),则库存水平计算将为M2=26 - H2= 30 =-4

But instead of entering formula every time I need to be updated every month based on the today month? 但是,不是每次都需要输入基于今天的每月更新的公式,而是每次输入公式?

Any tips on how to do it? 有关如何操作的任何提示?

Thanks 谢谢

I find the question a bit confusing. 我觉得这个问题有些令人困惑。 If A1 is June 2017, I am missing a month if R1 is December 2018 and July 2017 is in M1. 如果A1是2017年6月,那么我想念一个月,如果R1是2018年12月,而2017年7月是M1。 Also, even though you said 6 months, I am assuming that you want 5 months so that July compares to February. 另外,即使您说了6个月,我还是假设您要5个月,以便将7月与2月进行比较。 This response assumes that A1 is wrong but R1 and M1 (and B1 through R1 are accurate) and that the values in A1 through R1 are actually an Excel date that is formatted as m/yyyy (as opposed to some text representation of a date). 该响应假定A1错误,但是R1和M1(以及B1至R1是准确的),并且A1至R1中的值实际上是一个Excel日期,其格式设置为m / yyyy(与日期的某些文本表示形式相对) 。

This formula is further complicated since I do not know what day is used in the date representation in A1 through R1, hence I am adapting both that date and the current date to be a fixed day of the month (I chose the first day of the month in my formula). 由于我不知道A1到R1中的日期表示使用的是哪一天,因此该公式更加复杂,因此我将该日期和当前日期都调整为一个月的固定日期(我选择了日期的第一天)我的公式中的月份)。

Here is the formula that computes the -4 assuming M1 is 7/2018, M2 is 26, H1 is 2/2018, H2 is 30 and we want to compare column M to column H since today is in July and we want to compare July to February (5 months difference): 这是计算-4的公式,假设M1是7/2018,M2是26,H1是2/2018,H2是30,我们想将列M与列H进行比较,因为今天是7月,所以我们要比较7月到二月(相差5个月):

=INDIRECT("R2C"&MATCH(DATE(YEAR(TODAY()),MONTH(TODAY()),1),DATE(YEAR(A1:R1),MONTH(A1:R1),1),0),FALSE)-INDIRECT("R2C"&MATCH(DATE(YEAR(TODAY()),MONTH(TODAY()),1),DATE(YEAR(A1:R1),MONTH(A1:R1),1),0)-5,FALSE)

This is an array function so it needs to be entered with [Ctrl]+[Shift]+[Enter]. 这是一个数组函数,因此需要使用[Ctrl] + [Shift] + [Enter]输入。

The way it works is that it computes from today's date, the date value equal to the first day of this month. 它的工作方式是从今天的日期开始计算,日期值等于本月的第一天。 Then it compares that against the range A1:R1 with each of those dates likewise moved to the first of the month. 然后将其与范围A1:R1进行比较,每个日期同样移至该月的第一天。 This gives us the column to use for "this month" and using the same computation but subtracting 5 gives us the column to use for "six months ago." 这为我们提供了用于“本月”的列,并使用相同的计算,但减去5则为我们提供了用于“六个月前”的列。 Out of sheer laziness I used these values in an indirect cell reference to pull the Row 2 values for "this month" and "six months ago" and subtracted the latter from the former (ie, "this month" - "six months ago"). 出于纯粹的懒惰,我在间接单元格引用中使用这些值来提取“本月”和“六个月前”的第2行值,并从前者中减去后者(即“本月”-“六个月前”) )。

In other words, this part of the formula gives me the column of the current month: 换句话说,公式的这一部分为我提供了当月的列:

MATCH(DATE(YEAR(TODAY()),MONTH(TODAY()),1),DATE(YEAR(A1:R1),MONTH(A1:R1),1),0)

and this part gives me the column of the month six months ago (as defined in the original post): 这部分给了我六个月前的月份(在原始帖子中定义):

MATCH(DATE(YEAR(TODAY()),MONTH(TODAY()),1),DATE(YEAR(A1:R1),MONTH(A1:R1),1),0)-5

As I indicated, this may not be what you wanted, but this is what I thought you were asking. 正如我指出的那样,这可能不是您想要的,但这是我以为您要的。


Alternate version using INDEX function 使用INDEX函数的替代版本

=INDEX(A2:R2,MATCH(DATE(YEAR(TODAY()),MONTH(TODAY()),1),DATE(YEAR(A1:R1),MONTH(A1:R1),1),0))-INDEX(A2:R2,MATCH(DATE(YEAR(TODAY()),MONTH(TODAY()),1),DATE(YEAR(A1:R1),MONTH(A1:R1),1),0)-5)

The above would also be entered as an array formula. 上面的内容也将作为数组公式输入。

I also miss a date... 我也想念一个约会...

If you use today() function to identify today's date you can use: 如果您使用today()函数确定今天的日期,则可以使用:

=HLOOKUP(TODAY(),$A$1:$S$2,2)-HLOOKUP(DATE(YEAR(TODAY()),MONTH(TODAY())-5,1),$A$1:$S$2,2) = HLOOKUP(TODAY(),$ A $ 1:$ S $ 2,2)-HLOOKUP(DATE(YEAR(TODAY()),MONTH(TODAY())-5,1),$ A $ 1:$ S $ 2,2 )

If you need a cell with a specific date, replace today() formula with that cell. 如果需要具有特定日期的单元格,则用该单元格替换today()公式。

Excel打印屏幕

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM