Excel VBA:输出不同的值和小计 [英] Excel VBA: Output Distinct Values and Subtotals

查看:200
本文介绍了Excel VBA:输出不同的值和小计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种形式的数据:

 类别来源金额
会费FTW $ 100
捐款ODP $ 20
捐款IOI $ 33
费用MMK $ 124

没有排序顺序。这些类别在编译时是未知的。我想要一个VBA宏循环遍历范围,输出不同值的列表,每个小计。对于上述数据,它将如下所示:

 类别总金额
费用$ 224
捐款$ 55

我该怎么做?另外,有没有办法让每次上面的表都更新,或者用户点击一个按钮是否需要运行?

解决方案

您可能希望使用内置的Excel功能来执行此操作。如果您有很多值循环,循环可能需要很长时间才能遇到问题。



以下内容可能会让您开始创建一个数据透视表(从 http://www.ozgrid.com/News/pivot-tables.htm

  Sub MakeTable()
Dim Pt As PivotTable
Dim strField As String

'传递一个字符串变量
strField = Selection.Cells(1,1).Text

'命名列表范围
范围(选择,选择。 End(xlDown))。Name =Items

'根据我们的命名列表范围创建数据透视表。
'TableDestination:=将强制它到一个新的工作表
ActiveWorkbook.PivotCaches.Add(SourceType:= xlDatabase,_
SourceData:== Items)CreatePivotTable TableDestination:= ,_
TableName:=ItemList

'将数据透视表变量设置为新的数据透视表
设置Pt = ActiveSheet.PivotTables(ItemList)

'将数据透视表放在新表格上从A3开始
ActiveSheet.PivotTableWizard TableDestination:= Cells(3,1)

'将列表标题移动到行字段
Pt.AddFields RowFields:= strField
'将列表标题移动到数据字段
Pt.PivotFields(strField).Orientation = xlDataField
End Sub

这是针对小计(尽管我更喜欢数据透视表)
http://msdn.microsoft.com/en-us/library/aa213577(office.11​​)的.aspx






编辑:
我重新读取并有以下想法。在单独的工作表中设置数据透视表(不使用任何代码),然后将以下代码放在具有数据透视表的工作表中,以便每次选择工作表时,表将更新。

  Private Sub Worksheet_Activate()

Dim pt As PivotTable

'将MiPivot更改为您的枢轴名称table
设置pt = ActiveSheet.PivotTables(MyPivot)

pt.RefreshTable

End Sub
pre>




编辑#2
刷新工作表上的所有数据表
http://www.ozgrid.com/VBA/pivot-table-refresh.htm

  Private Sub Worksheet_Activate()
Dim pt As PivotTable

对于ActiveSheet中的每个pt。数据透视表
pt.RefreshTable
下一页pt
End Sub

链接在h上有多个选项可以刷新工作簿/工作簿中的数据透视表。


I have data of this form:

Category      Source      Amount
Dues          FTW         $100
Donations     ODP         $20
Donations     IOI         $33
Dues          MMK         $124

There is no sort order. The categories are unknown at compile time. I want a VBA macro to cycle through the range, output a list of distinct values, with the subtotals for each. For the above data, it would look like this:

Category      Total Amount
Dues          $224
Donations     $55

How can I do this? Also, is there a way to make the macro run every time the table above is updated, or is it necessary for the user to click a button?

解决方案

You may want to use a built in Excel feature to do this. Looping can take a long time and be problematic if you have a lot of values to loop through.

Something like the following might get you started on creating a pivot table (from http://www.ozgrid.com/News/pivot-tables.htm)

Sub MakeTable()
Dim Pt As PivotTable
Dim strField As String

    'Pass heading to a String variable
    strField = Selection.Cells(1, 1).Text

    'Name the list range
    Range(Selection, Selection.End(xlDown)).Name = "Items"

    'Create the Pivot Table based off our named list range.
    'TableDestination:="" will force it onto a new sheet
    ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
        SourceData:="=Items").CreatePivotTable TableDestination:="", _
            TableName:="ItemList"

    'Set a Pivot Table variable to our new Pivot Table
    Set Pt = ActiveSheet.PivotTables("ItemList")

    'Place the Pivot Table to Start from A3 on the new sheet
    ActiveSheet.PivotTableWizard TableDestination:=Cells(3, 1)

    'Move the list heading to the Row Field
    Pt.AddFields RowFields:=strField
    'Move the list heading to the Data Field
    Pt.PivotFields(strField).Orientation = xlDataField
End Sub

This is for subtotals (though I much prefer pivot tables) http://msdn.microsoft.com/en-us/library/aa213577(office.11).aspx


EDIT: I reread and have the following thoughts. Set up the pivot table on a separate sheet (without using any code) then put the following code on the sheet that has the pivot table so that the table will update everytime the sheet is selected.

    Private Sub Worksheet_Activate()

    Dim pt As PivotTable

        'change "MiPivot" to the name of your pivot table
        Set pt = ActiveSheet.PivotTables("MyPivot")

        pt.RefreshTable

    End Sub


Edit #2 Refresh all pivot tables on sheet http://www.ozgrid.com/VBA/pivot-table-refresh.htm

Private Sub Worksheet_Activate()    
    Dim pt As PivotTable

    For Each pt In ActiveSheet.PivotTables
        pt.RefreshTable
    Next pt
End Sub

The link has multiple options on how to refresh pivot tables in the sheet/workbook.

这篇关于Excel VBA:输出不同的值和小计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