【问题标题】:EXCEL Sum values in last column under a merged header合并标题下最后一列的 EXCEL 总和值
【发布时间】:2017-03-10 18:51:27
【问题描述】:

我在下面的“图片 1”中有几列每年重复的表格,我想对名为“剩余成本”的列下的所有值求和,但基于其上方合并单元格中的月份年份值.

例如,如果我在 2017 年 2 月的单元格 A1 中输入,我想对 2017 年 2 月的合并单元格下的“剩余成本”列中的值求和,根据位于 ES11:ES105 中的值下方的图片,如果我在单元格 A1 中输入 2017 年 3 月我需要公式来求和 FA11:FA105

问题是我似乎无法找到每个月下的最后一列,也不知道月份表中的最后一行是什么。

您能否就完成我需要的最佳方式提出建议。因为我是 VBA 新手,找不到任何 Excel 公式组合来实现这一目标。

Picture1

我找到了一个代码,并对其进行了调整以获取所需列中的第一个单元格 代码是:

 Sub Select_Range()
Dim Fm As Range, Av As Range, rng As Range, rngEnd As Range
Dim s As String, x

s = "February 2017 (IN/OUT)"
Set Fm = Rows(8).Find(s, LookIn:=xlValues)

If Not Fm Is Nothing Then
    Set Av = Fm.MergeArea

    Set rng = Av.Offset(3).Resize(, Av.Columns.Count)
    Set rngEnd = rng.Cells(rng.Rows.Count, rng.Columns.Count)
    rngEnd.Select
End If

结束子

我现在只需要弄清楚如何获取最后一行编号,其余的应该很容易。

我想我很接近了

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    希望这会有所帮助。

    Sub locate_column_n_row()
        Dim nextMonth As Range
        Dim thisMonthColumn As Long
        Dim thisMonthRow As Long
    
    
        'Use Find function to locate the next month.
        'To find the desired column for February, you must search for March.
        'In your example this will return Range("ET8"):
        Set nextMonth = Rows(8).Find("March 2017", LookIn:=xlValues, lookat:=xlPart)
        'Now find the desired column of the previous month. In your example subtract by 4:
        thisMonthColumn = nextMonth.Column - 4
    
        'The following expression returns the row of the last-cell-that-has-a-value in a given column.
        thisMonthRow = Cells(Rows.Count, thisMonthColumn).End(xlUp).Row
        'I think this will locate the last row in the month table,
        'on condition that every other cell under this (in this column), doesn't have any values.
    End Sub
    

    【讨论】:

    • 谢谢你写的非常有帮助我正在调整它,我会在完成后发布我如何找到总和
    • 不客气。如果您喜欢所提供的信息,您可以接受它作为答案。
    【解决方案2】:

    我写了一个解决方案来解决这个问题,即使我的 VBA 技能很差,现在提醒只是通过参数提供 s 的值并返回 x 的值并将此代码作为普通的 excel 函数调用

     Sub Sum_Specific_Month()
    Dim FoundR As Range, Av As Range, StartR As Range, SumR As Range
    Dim s As String, x
    Dim lRow As Long
    
    s = "February 2017 (IN/OUT)"
    Set FoundR = Rows(8).Find(s, LookIn:=xlValues)
    
    If Not FoundR Is Nothing Then
        Set Av = FoundR.MergeArea
    
        Set StartR = Av.Offset(3).Resize(, Av.Columns.Count)
        Set StartR = StartR.Cells(StartR.Rows.Count, StartR.Columns.Count)
        Set SumR = Range(StartR, StartR.End(xlDown))
        x = Application.WorksheetFunction.Sum(SumR)
        Range("EN3").Value = x
    
    End If
    

    结束子

    【讨论】:

    • 为了能够帮助你,你必须先把一些事情弄清楚。您希望将 x 返回到哪里?
    • 谢谢@dimitris 我把它作为函数而不是子函数并返回x作为函数的返回值
    【解决方案3】:

    你可以做一个这样的小桌子:

    Month - Column 
    ---------------
    February - "ES"
    March - "FA"
    

    然后,您使用 VLOOKUP 根据月份获取列,并使用 INDIRECT 函数到 SUM 中设置范围

    我说清楚了吗?

    【讨论】:

    • 是的,但问题是,如果我在“数量”列和“总退出”列之间插入一列,并且这种情况经常发生,那么 ES 会变成别的东西
    • 我明白了,因此您可以使用公式或 vba 函数找到“剩余成本”列,无论它在哪里
    • Correctluckily 我解决了它并将解决方案作为 vba Sub 发布在这里但是最后为了我的目的我把它变成了一个函数
    猜你喜欢
    • 1970-01-01
    • 2012-11-02
    • 1970-01-01
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-09
    相关资源
    最近更新 更多