试试这个修改过的代码——错过了神奇的单词“Preserve”…
还为当前图形中没有块定义的情况添加了保护。
- Public Sub Lindex()
- Dim BlockItem As Integer
- Dim LastIndex As String: Dim CurrentIndex As String
- [color=red]Dim BlocksList As Variant[/color]
- [color=black]BlocksList = SelectBlocksByPattern("Block-")[/color]
- [color=red] If UBound(BlocksList) = -1 Then Exit Sub 'exit if no block available[/color]
- LastIndex = CInt(Mid(BlocksList(0), 7)) 'retain first index as reference
- For BlockItem = 1 To UBound(BlocksList)
- CurrentIndex = Mid(BlocksList(BlockItem), 7)
- If CInt(CurrentIndex) > LastIndex Then 'compare with current item's index
- LastIndex = CurrentIndex 'and retain it if bigger
- End If
- Next BlockItem
- LastIndex = LastIndex + 1 'increase maximum index
- MsgBox LastIndex
- End Sub
- Public Function SelectBlocksByPattern(ByVal NamePattern As String)
- Dim theBlock As Variant: Dim BlocksList As Variant
- Dim BlockName As String
- BlocksList = Array()
- For Each theBlock In ThisDrawing.Blocks 'parse blocks database
- BlockName = theBlock.Name 'retain block's name
- If Left(BlockName, Len(NamePattern)) = NamePattern Then 'compare with argument pattern
- ReDim [color=red]Preserve[/color] BlocksList(UBound(BlocksList) + 1) 'and retain it it match
- BlocksList(UBound(BlocksList)) = BlockName
- End If
- Next theBlock
- SelectBlocksByPattern = BlocksList 'return found blocks list
- End Function
当做 |