简体   繁体   English

根据TODAY()函数和每月的第一天移动范围

[英]Moving a range based on TODAY() function and first of the month

I have a range which starts with =EDATE($A$46,-5) where A46 is =TODAY() . 我有一个以=EDATE($A$46,-5)开头的范围,其中A46是=TODAY() This range extends 5 additional cells to the right, with each cell containing =EDATE(_cell to the left_,1) and formatted to show just the month. 此范围向右扩展了5个其他单元格,每个单元格都包含=EDATE(_cell to the left_,1)并被格式化为仅显示月份。 This way, I end up with a complete range from left to right that shows the previous 6 months (ie. January, February, March, April, May, June). 这样,我最终获得了从左到右的完整范围,显示了过去6个月(即1月,2月,3月,4月,5月,6月)。 On the first of the month, the range obviously changes to include the current new month, and the prior 5 months behind it. 在该月的第一天,该范围显然会更改为包括当前的新月份以及它之后的前五个月。

In each of the cells below the months, I have data (entered manually) containing amounts for an account. 在每个月以下的每个单元中,我都有包含帐户金额的数据(手动输入)。 On the first of each month, when the workbook is opened, I need to cut the data that is in the data cells and move it back one column to account for the new additional month that is added (because the earliest month is now gone and hence so should the data be gone). 在每月的第一天,打开工作簿时,我需要剪切数据单元中的数据并将其移回一列,以说明所添加的新的额外月份(因为最早的月份现在已经过去了,并且因此数据也应该消失)。

在此处输入图片说明

So from the picture above, you can see that on July 1, I will want to move the February, March, April, May, and June data one column to the left (because January will be gone at that point). 因此,从上图可以看出,在7月1日,我想将2月,3月,4月,5月和6月的数据向左移动一列(因为此时1月将消失)。 I'd obviously need this to be dynamic so it occurs on the first of each month. 我显然需要使它动态,以便在每个月的第一天发生。

Sorry that I don't have any code here but I didn't even know where to start to do something based on the first of each month. 抱歉,我这里没有任何代码,但是我什至不知道从每个月的第一天开始在哪里做某事。 The workbook is currently macro enabled and does have a macro attached to it. 该工作簿当前启用了宏,并且确实附加了一个宏。

First you need a characteristic sign to recognize the new month. 首先,您需要一个特征符号来识别新的月份。
This could be the day's number, but the first might be on a weekend - so we need a better solution. 这可能是当天的数字,但第一个可能是在周末-因此我们需要一个更好的解决方案。

I suggest to insert the headlines not by formula, but initially manual as date (still as full date, shown as monthname only). 我建议不要按公式插入标题,而应将手册最初插入日期(仍然是完整日期,仅显示为月份名称)。 Then VBA code can compare the last cell's text with the current month's name. 然后,VBA代码可以将最后一个单元格的文本与当前月份的名称进行比较。

If you place this code in the workbook's module, it runs on every opening of the file. 如果将此代码放在工作簿的模块中,则它将在文件的每次打开时运行。 It copies the range including headlines, inserts the new date in the last headline and clears the content below it. 它将复制包括标题在内的范围,在最后一个标题中插入新日期,并清除其下方的内容。

I assumed the range shown in your image is A47 to F51. 我假设您的图片中显示的范围是A47至F51。
Please adapt it and the sheet's name to your needs. 请根据您的需要对它和工作表的名称进行修改。

Private Sub Workbook_Open()
    Dim CurrentMonthname As String
    Dim MsgAnswer As Integer

    CurrentMonthname = MonthName(Month(Date), False) ' or Format(Date, "MMMM")
    With Me.Sheets("AccountAmount")
        If .Range("F47").Text <> CurrentMonthname Then
            If MsgBox("Insert new month and move data?", _
              vbQuestion + vbOKCancel, "New Month") = vbOK Then
                .Range("A47:E51").Value = .Range("B47:F51").Value
                .Range("F47").Value = Date
                .Range("F48:F51").ClearContents
            End If
        End If
    End With
End Sub

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

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