dianbotang 发表于 2008-11-13 08:06:00

[原创]批量改编号工具

本帖最后由 作者 于 2009-3-22 1:35:53 编辑 <br /><br /> <p>&nbsp;</p><p>&nbsp;可针对相同前后缀的文本字符串,或所有文本字符串,将其中数字同步加减某一个数,要求其中只含一组数字。</p><p>&nbsp;</p><p></p><p></p><p></p><p>Option Explicit<br/>'相同前后缀的字符串中的数字同步增加或减少()<br/>Private Sub CommandButton1_Click()<br/>&nbsp; UserForm1.hide<br/>&nbsp; <br/>&nbsp; Dim prefix As String '前缀<br/>&nbsp; prefix = TextBox1.Text<br/>&nbsp; Dim postfix As String '后缀<br/>&nbsp; postfix = TextBox2.Text<br/>&nbsp; Dim StartNumber As Double '初值<br/>&nbsp; StartNumber = Val(TextBox3.Text)<br/>&nbsp; If TextBox3.Text = "" Then StartNumber = 1<br/>&nbsp; Dim EndNumber As Double '终值<br/>&nbsp; EndNumber = Val(TextBox4.Text)<br/>&nbsp; If TextBox4.Text = "" Then EndNumber = 1000000000000#<br/>&nbsp; Dim increment As Double '增量<br/>&nbsp; increment = Val(TextBox5.Text)<br/>&nbsp; <br/>'设置选择过滤器<br/>&nbsp; Dim FilterType(1) As Integer<br/>&nbsp; Dim FilterData(1) As Variant<br/>&nbsp; FilterType(0) = 0<br/>&nbsp; FilterData(0) = "text,mtext"<br/>&nbsp; FilterType(1) = 1<br/>&nbsp; FilterData(1) = prefix &amp; "*" &amp; postfix<br/>&nbsp; <br/>'安全创建选择集<br/>&nbsp; On Error Resume Next<br/>&nbsp; Dim SSet As AcadSelectionSet<br/>&nbsp; If Not IsNull(ThisDrawing.SelectionSets.Item("Example")) Then<br/>&nbsp;&nbsp;&nbsp; Set SSet = ThisDrawing.SelectionSets.Item("Example")<br/>&nbsp;&nbsp;&nbsp; SSet.Delete<br/>&nbsp; End If<br/>&nbsp; Set SSet = ThisDrawing.SelectionSets.Add("Example")<br/>&nbsp; SSet.SelectOnScreen FilterType, FilterData</p><p>'修改编号<br/>&nbsp; Dim TextObj As AcadEntity '文本<br/>&nbsp; Dim TextStr As String '文本字符串<br/>&nbsp; Dim NumberStr As Double '文本字符串中的数字<br/>&nbsp; Dim N As Long '统计修改编号个数<br/>&nbsp; N = 0<br/>&nbsp; For Each TextObj In SSet<br/>&nbsp;&nbsp;&nbsp; TextStr = TextObj.TextString<br/>&nbsp;&nbsp;&nbsp; If NumberOfTextStr(TextStr) &lt;&gt; "" Then '只对含有数字的文本进行操作<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NumberStr = Val(NumberOfTextStr(TextStr))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If NumberStr &gt;= StartNumber And NumberStr &lt;= EndNumber Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NumberStr = NumberStr + increment<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextStr = prefix &amp; CStr(NumberStr) &amp; postfix<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextObj.TextString = TextStr<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; N = N + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp; Next<br/>&nbsp; MsgBox "共修改了" &amp; N &amp; "个编号!"<br/>&nbsp; UserForm1.Show<br/>End Sub</p><p>Private Sub CommandButton2_Click()<br/>&nbsp; TextBox1 = ""<br/>&nbsp; TextBox2 = ""<br/>&nbsp; TextBox3 = ""<br/>&nbsp; TextBox4 = ""<br/>&nbsp; TextBox5 = ""<br/>End Sub</p><p>Private Sub CommandButton3_Click()<br/>&nbsp; End<br/>End Sub</p><p>'提取字符串中的数字,其中只含有一处数字和小数点<br/>Function NumberOfTextStr(ByVal TextStr As String) As String<br/>&nbsp; Dim NumberStr As String<br/>&nbsp; NumberStr = ""<br/>&nbsp; Dim i As Long<br/>&nbsp; For i = 1 To Len(TextStr)<br/>&nbsp;&nbsp;&nbsp; If (Asc(Mid(TextStr, i, 1)) &gt;= 48 And Asc(Mid(TextStr, i, 1)) &lt;= 57) Or Asc(Mid(TextStr, i, 1)) = 46 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NumberStr = NumberStr &amp; Mid(TextStr, i, 1)<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp; Next i<br/>&nbsp; NumberOfTextStr = NumberStr<br/>End Function</p><p>Private Sub UserForm_Click()</p><p>End Sub</p><p></p><p><br/>Option Explicit<br/>'所有字符串中的数字同步增加或减少()<br/>Private Sub CommandButton1_Click()<br/>&nbsp; UserForm2.hide<br/>&nbsp; <br/>&nbsp; Dim StartNumber As Double '初值<br/>&nbsp; StartNumber = Val(TextBox1.Text)<br/>&nbsp; If TextBox1.Text = "" Then StartNumber = 1<br/>&nbsp; Dim EndNumber As Double '终值<br/>&nbsp; EndNumber = Val(TextBox2.Text)<br/>&nbsp; If TextBox2.Text = "" Then EndNumber = 1000000000000#<br/>&nbsp; Dim increment As Double '增量<br/>&nbsp; increment = Val(TextBox3.Text)<br/>&nbsp; <br/>'设置选择过滤器<br/>&nbsp; Dim FilterType(0) As Integer<br/>&nbsp; Dim FilterData(0) As Variant<br/>&nbsp; FilterType(0) = 0<br/>&nbsp; FilterData(0) = "text,mtext"<br/>&nbsp; <br/>'安全创建选择集<br/>&nbsp; On Error Resume Next<br/>&nbsp; Dim SSet As AcadSelectionSet<br/>&nbsp; If Not IsNull(ThisDrawing.SelectionSets.Item("Example")) Then<br/>&nbsp;&nbsp;&nbsp; Set SSet = ThisDrawing.SelectionSets.Item("Example")<br/>&nbsp;&nbsp;&nbsp; SSet.Delete<br/>&nbsp; End If<br/>&nbsp; Set SSet = ThisDrawing.SelectionSets.Add("Example")<br/>&nbsp; SSet.SelectOnScreen FilterType, FilterData</p><p>'修改编号<br/>&nbsp; Dim TextObj As AcadEntity '文本<br/>&nbsp; Dim TextStr As String '文本字符串<br/>&nbsp; Dim prefix As String '前缀<br/>&nbsp; Dim postfix As String '后缀<br/>&nbsp; Dim NumberStr As Double '文本字符串中的数字<br/>&nbsp; Dim N As Long '统计修改编号个数<br/>&nbsp; N = 0<br/>&nbsp; Dim i As Long<br/>&nbsp; For Each TextObj In SSet<br/>&nbsp;&nbsp;&nbsp; TextStr = TextObj.TextString<br/>&nbsp;&nbsp;&nbsp; prefix = ""<br/>&nbsp;&nbsp;&nbsp; postfix = ""<br/>&nbsp;&nbsp;&nbsp; If NumberOfTextStr(TextStr) &lt;&gt; "" Then '只对含有数字的文本进行操作<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For i = 1 To Len(TextStr) '取得数字的前缀<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If (Asc(Mid(TextStr, i, 1)) &gt;= 48 And Asc(Mid(TextStr, i, 1)) &lt;= 57) Or Asc(Mid(TextStr, i, 1)) = 46 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit For<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prefix = prefix &amp; Mid(TextStr, i, 1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next i<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For i = Len(TextStr) To 1 Step -1 '取得数字的后缀<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If (Asc(Mid(TextStr, i, 1)) &gt;= 48 And Asc(Mid(TextStr, i, 1)) &lt;= 57) Or Asc(Mid(TextStr, i, 1)) = 46 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit For<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; postfix = Mid(TextStr, i, 1) &amp; postfix<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next i<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NumberStr = Val(NumberOfTextStr(TextStr))<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If NumberStr &gt;= StartNumber And NumberStr &lt;= EndNumber Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NumberStr = NumberStr + increment<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextStr = prefix &amp; CStr(NumberStr) &amp; postfix<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextObj.TextString = TextStr<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; N = N + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp; Next<br/>&nbsp; MsgBox "共修改了" &amp; N &amp; "个编号!"<br/>&nbsp; UserForm2.Show<br/>End Sub</p><p>Private Sub CommandButton2_Click()<br/>&nbsp; End<br/>End Sub</p><p>'提取字符串中的数字,其中只含有一处数字和小数点<br/>Function NumberOfTextStr(ByVal TextStr As String) As String<br/>&nbsp; Dim NumberStr As String<br/>&nbsp; NumberStr = ""<br/>&nbsp; Dim i As Double<br/>&nbsp; For i = 1 To Len(TextStr)<br/>&nbsp;&nbsp;&nbsp; If (Asc(Mid(TextStr, i, 1)) &gt;= 48 And Asc(Mid(TextStr, i, 1)) &lt;= 57) Or Asc(Mid(TextStr, i, 1)) = 46 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NumberStr = NumberStr &amp; Mid(TextStr, i, 1)<br/>&nbsp;&nbsp;&nbsp; End If<br/>&nbsp; Next i<br/>&nbsp; NumberOfTextStr = NumberStr<br/>End Function</p><p>Private Sub UserForm_Click()</p><p>End Sub</p><p></p><p>&nbsp;</p><p></p><p></p><p></p>

kunlongmann 发表于 2008-11-19 01:04:00

怎么沒有辦法看到圖片呢

兰州人 发表于 2008-11-28 15:20:00

<p>关注这个帖子</p>

3wlds 发表于 2008-12-7 00:39:00

关注   

lqyqyy 发表于 2009-12-21 13:24:00

怎么用啊????

znyan 发表于 2010-1-15 23:26:00

用正则表达式提取数字比较轻松,也不在乎你有多少个数字。

blues_眼语 发表于 2013-6-25 14:11:23

命令是什么怎么用啊

wwswwswws 发表于 2013-8-23 11:33:25

在论坛里多泡泡,网上收收就知道怎么用了

清风明月名字 发表于 2013-8-25 14:47:43

谢谢楼主代码分享!很少看到有人发VBA代码

zhangcan0515 发表于 2020-3-14 23:04:12

谢谢老师分享的内容
页: [1] 2
查看完整版本: [原创]批量改编号工具