【问题标题】:Using variable as column reference to autosum said column VBA使用变量作为自动求和列 VBA 的列引用
【发布时间】:2020-01-09 03:18:40
【问题描述】:

我真的是 VBA 的新手,并且一直在逐节编写一些代码来格式化工作表(我一直在逐段进行,以便我了解每个代码的工作原理,并使用 final宏将所有宏调用到一个漫长的过程中)。

问题有时是我使用的工作表没有按月以相同的顺序导出列(超出我的控制),因此要对特定列进行自动求和,我必须找到列标题,然后自动求和该列,但这使得列字母(或数字)完全可变。我知道如何将行用作变量,但我被困在列上。我一直在搜索论坛以尝试找到简明的解释,但无济于事。

此代码确实适用于 Y 列,但我试图弄清楚如何让它使用列的变量。 例如,我正在使用一个名为“FindInvoiceColumn”的单独宏来选择包含字符串“invoice_amount”的列中的第一个单元格,然后我想使用我在下面写的内容将“ColumnAddress”设置为列该单元格的值。据我所知.Column 返回列号,这很好,但我假设我必须使用 Cells() 而不是 Range(),我只是不知道如何到达这里。

(部分代码还显示在包含自动求和值的单元格左侧添加单词“Total”,并将两者都设为粗体)。

这是我目前所拥有的:

Dim Rng As Range
   Dim c As Range

   Set Rng = Range("Y" & rows.Count).End(xlUp).Offset(1, 0)
   Set c = Range("Y1").End(xlDown).Offset(1, 0)
   c.Formula = "=SUM(" & Rng.Address(False, False) & ")"

   'Selects next empty row of column X to add "Total" label for sum of column Y'
    Range("X" & Cells.rows.Count).End(xlUp).Offset(1, 0).Select
   ActiveCell.FormulaR1C1 = "Total"

   'Bolds Total and the Sum of invoices'
   Range("X" & Cells.rows.Count).End(xlUp).Select
   Selection.Font.Bold = True
   Range("Y" & Cells.rows.Count).End(xlUp).Select
   Selection.Font.Bold = True```




'The below is what I'd like to use to find the dynamic value of the column.'

'Finds cell in row 1 that contains column header "invoice_amount" and selects it'
 Call FindInvoiceColumn

'Dim ColumnAddress As Integer
 ColumnAddress = ActiveCell.Column

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您可以使用.Address 获取列引用,例如:

    Sub test()
        Dim varCol As String
        varCol = Columns(ActiveCell.Column).Address
        Debug.Print varCol 'OUTPUTS $A:$A when I had cells(1,1) selected
    End Sub
    

    在上面的示例中,我选择了一个单元格来 A) 通过 .Column 找到它的列引用,并且 B) 找到所述列的 .address


    您还可以使用.cells() 表示法而不是.range() 表示法在定义的范围内执行求和。

    Sub test2()
        Dim rng As Range
        Set rng = Range(Cells(1, 1), Cells(2, 1))
        Cells(3, 1).Formula = "=sum(" & rng.Address & ")"
    End Sub
    

    以上代码输出:


    具体到使用.cells() 表示法,你可以让你的列引用一个变量,例如:

    dim r as long, c as long
    r = 1
    c = 4
    debug.print cells(r,c).address `should output $D$1 (untested)
    

    您可以选择rc 以满足您的需求。


    和往常一样...avoid select/activate where possible!!!



    编辑

    通过代码添加对最后一行的使用,因为 cmets 很糟糕:

    dim col as long
    col = 25 'Y
    With sheets("name")
        dim lastRow as long
        lastRow = .cells(.rows.count,col).end(xlup).row
        Dim rng As Range
        Set rng = .Range(.Cells(1, 1), .Cells(lastRow, col))    
    end with
    

    这正是我在该部分之后提到关于符号的细节的原因(使用rc 作为变量)。

    【讨论】:

    • 谢谢。我将无法使用第二种方法,因为行数也因工作表而异。既然 varCol 是 Dim as String,这会返回列地址的字母形式吗?如果是这样,我将如何实现Set Rng = Range("Y" & rows.Count).End(xlUp).Offset(1, 0) Set c = Range("Y1").End(xlDown).Offset(1, 0) c.Formula = "=SUM(" & Rng.Address(False, False) & ")"
    • @Kaz 我认为您可以对 Cells 使用第二种方法。对于每个工作表/列/等,您将确定最后一行 (lastRow)。 lastRow = .Cells(.Rows.Count, col).End(xlUp).Row 其中col 是您的变量列。然后,您可以使用 Cells(lastRow,c) 作为范围的结尾,例如示例 2。
    • 啊好吧!谢谢你的澄清。抱歉,VBA 的新手和一般的任何类型的代码。我只是一个试图让自己的生活更轻松的 EA!
    • 我们都从某个地方开始,@Kaz。如果这解决了您的问题,请告诉我们(用帖子左侧的绿色复选标记标记为答案);否则,让我们知道还有什么未解决的!
    • 感谢您的知识!
    【解决方案2】:

    如果您的标题位于可变位置,我已使用此代码设置列号

    Dim F As Object
    ColumnAddress = 0
    With ActiveSheet.Rows(1)
            Set F = .Find(What:="invoice_amount", LookAt:=xlWhole)
            If F Is Nothing Then
                MsgBox "This is not a proper file"
                ' other code
            Else 
                ColumnAddress = F.Column
            End If
    End With
    

    然后您将使用 Cells() 代替 range 来进一步处理 ColumnAddress 的结果。此外,为了准确起见,ColumnAddress 应该变暗为 Long。

    【讨论】:

    • 感谢您提供这段代码!但是,每当我到达这部分代码时,是否需要将 Rng 更改为 Range 以外的其他内容才能使用 Cells()? (现在的意思是变量 Dim Rng 作为 Range - 它应该是别的东西吗?)否则我在设置 Rng = 第一个空行的行上不断收到“运行时错误'1004':应用程序定义的或对象定义的错误”的列。 Set Rng = Range("Y" & rows.Count).End(xlUp).Offset(1, 0) Set c = Range("Y1").End(xlDown).Offset(1, 0) c.Formula = "=SUM(" & Rng.Address(False, False) & ")"
    • Set Rng = Cells(Rows.Count, ColumnAddress).End(xlUp).Offset(1, 0) Set c = Cells(1, ColumnAddress).End(xlDown).Offset(1, 0) 你会希望在你引用 X 的任何地方(即左边一列)成为单元格(行,列地址-1)
    • ... 但是按照编码,Rng 和 c 不返回相同的东西吗?您是否想让 c 成为一系列单元格,例如 Y2:Y9999 或其他内容,因为您需要进一步编辑才能做到这一点。
    猜你喜欢
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    相关资源
    最近更新 更多