明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 5790|回复: 10

[原创]批量改编号工具

[复制链接]
发表于 2008-11-13 08:06:00 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2009-3-22 1:35:53 编辑

 

 可针对相同前后缀的文本字符串,或所有文本字符串,将其中数字同步加减某一个数,要求其中只含一组数字。

 

Option Explicit
'相同前后缀的字符串中的数字同步增加或减少()
Private Sub CommandButton1_Click()
  UserForm1.hide
 
  Dim prefix As String '前缀
  prefix = TextBox1.Text
  Dim postfix As String '后缀
  postfix = TextBox2.Text
  Dim StartNumber As Double '初值
  StartNumber = Val(TextBox3.Text)
  If TextBox3.Text = "" Then StartNumber = 1
  Dim EndNumber As Double '终值
  EndNumber = Val(TextBox4.Text)
  If TextBox4.Text = "" Then EndNumber = 1000000000000#
  Dim increment As Double '增量
  increment = Val(TextBox5.Text)
 
'设置选择过滤器
  Dim FilterType(1) As Integer
  Dim FilterData(1) As Variant
  FilterType(0) = 0
  FilterData(0) = "text,mtext"
  FilterType(1) = 1
  FilterData(1) = prefix & "*" & postfix
 
'安全创建选择集
  On Error Resume Next
  Dim SSet As AcadSelectionSet
  If Not IsNull(ThisDrawing.SelectionSets.Item("Example")) Then
    Set SSet = ThisDrawing.SelectionSets.Item("Example")
    SSet.Delete
  End If
  Set SSet = ThisDrawing.SelectionSets.Add("Example")
  SSet.SelectOnScreen FilterType, FilterData

'修改编号
  Dim TextObj As AcadEntity '文本
  Dim TextStr As String '文本字符串
  Dim NumberStr As Double '文本字符串中的数字
  Dim N As Long '统计修改编号个数
  N = 0
  For Each TextObj In SSet
    TextStr = TextObj.TextString
    If NumberOfTextStr(TextStr) <> "" Then '只对含有数字的文本进行操作
      NumberStr = Val(NumberOfTextStr(TextStr))
      If NumberStr >= StartNumber And NumberStr <= EndNumber Then
        NumberStr = NumberStr + increment
        TextStr = prefix & CStr(NumberStr) & postfix
        TextObj.TextString = TextStr
        N = N + 1
      End If
    End If
  Next
  MsgBox "共修改了" & N & "个编号!"
  UserForm1.Show
End Sub

Private Sub CommandButton2_Click()
  TextBox1 = ""
  TextBox2 = ""
  TextBox3 = ""
  TextBox4 = ""
  TextBox5 = ""
End Sub

Private Sub CommandButton3_Click()
  End
End Sub

'提取字符串中的数字,其中只含有一处数字和小数点
Function NumberOfTextStr(ByVal TextStr As String) As String
  Dim NumberStr As String
  NumberStr = ""
  Dim i As Long
  For i = 1 To Len(TextStr)
    If (Asc(Mid(TextStr, i, 1)) >= 48 And Asc(Mid(TextStr, i, 1)) <= 57) Or Asc(Mid(TextStr, i, 1)) = 46 Then
      NumberStr = NumberStr & Mid(TextStr, i, 1)
    End If
  Next i
  NumberOfTextStr = NumberStr
End Function

Private Sub UserForm_Click()

End Sub


Option Explicit
'所有字符串中的数字同步增加或减少()
Private Sub CommandButton1_Click()
  UserForm2.hide
 
  Dim StartNumber As Double '初值
  StartNumber = Val(TextBox1.Text)
  If TextBox1.Text = "" Then StartNumber = 1
  Dim EndNumber As Double '终值
  EndNumber = Val(TextBox2.Text)
  If TextBox2.Text = "" Then EndNumber = 1000000000000#
  Dim increment As Double '增量
  increment = Val(TextBox3.Text)
 
