【问题标题】:sum of range amounts realated to the beginning of the month and today date与月初和今天日期相关的范围金额的总和
【发布时间】:2014-01-16 08:16:35
【问题描述】:

我写了这段代码,但它给出了一个错误。你能帮我纠正一下吗?我在excel中有两列:一列显示日期(每天,一个接一个),因此列中的最后一个日期是“昨天”,第二列显示相关金额。我需要通过 VBA 编写一个公式,计算从月初到最后一行(最后一个日期,即“昨天”)的这些金额的总和。正如您可能理解的那样,月初每个月都在变化,最后日期每天都在变化。 - 提前非常感谢您!

Dim x As Long
x = Month(Workbooks("Reports.xlsm").Worksheets("MACRO").Cells(2, 3))
Range("F64").Formula = "=Sum(" & Range(Cells(Worksheets("A").Cells(Rows.Count, 4).End(xlUp).Row, 4), Cells(Worksheets("A").Cells(Rows.Count, 4).End(xlUp).Row - x, 4))

【问题讨论】:

    标签: excel vba date


    【解决方案1】:

    昨天是Day(Now-1)。所以:

    x = Day(Now-1)-1
    

    【讨论】:

      【解决方案2】:

      这是一种查找当月第一天和昨天的行的方法。

      Sub FindYesterdayAnd1stDayofMonth()
      
      ' Turn Off screenupdating and calc to speed up code
      Application.Calculation = xlCalculationManual
      Application.ScreenUpdating = False
      
      Dim wS As Worksheet
      Set wS = ActiveSheet
      
      Dim rFound As Range
      Dim rS%, rE%
      
      Dim M As Integer, Y As Integer
      
      
      ' Find 1st day of current month
      M = Month(Now - 1)
      Y = Year(Now - 1)
      
      ' Look in column A. Using a Range object to do some error checking when it doesn't work
      Set rFound = wS.Columns(1).Find(What:=DateSerial(Y, M, 1), LookIn:=xlValues)
      
      If rFound Is Nothing Then
          MsgBox "Date '" & DateSerial(Y, M, 1) & "' not found"
      Else
          rS = rFound.Row
      End If
      
      
      
      ' Find yesterday
      Set rFound = wS.Columns(1).Find(What:=Date - 1, LookIn:=xlValues)
      
      If rFound Is Nothing Then
          MsgBox "Date '" & Date - 1 & "' not found"
      Else
          rE = rFound.Row
      End If
      
      
      ' Turn updating and calc back on
      Application.Calculation = xlCalculationAutomatic
      Application.ScreenUpdating = True
      
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多