Custom programming solutions, applications and training!

  VBA - Renumber Text, Mtext and Blocks with Attributes
Contract CADD Group - Phone: (604) 591-1140 Email:

Looking for CADD expertise? 
Looking for Web Solutions?   

Think Contract CADD Group!

Phone: (604) 591-1140
Toll Free: 1 866 433-2233

CORBIMITE Web Solutions
CORBIMITE Web Solutions

Renumbering Text, Mtext and Block attribute values with a VBA program

By Frank Zander

Contract CADD Group


One of the more annoying and error prone areas of repetitive work in AutoCAD is the renumbering of blocks attributes or text.  For example, the numbering of grid lines and layout numbers can be a mindless bore.  The following renumber VBA program creates a simple user interface to automate the chore of renumbering block attributes, Text and Mtext objects in a drawing.  To use the program, the starting number (or text value) is entered by the user in the textbox on the form.  The user then presses the Renumber button and selects Text, Mtext, Block (with attributes) objects in the drawing. After the user has finished selecting all the objects to renumber (in sequence one by one), the user presses the Enter key.  The renumber program processes the selected objects (in sequence as selected).  After processing the selected objects, the renumber program shows the next number in sequence just in case the user has more numbering to do -- starting with the next number.


What you will need for this program.

 AutoCAD r14.01 or AutoCAD 2000 with the VBA install option enabled (when AutoCAD was installed).  To check if the VBA editor is installed in your version of AutoCAD type VBAide at the AutoCAD command prompt.  If you receive an AutoCAD error “unknown command” you do not have VBA installed.  If you have VBA installed, the resulting window that pops up is the VBA editor.  Once the VBA editor is open, create a form with three buttons (the About button is optional) and one text box as shown in Figure 1.  For AutoCAD r14 users, check your version of AutoCAD at the AutoCAD command prompt type: ACADver.  If you do not have AutoCAD r14.01 or better installed, see your trusty AutoCAD dealer for an update to r14.01 or 2000.  The r14.01 should be a free update from your dealer.



Figure 1



Button Settings

For this project, I have set the (name) property for the first button (typically CommandButton1) to cmdReNumber, I also set the Caption property of the button to Renumber.  To have the letter r in R Renumber button underlined for the cmdReNumber button, I set the Accelerator property to R.  See Figure 3.  For the second button (optional) I have set the Caption property to About.  The about button can be customized to fit your needs.  Typically, I use the message box to display the about information for advertising etc. See figure 2


Text box settings

For the text box, set the (name) property to txtStartNumber.


Figure 2

Figure 3



Extending the Renumber program

The renumber VBA program has several limitations as written.  The program will only renumber the first attributed block text value (multiple block attributes are not supported).  The program assumes that the user wants to change the first block attribute text value.  Also, the program will not work with double letters (i.e. “AA”) but will work with any number.  The program could also use a filter to let the user select a window of objects and then sort the objects based on the X and Y coordinates of the selected objects.



Frank Zander is the owner of Contract CADD Group in Surrey BC, Canada.  Frank can be contacted via email at or visit the Contract CADD Group website at




The Code

'Force all variables to be declared.

Option Explicit


Private Sub cmdAbout_Click()

MsgBox "Programming by: Frank Zander" & Chr(13) & _

"Email:" & Chr(13) & _

"Website:", vbInformation, _

"Renumber program --  Contract CADD Group"

End Sub


Private Sub cmdReNumber_Click()

Dim varUserInput As Variant

Dim objACAD As AcadApplication

Dim objDOC As AcadDocument

Dim objNEWSS As AcadSelectionSet

Dim varPT1 As Variant

Dim intGroupCode(0 To 4) As Integer

Dim varGroupValue(0 To 4) As Variant

Dim entTypeConstant As String

Dim I As Integer

Dim attribs As Variant


' Build a selection set of group codes and values to search for: Blocks (inserts), Text, or Mtext.

intGroupCode(0) = -4

varGroupValue(0) = "<OR"

