Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使列数组成为动态的_Arrays_Excel_Vba - Fatal编程技术网

Arrays 使列数组成为动态的

Arrays 使列数组成为动态的,arrays,excel,vba,Arrays,Excel,Vba,我有以下代码,可以根据行的动态范围在特定列的底部运行求和公式。我的限制是,我必须定义我希望发生这种情况的列。如何根据最后一列中的数据使其成为动态的 谢谢 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 <>

我有以下代码,可以根据行的动态范围在特定列的底部运行求和公式。我的限制是,我必须定义我希望发生这种情况的列。如何根据最后一列中的数据使其成为动态的

谢谢

 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

如果您的范围是连续的,您可以找到第一列和最后一列,如下所示:

只需调整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
比如:

忽略行或列不足的图纸

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

你能举个例子吗?在上面的哪一列中,您会使用最后一列,并且您希望colArray保存所有列(包括这一列)吗?我有一个表,它是B列到X列的数量,其中数据是第9行到X行的数量。我想在L列的最后一行下面,通过该列中直到第9行的所有数据的可变列数,得到一个求和公式。柱可以在z处结束,也可以在AZ处结束。现在我必须准确地定义要在数组中求和的列。我希望它能动态变化。它是excel表格吗?例如,一个实际的表格,如果你将公式放在一列中,它会自动填充?是的,只是excel中的数据。但是我需要穿过一行,而不是向下一列,并且您打算从第9行向下求和?我通常使用ws.columns.count.endxltoleft/up.column来查找最后一列和最后一行。有没有一种方法可以使用它将其放入数组中,使其类似于colsArray=ArrayL LastColumn如果列L=column 12,为什么不将列号存储在i=12到LastColumn中?我如何实现它?版本2正是我所需要的。非常感谢你的帮助。你不知道你刚才为我节省了多少时间。以防万一,如果我想更改它开始的列。我会改变什么?对这一切还是新鲜事。所以我可以把a调暗为整数,把a链接到一个单元格中,我可以把它放在数字11中,然后把现在显示为11的地方替换为a吗?是的,你可以使用一个单元格,在这种情况下,将dim startColumn声明为Long,并将它指向一个单元格。