乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 22|回复: 7

[编程交流] 最大VBA数

[复制链接]

16

主题

36

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
80
发表于 2022-7-6 11:30:35 | 显示全部楼层 |阅读模式
我有很多街区都有这样的名字:block-1 block-2 block-5 block-11 block-34。。。等
 
 
我想从块名中获取数字(1,2,5,11,35…)并确定最大数+1
 
 
有人能帮我吗?
回复

使用道具 举报

35

主题

2471

帖子

2447

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
174
发表于 2022-7-6 11:40:31 | 显示全部楼层
假设块已存储在数组中,索引始终从第7个字符开始(长度无关):
 
  1. Blockslist = Array("Block-1", "Block-7", "Block-35", "Block-2", "Block-11")

 
以下代码将解决您的问题:
 
  1. Dim BlockItem As Integer
  2. Dim LastIndex As String: Dim CurrentIndex As String
  3. LastIndex = Mid(Blockslist(0), 7)                'retain first index as reference
  4. For BlockItem = 1 To UBound(Blockslist)
  5.    CurrentIndex = Mid(Blockslist(BlockItem), 7)
  6.    If CInt(CurrentIndex) > CInt(LastIndex) Then   'compare with current item's index
  7.        LastIndex = CurrentIndex                   'and retain it if bigger
  8.    End If
  9. Next BlockItem
  10. LastIndex = LastIndex + 1                          'increase maximum index

 
当做
回复

使用道具 举报

16

主题

36

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
80
发表于 2022-7-6 11:52:52 | 显示全部楼层
Thx,这很有帮助。。。
 
如何将块名添加到数组中?
回复

使用道具 举报

35

主题

2471

帖子

2447

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
174
发表于 2022-7-6 11:57:14 | 显示全部楼层
上述功能将允许您根据名称模式对当前图形中定义的所有块进行排序:
 
  1. Public Function SelectBlocksByPattern(ByVal NamePattern As String)
  2.    Dim theBlock As Variant: Dim BlocksList As Variant
  3.    Dim BlockName As String
  4.    BlocksList = Array()
  5.    For Each theBlock In ThisDrawing.Blocks                       'parse blocks database
  6.        BlockName = theBlock.Name                                 'retain block's name
  7.        If Left(BlockName, Len(NamePattern)) = NamePattern Then   'compare with argument pattern
  8.            ReDim BlocksList(UBound(BlocksList) + 1)              'and retain it it match
  9.            BlocksList(UBound(BlocksList)) = BlockName
  10.        End If
  11.    Next theBlock
  12.    SelectBlocksByPattern = BlocksList                            'return found blocks list
  13. End Function

 
使用方法如下:
 
  1. BlocksList = SelectBlocksByPattern("Block-")

 
当做
回复

使用道具 举报

16

主题

36

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
80
发表于 2022-7-6 12:12:15 | 显示全部楼层
我没有什么问题,当图形文件中只有一个块(块-1)时,当有多个块(块-1,块-2,…)时,代码工作正常然后我有一条“运行时错误”消息。
 
  1. Public Sub Lindex()
  2. Dim BlockItem As Integer
  3. Dim LastIndex As String: Dim CurrentIndex As String
  4. BlocksList = SelectBlocksByPattern("Block-")
  5. LastIndex = Mid(BlocksList(0), 7)                'retain first index as reference
  6. For BlockItem = 1 To UBound(BlocksList)
  7.    CurrentIndex = Mid(BlocksList(BlockItem), 7)
  8.    If CInt(CurrentIndex) > CInt(LastIndex) Then   'compare with current item's index
  9.        LastIndex = CurrentIndex                   'and retain it if bigger
  10.    End If
  11. Next BlockItem
  12. LastIndex = LastIndex + 1                          'increase maximum index
  13. MsgBox LastIndex
  14. End Sub
  15. Public Function SelectBlocksByPattern(ByVal NamePattern As String)
  16.    Dim theBlock As Variant: Dim BlocksList As Variant
  17.    Dim BlockName As String
  18.    BlocksList = Array()
  19.    For Each theBlock In ThisDrawing.Blocks                       'parse blocks database
  20.        BlockName = theBlock.Name                                 'retain block's name
  21.        If Left(BlockName, Len(NamePattern)) = NamePattern Then   'compare with argument pattern
  22.            ReDim BlocksList(UBound(BlocksList) + 1)              'and retain it it match
  23.            BlocksList(UBound(BlocksList)) = BlockName
  24.        End If
  25.    Next theBlock
  26.    SelectBlocksByPattern = BlocksList                            'return found blocks list
  27. End Function
回复

使用道具 举报

35

主题

2471

帖子

2447

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
174
发表于 2022-7-6 12:20:10 | 显示全部楼层
试试这个修改过的代码——错过了神奇的单词“Preserve”…
还为当前图形中没有块定义的情况添加了保护。
 
  1. Public Sub Lindex()
  2.    Dim BlockItem As Integer
  3.    Dim LastIndex As String: Dim CurrentIndex As String
  4.    [color=red]Dim BlocksList As Variant[/color]
  5.    [color=black]BlocksList = SelectBlocksByPattern("Block-")[/color]
  6. [color=red]    If UBound(BlocksList) = -1 Then Exit Sub           'exit if no block available[/color]
  7.    LastIndex = CInt(Mid(BlocksList(0), 7))            'retain first index as reference
  8.    For BlockItem = 1 To UBound(BlocksList)
  9.        CurrentIndex = Mid(BlocksList(BlockItem), 7)
  10.        If CInt(CurrentIndex) > LastIndex Then         'compare with current item's index
  11.            LastIndex = CurrentIndex                   'and retain it if bigger
  12.        End If
  13.    Next BlockItem
  14.    LastIndex = LastIndex + 1                          'increase maximum index
  15. MsgBox LastIndex
  16. End Sub
  17. Public Function SelectBlocksByPattern(ByVal NamePattern As String)
  18.    Dim theBlock As Variant: Dim BlocksList As Variant
  19.    Dim BlockName As String
  20.    BlocksList = Array()
  21.    For Each theBlock In ThisDrawing.Blocks                       'parse blocks database
  22.        BlockName = theBlock.Name                                 'retain block's name
  23.        If Left(BlockName, Len(NamePattern)) = NamePattern Then   'compare with argument pattern
  24.            ReDim [color=red]Preserve[/color] BlocksList(UBound(BlocksList) + 1)     'and retain it it match
  25.            BlocksList(UBound(BlocksList)) = BlockName
  26.        End If
  27.    Next theBlock
  28.    SelectBlocksByPattern = BlocksList                            'return found blocks list
  29. End Function

 
当做
回复

使用道具 举报

16

主题

36

帖子

20

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
80
发表于 2022-7-6 12:23:57 | 显示全部楼层
Thx,非常。。。
回复

使用道具 举报

35

主题

2471

帖子

2447

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
174
发表于 2022-7-6 12:38:53 | 显示全部楼层
不客气!
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-6 02:18 , Processed in 0.417477 second(s), 68 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表