intGroupCode(1) = 0

varGroupValue(1) = "insert"

intGroupCode(2) = 0

varGroupValue(2) = "text"

intGroupCode(3) = 0

varGroupValue(3) = "mtext"

intGroupCode(4) = "-4"

varGroupValue(4) = "OR>"


' get the value in the Text box on the Renumber form.

varUserInput = txtStartNumber.Text

' Set objACAD to the current running AutoCAD using the Thisdrawing object.

Set objACAD = ThisDrawing.Application

' To access the current running AutoCAD session from a Visual Basic *.exe program use:

' Set objACAD = GetObject(, "autocad.application")

Set objDOC = objACAD.ActiveDocument

' Error handler

On Error Resume Next



Set objNEWSS = objDOC.SelectionSets.Add("VBA")

' Hide the renumber form.


' Select Blocks (with Attributes) Text and Mtext on screen


' Select text, Mtext or Blocks onscreen using the group codes and values filter.

objNEWSS.SelectOnScreen intGroupCode, varGroupValue

' Start processing every Object in the

' selected/within objNEWSS.

' If the user has not selected a suitable object (i.e. block with attribute), keep on picking!

If objNEWSS.Count = 0 Then GoTo PickOnScreeN

   For I = 0 To objNEWSS.Count - 1

      ' Check for Text or Mtext Object...

      entTypeConstant = objNEWSS.Item(I).EntityType

      If entTypeConstant = acText Or entTypeConstant = acMtext Then

         objNEWSS.Item(I).TextString = varUserInput


      End If

      ' Check to see this object in the selection set is a block.

      If entTypeConstant = acBlockReference Then

      ' Get attribute value(s) from the block.

       attribs = objNEWSS.Item(I).GetAttributes

       ' We will use only the First attribute in the block found at location Zero.

       ' attribs(0) is the first block attribute value.

       ' Note, most programs uses Zero-based counting & therefore the first number is Zero when counting rather than one.

       attribs(0).TextString = varUserInput

       ' Update the block so we can see the new Values applied to the block attribute values above.

       ' This is similar to a localized regen, only the block is updated/regenerated.


       End If

       ' Check for number or integer

       ' if ASCII character change to ASCII character code to add 1 to the ASCII code and swap back to next character

       ' set the text box to the next number increment or character

       txtStartNumber.Text = AddtoCharacter(varUserInput, 1)

       ' if check box not checked then add one to the varUserInput

       varUserInput = txtStartNumber.Text

' Process the next object in the selection set


' Destroy the selection set

If Not objNEWSS Is Nothing Then objNEWSS.Delete

' Display the renumber form


End Sub


Private Sub cmdExit_Click()

Unload Me

End Sub


Function AddtoCharacter(varUseramount As Variant, intUserAmountToADD As Integer) As Variant

' this function returns an incremented number or text value.

' For this project we are only incrementing by one.

Dim intValue As Variant

Select Case Asc(varUseramount)

   Case 65 To 89

      If Chr(Asc(varUseramount)) = varUseramount Then

         intValue = Asc(varUseramount) + intUserAmountToADD

         intValue = Chr(intValue)

   End If

   Case Else

      intValue = varUseramount + intUserAmountToADD

End Select


AddtoCharacter = intValue

End Function


Private Sub txtStartNumber_Change()

' As the user types in characters in the Textbox on the Renumber form, the text is changed to uppercase.

   txtStartNumber.Text = UCase(txtStartNumber.Text)

End Sub  

CORBIMITE Web Solutions CORBIMITE Web Solutions CORBIMITE Web Solutions
CORBIMITE Web Solutions
CORBIMITE Web Solutions

CORBIMITE Web Solutions
Website created By Frank Zander
Phone: (604) 591-1140
Copyright © 2006 
Contract CADD Group
All rights reserved.

Contract CADD Group is an Autodesk Developer Network member.

Please give us Feedback!
Send your comments and  suggestions to:
Frank Zander
Revised: May 03, 2009.

Back Back Top of Page