动态获取列的总和并粘贴到最后一个空白Columc中

时间:2016-09-06 08:57:10

标签: excel vba excel-vba

我有一个Excel文件,其中包含不同数量的列。每列的行数也会有所不同。我想要实现的是获取每列的总和并将总数粘贴到工作表的最后一个空白列。我希望有人可以帮我解决我的问题。

3 个答案:

答案 0 :(得分:0)

这将把所有列的总和放在下一个空列的第一行中。


Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1).Value = WorksheetFunction.Sum(ActiveSheet.UsedRange)

答案 1 :(得分:0)

您可以使用以下子项:

    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#71b6ca"
    >
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#71b6ca"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:title="DocVids"/>


        <FrameLayout
            android:id="@+id/containerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        </FrameLayout>

        <android.support.design.widget.NavigationView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/mynav"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:layout_marginTop="-24dp"
            app:menu="@menu/drawer"//Your drawer menu consisting of menu items
        />
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

代码调用,如下所示:

Sub AddSums(ws As Worksheet)
    With ws.UsedRange
        With .Offset(1, .Columns.Count).Resize(.Rows.Count - 1, 1)
            .FormulaR1C1 = "=SUM(RC1:RC[-1])"
            .Offset(.Rows.Count).Resize(1, 1).FormulaR1C1 = "=AVERAGE(R2C:R[-1]C)"
        End With
    End With
End Sub

答案 2 :(得分:0)

enter image description here

Sub GetTotals()
    Dim lastColumn As Long, y As Long
    Dim List As Object
    Set List = CreateObject("System.Collections.ArrayList")
    lastColumn = ActiveSheet.Cells.Find(What:="*", After:=Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column

    For y = 1 To lastColumn
        List.Add WorksheetFunction.Sum(Columns(y))
    Next

    Cells(1, y).Resize(List.Count) = Application.Transpose(List.ToArray)

    Cells(Rows.Count, y).End(xlUp).Offset(1) = WorksheetFunction.Sum(Columns(y))

End Sub