【问题标题】:Make Column Array dynamic使列数组动态化
【发布时间】:2018-04-14 02:56:34
【问题描述】:

我有以下代码可以根据行的动态范围在特定列的底部运行求和公式。我的限制是我必须定义我希望这发生在哪些列。如何根据其中包含数据的最后一列使其动态化?

谢谢

 Option Explicit

Sub Sum()

Dim WS As Worksheet

For Each WS In ThisWorkbook.Worksheets
    If WS.Name <> "Master" And WS.Name <> "How to" And WS.Name <> "Template" Then

     Dim CurCal As XlCalculation
     Dim wb As Workbook, colsLastRow As Long
     Dim cols As Variant, SumCols As Long, colsArray As Variant
     Dim biggestRow As Long
     Dim shNAMES As Range

        Application.ScreenUpdating = False
        CurCal = Application.Calculation
        Application.Calculation = xlCalculationManual

        biggestRow = 1

        Set wb = ThisWorkbook

        colsArray = Array("L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "AA", "AB", "AC", "AD", "AE", "AF", "AG", "AH", "AI", "AJ")

        For Each cols In colsArray
        colsLastRow = WS.Cells(Rows.Count, cols).End(xlUp).Row
        If colsLastRow > biggestRow Then
        biggestRow = colsLastRow + 1
        End If
        Next cols

        For Each cols In colsArray
        colsLastRow = WS.Cells(Rows.Count, cols).End(xlUp).Row
        WS.Cells(biggestRow, cols).Formula = "=SUM(" & cols & "9:" & cols & colsLastRow & ")"
        Next cols

        WS.Range("B" & biggestRow).Value = "TOTAL"

        WS.Cells(3, 3).Formula = "=LOOKUP(2,1/(N:N<>""""),N:N)"

        Application.ScreenUpdating = True
        Application.Calculation = CurCal
    End If
Next WS
End Sub

【问题讨论】:

  • 你能举个例子吗?在上面的什么地方你会使用最后一列,你是否希望 colArray 保存所有列,包括这个?
  • 我有一个表,它是 B 列到(X 列数)的第 9 行到(X 行数)行的数据。对于该列中直到第 9 行的所有数据,我想要在 L 到(可变数量的列)列的最后一行下方有一个求和公式。这些列可能在 z 处结束,也可能在 AZ 处结束。现在我必须准确定义我想要在数组中求和的列。我希望它动态改变。
  • 是excel表格吗? IE。一个实际的表格,如果你将一个公式放在一个列中,它会自动填充?
  • 是的,只是 excel 中的数据。但我需要跨越一行,而不是一列
  • 你打算从第 9 行向下求和?

标签: arrays excel vba


【解决方案1】:

类似:

忽略行或列不足的工作表

Option Explicit
Public Sub test()
    Application.ScreenUpdating = False
    Dim CurCal As Variant
    CurCal = Application.Calculation
    Application.Calculation = xlCalculationManual
    Dim i As Long, ws As Worksheet, lastColumn As Long, lastRow As Long
    Const startRow As Long = 9                   '<=====change this to sum from a different row
    Const startColumn As Long = 12               '<====change this for column to start putting totals at
    For Each ws In ThisWorkbook.Worksheets
        With ws
            On Error Resume Next
            lastColumn = GetLastColumn(ws)
            lastRow = GetLastRow(ws)
            If .Name <> "Master" And .Name <> "How to" And .Name <> "Template" Then
                For i = 1 To lastColumn - startColumn + 1
                    .Cells(lastRow, i + startColumn - 1).Offset(1, 0).Formula = "=Sum(" & .Range(.Cells(startRow, i + startColumn - 1), .Cells(lastRow, i + startColumn - 1)).Address & ")"
                Next i
                If ws.UsedRange.Rows.Count > startRow - 1 And ws.UsedRange.Columns.Count > startColumn - 1 Then
                    ws.Range("B" & lastRow + 1) = "TOTAL"
                    ws.Cells(3, 3).Formula = "=LOOKUP(2,1/(N:N<>""""),N:N)"
                End If
            End If
            On Error GoTo 0
        End With
    Next ws
    Application.ScreenUpdating = True
    Application.Calculation = CurCal
End Sub

Public Function GetLastColumn(ByVal ws As Worksheet) As Long
    If Application.WorksheetFunction.Subtotal(103, ws.UsedRange) > 0 And ws.Cells.SpecialCells(xlCellTypeLastCell).Column > 11 Then
        GetLastColumn = ws.Cells.SpecialCells(xlCellTypeLastCell).Column
    End If
End Function

Public Function GetLastRow(ByVal ws As Worksheet) As Long
    If Not Application.WorksheetFunction.Subtotal(103, ws.UsedRange) = 0 And ws.Cells.SpecialCells(xlCellTypeLastCell).Row > 8 Then
        GetLastRow = ws.Cells.SpecialCells(xlCellTypeLastCell).Row
    End If
End Function

【讨论】:

  • 第 2 版完全符合我的需要。非常感谢你的帮助。你不知道你刚刚为我节省了多少时间。
  • 以防万一我想更改它开始的列。我会改变什么?
  • 对这一切还是陌生的。那么我可以将“a”调暗为整数并将 A 链接到我可以输入数字“11”的单元格,然后将当前显示 11 的任何地方替换为“a”吗?
  • 是的,您可以使用一个单元格,在这种情况下声明 Dim startColumn As Long 并将其指向一个单元格。
【解决方案2】:

如果您的范围是连续的,您可以像这样找到第一列和最后一列:

只需调整rng 范围以适合您的表格数据范围

Option Explicit

Sub test()

Dim first_col_letter As String
Dim first_col_number As Long
Dim last_col_letter As String
Dim last_col_number As Long

Dim rng As Range
Set rng = ActiveCell.CurrentRegion

    With rng

        first_col_letter = Chr(.Columns(1).Column + 64)
        first_col_number = .Columns(1).Column

        last_col_letter = Chr(.Columns(.Columns.Count).Column + 64)
        last_col_number = .Columns(.Columns.Count).Column

    End With

End Sub

【讨论】:

  • 我通常使用 ws.columns.count) .end(xltoleft/up).column 来查找最后一列和最后一行。有没有办法使用它来放入数组中,所以它就像 colsArray = Array("L"-Lastcolumn)
  • 为什么不存储列号,如果列“L”=第 12 列,For i = 12 to LastColumn
  • 我该如何实现?
猜你喜欢
  • 1970-01-01
  • 2011-10-30
  • 2014-02-19
  • 2016-06-24
  • 1970-01-01
  • 2020-12-04
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
相关资源
最近更新 更多