找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 450|回复: 1

[转贴]:Excel Sort And Count Blocks

[复制链接]
发表于 2003-12-3 06:15:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
Description:

REQUIRED: Reference to the Excel Type Library
Connects to a running instance of Excel, CLEARS the contents of the active worksheet and then populates it with the names of all inserted block references. Once complete Excel is used to sort the sheet (removing duplicate entries) while counting the totals of each insert.
Snippet Code: - Excel Sort And Count Blocks

.:Double Click On Code To Copy:.

'Remember to add a reference to the
'Excel Object type Library!

Public Sub XLSortBlocksAndCount()
    Dim objExcel As Excel.Application
    Dim objSelSet As AcadSelectionSet
    Dim objBlkRef As AcadBlockReference
    Dim intRow As Integer
    Dim intBlkCnt As Integer
    Dim intType(0) As Integer
    Dim varData(0) As Variant
    Dim strValue As String
    On Error GoTo Err_Control
    'Excel MUST be running for this sample
    Set objExcel = GetObject(, "Excel.Application")
    'clears all cells - make sure there is nothing you
    'want in the active book
    objExcel.Cells.Select
    objExcel.Selection.ClearContents
    objExcel.Range("A1") = "Name"
    objExcel.Range("B1") = "Count"
    'Move to the first data row
    intRow = 2
    intType(0) = 0
    varData(0) = "INSERT"
    Set objSelSet = ThisDrawing.PickfirstSelectionSet
    objSelSet.Select acSelectionSetAll, filtertype:=intType, _
    filterdata:=varData
    For Each objBlkRef In objSelSet
        objExcel.Cells(intRow, 1).Value = objBlkRef.Name
        intRow = intRow + 1
    Next objBlkRef
    objExcel.ActiveSheet.Range("A1").Sort _
    key1:=objExcel.ActiveSheet.Columns("A"), _
    Header:=xlYes
    objExcel.Range("A1").Select
    objExcel.ActiveCell.Offset(1, 0).Select
    Do
    strValue = objExcel.ActiveCell.Value
    If objExcel.ActiveCell.Offset(1, 0).Value = strValue Then
        intBlkCnt = intBlkCnt + 1
        objExcel.Selection.Delete Shift:=xlUp
        objExcel.ActiveCell.Offset(-1, 0).Select
        Else
        'Add 1 for the first item the rest are compared against
        objExcel.ActiveSheet.Cells(objExcel.ActiveCell.Row, 2).Value = intBlkCnt + 1
        intBlkCnt = 0
    End If
    objExcel.ActiveCell.Offset(1, 0).Select
    Loop While strValue <> vbNullString
    Exit_Here:
    Exit Sub
    Err_Control:
    MsgBox Err.Description
    Resume Exit_Here
End Sub
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 137个

财富等级: 日进斗金

发表于 2020-1-17 19:07:41 | 显示全部楼层
学习学习学习学习学习学习学习学习学习学习
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-4-25 18:44 , Processed in 0.354621 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表