【问题标题】:VBA - Find a column with a specific header and find sum of all the rows in that columnVBA - 查找具有特定标题的列并查找该列中所有行的总和
【发布时间】:2013-12-06 00:10:12
【问题描述】:

我有一张大床单。我必须在该工作表中将多个过滤器设置为动态位置的列标题。设置过滤器后,我必须在工作表中找到具有列标题“Nov”的特定列,然后获取该列中值的总和并将该特定总和值导入到不同的工作表中。 我已经编写了代码,直到可以将过滤器设置为多列的部分,但是我发现很难找到列标题并添加该列。下面是我目前写的代码。

Sub Button2_Click()
Dim colName As Long
Dim colName1 As Long
Dim colName2 As Long
Dim r As Long

SearchV = Range("A8:DD8").Find(What:="Nov", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False).Column

lastrow = Cells(Rows.Count, SearchV).End(xlUp).Row

colName = Range("A8:DD8").Find(What:="Teams", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False).Column

colName1 = Range("A8:DD8").Find(What:="Items", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False).Column

colName2 = Range("A8:DD8").Find(What:="Domain", LookIn:=xlValues, LookAt:=xlWhole, _
MatchCase:=False, SearchFormat:=False).Column

ActiveSheet.Range("$A$8:$DD$9999").AutoFilter Field:=colName, Criteria1:="ST Test",     Operator:=xlOr, Criteria2:=""
ActiveSheet.Range("$A$8:$DD$9999").AutoFilter Field:=colName1, Criteria1:="Variance", Operator:=xlOr, Criteria2:="(Blanks)"
ActiveSheet.Range("$A$8:$DD$9999").AutoFilter Field:=colName2, Criteria1:="9S", Operator:=xlOr, Criteria2:="(Blanks)"

列标题始终从第 8 行开始。上面的行中存在一些无用的信息。所以我想要的是,假设“Nov”列在 H 行。总和应从 H9 计算到最后一行的末尾。当列在“H”列时,我使用了这个公式。

Cells(lastrow + 1, colName3).Formula = "=SUBTOTAL(9,H9:H" & lastrow & ")"

但是 'Nov' 列并不总是出现在 'H' 行中,所以我无法弄清楚如何更改我的代码以动态选择列。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    确保您完全限定您的对象,并检查.Find 是否返回某些内容。这是一个例子。

    假设您的工作表如下所示

    现在试试这段代码

    Option Explicit
    
    Sub Sample()
        Dim ws As Worksheet
        Dim aCell As Range, Rng As Range
        Dim col As Long, lRow As Long
        Dim colName As String
    
        '~~> Change this to the relevant sheet
        Set ws = ThisWorkbook.Sheets("Sheet1")
    
        With ws
            Set aCell = .Range("A8:DD8").Find(What:="Nov", LookIn:=xlValues, LookAt:=xlWhole, _
                        MatchCase:=False, SearchFormat:=False)
    
            '~~> If Found
            If Not aCell Is Nothing Then
                col = aCell.Column
                colName = Split(.Cells(, col).Address, "$")(1)
    
                lRow = .Range(colName & .Rows.Count).End(xlUp).Row
    
                '~~> This is your range
                Set Rng = .Range(colName & "8:" & colName & lRow)
    
                Debug.Print Rng.Address
            '~~> If not found
            Else
                MsgBox "Nov Not Found"
            End If
        End With
    End Sub
    

    输出

    【讨论】:

    • 非常感谢您的帮助悉达多。我设法完成了大部分工作。但实际的问题是设法添加行的总和。我现在正在使用此代码。 SumV = SV & "9:" SumW = SV & lRow Cells(lRow + 1, SV).Formula = "=SUBTOTAL(9,SumV:SumW)" 但这是在 Excel 中返​​回以下值 "=SUBTOTAL(9,SumV:SumW)" 我得到了#NAME?错误。
    • 对不起,我不清楚。无论您在代码中提到的 colName 是什么,我都将其重命名为 SV,因为我已经在其他地方使用了 colName 变量。所以基本上,我想要做的是连接列和行以获得某个值(例如 W9)。所以我得到了 SumV 的 W9 和 SumW 的 W2128 的值。但是当我使用 Subtotal 函数时,会插入相同的值而不是实际值。我也尝试过像这样使用它们。 =SUBTOTAL(9," & SumV & ":" & SumW & ")。但即使这样也会引发异常。
    • 你必须像rng.Formula = "=SUBTOTAL(9," & SumV & ":" & SumW & ")"一样使用它
    • 我就是这样使用的。 Cells(lRow + 1, SV).Formula = "=SUBTOTAL(9," & SumV & ":" & SumW & ")" 。我收到应用程序定义或对象定义错误。我已经定义了 lRow & SV。 SumV 和 SumW 作为字符串。这有关系吗?
    • 你能给我看一张插入的截图吗?您可以将图片上传到 www.wikisend.com?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 2019-05-12
    • 2021-08-06
    • 1970-01-01
    • 2016-04-03
    • 2020-03-30
    相关资源
    最近更新 更多