'设置选择过滤器
  Dim FilterType(0) As Integer
  Dim FilterData(0) As Variant
  FilterType(0) = 0
  FilterData(0) = "text,mtext"
 
'安全创建选择集
  On Error Resume Next
  Dim SSet As AcadSelectionSet
  If Not IsNull(ThisDrawing.SelectionSets.Item("Example")) Then
    Set SSet = ThisDrawing.SelectionSets.Item("Example")
    SSet.Delete
  End If
  Set SSet = ThisDrawing.SelectionSets.Add("Example")
  SSet.SelectOnScreen FilterType, FilterData

'修改编号
  Dim TextObj As AcadEntity '文本
  Dim TextStr As String '文本字符串
  Dim prefix As String '前缀
  Dim postfix As String '后缀
  Dim NumberStr As Double '文本字符串中的数字
  Dim N As Long '统计修改编号个数
  N = 0
  Dim i As Long
  For Each TextObj In SSet
    TextStr = TextObj.TextString
    prefix = ""
    postfix = ""
    If NumberOfTextStr(TextStr) <> "" Then '只对含有数字的文本进行操作
      For i = 1 To Len(TextStr) '取得数字的前缀
        If (Asc(Mid(TextStr, i, 1)) >= 48 And Asc(Mid(TextStr, i, 1)) <= 57) Or Asc(Mid(TextStr, i, 1)) = 46 Then
          Exit For
        Else
          prefix = prefix & Mid(TextStr, i, 1)
        End If
      Next i
      For i = Len(TextStr) To 1 Step -1 '取得数字的后缀
        If (Asc(Mid(TextStr, i, 1)) >= 48 And Asc(Mid(TextStr, i, 1)) <= 57) Or Asc(Mid(TextStr, i, 1)) = 46 Then
          Exit For
        Else
          postfix = Mid(TextStr, i, 1) & postfix
        End If
      Next i
      NumberStr = Val(NumberOfTextStr(TextStr))
      If NumberStr >= StartNumber And NumberStr <= EndNumber Then
        NumberStr = NumberStr + increment
        TextStr = prefix & CStr(NumberStr) & postfix
        TextObj.TextString = TextStr
        N = N + 1
      End If
    End If
  Next
  MsgBox "共修改了" & N & "个编号!"
  UserForm2.Show
End Sub

Private Sub CommandButton2_Click()
  End
End Sub

'提取字符串中的数字,其中只含有一处数字和小数点
Function NumberOfTextStr(ByVal TextStr As String) As String
  Dim NumberStr As String
  NumberStr = ""
  Dim i As Double
  For i = 1 To Len(TextStr)
    If (Asc(Mid(TextStr, i, 1)) >= 48 And Asc(Mid(TextStr, i, 1)) <= 57) Or Asc(Mid(TextStr, i, 1)) = 46 Then
      NumberStr = NumberStr & Mid(TextStr, i, 1)
    End If
  Next i
  NumberOfTextStr = NumberStr
End Function

Private Sub UserForm_Click()

End Sub

 

本帖子中包含更多资源

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

x
发表于 2008-11-19 01:04:00 | 显示全部楼层
怎么沒有辦法看到圖片呢
发表于 2008-11-28 15:20:00 | 显示全部楼层

关注这个帖子

发表于 2008-12-7 00:39:00 | 显示全部楼层
关注   
发表于 2009-12-21 13:24:00 | 显示全部楼层
怎么用啊????
发表于 2010-1-15 23:26:00 | 显示全部楼层
用正则表达式提取数字比较轻松,也不在乎你有多少个数字。
发表于 2013-6-25 14:11:23 | 显示全部楼层
命令是什么怎么用啊
发表于 2013-8-23 11:33:25 | 显示全部楼层
在论坛里多泡泡,网上收收就知道怎么用了
发表于 2013-8-25 14:47:43 | 显示全部楼层
谢谢楼主代码分享!很少看到有人发VBA代码
发表于 2020-3-14 23:04:12 | 显示全部楼层
谢谢老师分享的内容
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 03:23 , Processed in 0.166113 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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