Application of VBA in Excel to perform statistical table

  Excel received a temporary table, to perform statistical work, manual approach is cumbersome application VBA little bit familiar.

  For a long time, then it will not be unfamiliar, write a little recording for later investigation.
 1, in the definition of the structure of a module for recording data, a general structure for one line.

            '定义物料编码的结构体
        Public Type WZStruct
                 WLBM      As String           '物料编码
                 WLZ       As String           '物料组
                 WLMS      As String           '物料描述
                 DW        As String           '单位
                 WLSum2017 As Single           '计算统计的和
                 WLSum2018 As Single           '计算统计的和
                 WLSum2019 As Single           '计算统计的和
                 WL2017    As Integer          '统计2017个数
                 WL2018    As Integer          '统计2018个数
                 WL2019    As Integer          '统计2019个数
        End Type

        Public WZYQCY(6639) As WZStruct   '6639行数据

        2、常用的操作记录:

            '补全信息
                '先搜索2017表
                Sheets("2017年消耗").Activate
                For IFor = 1 To MaxFor
                                DoEvents
                                '1.取列表的物料编码
                                WZYQCY(IFor).WLBM = ListBox1.List(IFor)
                                '搜索物料编码所在的行
                                Set FindCellOK = Worksheets("2017年消耗").Columns("B").Find(what:=WZYQCY(IFor).WLBM)
                                If Not FindCellOK Is Nothing Then
                                        '2.物料组
                                        Range("A" + Trim(Str(FindCellOK.Cells.Row))).Select
                                        WZYQCY(IFor).WLZ = Selection.Formula
                                        '3.物料描述
                                        Range("C" + Trim(Str(FindCellOK.Cells.Row))).Select
                                        WZYQCY(IFor).WLMS = Selection.Formula
                                        '4.单位
                                        Range("D" + Trim(Str(FindCellOK.Cells.Row))).Select
                                        WZYQCY(IFor).DW = Selection.Formula
                                        '5.统计消耗量
                                        'WZYQCY(IFor).WLSum = Application.SumIf(Sheets("2017年消耗").Range("B2:B4386"), WZYQCY(IFor).WLBM, Sheets("2017年消耗").Range("F2:F4386"))
                                        '6.统计个数
                                        'WZYQCY(IFor).WL2017 = Application.CountIf(Sheets("2017年消耗").Range("B2:B4386"), WZYQCY(IFor).WLBM)
                                End If
                Next

                3、一般可以录制宏以后然后修改录制的编码,但是VBA里面略有不同。

'插入形成的数据行
Sheets("小修1").Activate
For IFor = 1 To MaxFor
    '插入一个新行
    Range("A" + Trim(Str(IFor + 2))).Select
    Selection.Insert Shift:=xlDown
    '更新数据
    '序号
    Range("A" + Trim(Str(IFor + 2))).Select
    Selection.Formula = Str(IFor)
    '物料组
    Range("B" + Trim(Str(IFor + 2))).Select
    Selection.Formula = WZYQCY(IFor).WLZ
    '物料编码
    Range("C" + Trim(Str(IFor + 2))).Select
    Selection.Formula = WZYQCY(IFor).WLBM
    '物料描述
    Range("D" + Trim(Str(IFor + 2))).Select
    Selection.Formula = WZYQCY(IFor).WLMS
    '单位
    Range("E" + Trim(Str(IFor + 2))).Select
    Selection.Formula = WZYQCY(IFor).DW
    '2017年数据
    Range("G" + Trim(Str(IFor + 2))).Select
    Selection.Formula = WZYQCY(IFor).WL2017
    '2018年数据
    Range("H" + Trim(Str(IFor + 2))).Select
    Selection.Formula = WZYQCY(IFor).WL2018
    '2019年数据
    Range("I" + Trim(Str(IFor + 2))).Select
    Selection.Formula = WZYQCY(IFor).WL2019
Next

Guess you like

Origin blog.51cto.com/dawn0919/2449531