码迷,mamicode.com
首页 > 编程语言 > 详细

20170503xlVBA房地产数据分类连接

时间:2017-07-07 00:54:13      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:sum   box   分析   pre   ima   timer   his   merge   step   

Sub NextSeven_CodeFrame4()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.Calculation = xlCalculationManual
    Application.StatusBar = ">>>>>>>>程序正在运行>>>>>>>>"


    On Error GoTo ErrHandler

    Dim StartTime, UsedTime As Variant
    StartTime = VBA.Timer
    ‘>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    Dim Wb As Workbook
    Dim Sht As Worksheet
    Dim oSht As Worksheet
    Dim Rng As Range
    Dim Arr As Variant
    Dim EndRow As Long
    Const HEAD_ROW As Long = 2
    Const SHEET_NAME As String = "具体事项"
    Const START_COLUMN As String = "A"
    Const END_COLUMN As String = "I"


    Dim Key As String
    Dim OneKey

    Dim Dic As Object
    Set Dic = CreateObject("Scripting.Dictionary")

    Dim dInfo As Object
    Set dInfo = CreateObject("Scripting.Dictionary")

    Dim dCal As Object



    ‘>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    Set Wb = Application.ThisWorkbook
    Set Sht = Wb.Worksheets(SHEET_NAME)
    With Sht
        EndRow = .Cells(.Cells.Rows.Count, "D").End(xlUp).Row
        Debug.Print EndRow
        Set Rng = .Range(.Cells(HEAD_ROW + 1, START_COLUMN), .Cells(EndRow, END_COLUMN))

        Arr = Rng.Value
        For i = LBound(Arr) To UBound(Arr)
            If Arr(i, 1) = "" Then Arr(i, 1) = Arr(i - 1, 1)
            Key = CStr(Arr(i, 5))
            Dic(Key) = Dic(Key) + 1

            Key = CStr(Arr(i, 5) & ";" & Arr(i, 1))
            dInfo(Key) = dInfo(Key) + 1

        Next i
    End With


    ‘>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    Set oSht = Wb.Worksheets("协调合作单位分析")
    With oSht
        .UsedRange.Offset(HEAD_ROW).Clear
        N = 0
        dicsum = Application.WorksheetFunction.Sum(Dic.items)
        For Each ok In Dic.Keys    ‘合作单位是OK
            N = N + 1
            .Cells(N + HEAD_ROW, "A").Value = N
            .Cells(N + HEAD_ROW, "B").Value = ok
            .Cells(N + HEAD_ROW, "C").Value = Dic(ok)
            .Cells(N + HEAD_ROW, "D").Value = Format(Dic(ok) / dicsum, "#0.00%")



            Set dCal = CreateObject("Scripting.Dictionary")

            For Each pk In dInfo.Keys
                pos = InStr(1, pk, ok)
                If pos > 0 Then
                    pos = InStr(1, pk, ";")
                    nk = Mid(pk, pos + 1)    ‘区域
                    ‘Debug.Print nk
                    ‘区域及对应数量
                    dCal(nk) = dInfo(pk)
                End If
            Next pk

            iMax = Application.WorksheetFunction.Max(dCal.items)
            info = ""

            For x = iMax To 1 Step -1
                For Each nk In dCal.Keys    ‘区域
                    If dCal(nk) = x Then
                        info = info & nk
                        info = info & x
                        info = info & ";"
                    End If
                Next nk
            Next x
            .Cells(N + HEAD_ROW, "E").Value = Left(info, Len(info) - 1)
        Next ok
        Set Rng = .Range("A65536").End(xlUp).Offset(1)
        Rng.Resize(1, 2).Merge
        Rng.Value = "汇总"

        .Range("C65536").End(xlUp).Offset(1).Value = dicsum
        .Range("D65536").End(xlUp).Offset(1).Value = "100%"
             .Range("E:E").WrapText = True
             
             SetEdges .UsedRange
    End With

    ‘>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    UsedTime = VBA.Timer - StartTime
    ‘MsgBox "本次耗时:" & Format(UsedTime, "0.000秒"), vbOKOnly, "NextSeven Excel Studio"

ErrorExit:
    Set Wb = Nothing
    Set Sht = Nothing
    Set Rng = Nothing
    Set Dic = Nothing


    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.Calculation = xlCalculationAutomatic
    Application.StatusBar = False
    Exit Sub
    ‘>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ErrHandler:
    If Err.Number <> 0 Then
        MsgBox Err.Description & "!", vbCritical, "NextSeven Excel Studio"
        ‘Debug.Print Err.Description
        Err.Clear
        Resume ErrorExit
    End If
End Sub

  

20170503xlVBA房地产数据分类连接

标签:sum   box   分析   pre   ima   timer   his   merge   step   

原文地址:http://www.cnblogs.com/nextseven/p/7129159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!