【问题标题】:Getting Excel to Copy Data From One Cell to Another Depending on Date让 Excel 根据日期将数据从一个单元格复制到另一个单元格
【发布时间】:2020-03-30 13:42:18
【问题描述】:

提前道歉,因为这是我第一次在这个网站上发布东西,我不是最擅长解释问题。

我有一个电子表格,其中包含生产数据,例如每日计量表、每月计量表等。这些值是通过使用 Rockwell VantagePoint Excel 插件从 PLC 添加 TAGS 来更新的(如果您不熟悉这应该没关系这部分不是我正在努力的)

我需要在月底将数据从一个单元格复制到同一张表上的另一个单元格。基本上,每月仪表字段需要在月底复制到另一个单元格中,以记录该月运行的仪表。每月运行的计量表在月底重置为 0。

基本上我需要在那个月底将J7中的值复制到W列的相应月份中。如果它可以忽略那一年,那将是有利的,因为我不需要它来保留旧值,这意味着我只需要一列。

我在 MS-Excel 和 VBA 方面有一些经验,但主要是在 MS-Access 方面,从来没有在 MS-Excel 方面。如果答案可以尽可能简单地解释并亲自动手,将不胜感激。

在谷歌搜索问题后,我遇到了这个公式并更改了范围以适合我的工作表,但 Excel 不喜欢它说它包含错误

=QUERY( A1:B6; "select B where A =date """&TEXT(TODAY();"yyyy-mm-dd")&""" "; 0

如果我没有正确解释自己,再次抱歉。

【问题讨论】:

  • QUERY 是 Google 表格公式。
  • 嗨。你怎么知道哪个月是哪个月?比如,我怎么知道 J7 数据来自 2019 年 1 月?什么是 L1、L2、L3、...?
  • Google 表格 - 现在我觉得自己很愚蠢,哈哈,
  • Hi Mark、L1、L2 等是生产线,单元 (J3 - J6) 正在从 VantagePoint 实时标签获取数据,并在每月 1 日重置为 0。所以我需要在月底重置之前的值并将其粘贴到当前月份。

标签: excel vba excel-formula formula spreadsheet


【解决方案1】:

好的,我仍然不太确定问题是什么,而且我知道的 Access VBA 比 Excel VBA 多,但这里有一些内容可能有助于找到解决方案。

您可以创建一个返回布尔值的检查日期函数:

Public Function EoMonthCheck() As Boolean

    Dim eo_month As Date, today As Date

    eo_month = Format(WorksheetFunction.EoMonth(Now(), 0), "yyyy-MM-dd")
    today = Format(Now(), "yyyy-MM-dd")

    If today = eo_month Then
        EoMonthCheck = True
    Else
        EoMonthCheck = False
    End If


End Function

还有,要向“W”列添加值,我们可能会使用如下内容:

Public Function AppendValue(Optional target_cell As String = "J7")
''' This could be a subroutine, too, I guess, since we're not returning anything.

    Dim i As Integer

    ''' Activate whatever sheet you want to work with
    Worksheets("Sheet1").Activate

    If EoMonthCheck() = True Then
        ''' Look up the bottom of the  'W' column and find the first non-empty cell
        ''' Add 1 to that cell to get you to the next cell (the first empty one).
        i = Cells(Rows.Count, "W").End(xlUp).Row + 1

        ''' Set the value of that empty cell in the 'W' column to the value of 'J7'
        ''' which will happen after we evaluate whether it is the end of the month.
        Cells(i, "W").Value = Range(target_cell).Value

End If

然后,您可能会在每次打开工作簿时触发它。

【讨论】:

    【解决方案2】:

    如果您的工作簿不能保证在每个月底打开,我会在每次打开时更新值,例如(应该放在 ThisWorkbook 中):

    'Runs when you open the workbook
    Private Sub Workbook_Open()
        'Loops through U3 to the last used cell in that column
        For Each c In Range(Cells(3, 21), Cells(Rows.Count, 21).End(xlUp))
            'Applies the J7 value to the current month and exits the sub
            If Month(c) = Month(Now) Then c.Offset(, 2).Value = [J7]: Exit Sub
        Next c
    End Sub
    

    另外,这并不重要,但我会在 U3:U14 中应用以下公式以始终获得正确的日期:

    =EOMONTH(DATE(YEAR(TODAY()),ROW()-2,15),0)
    

    【讨论】:

      猜你喜欢
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多