Automate AutoCAD with Excel VBA: How to Draw a Rectangle
Are you looking to streamline your drafting process? This tutorial demonstrates how to send commands from Microsoft Excel to AutoCAD using VBA. Below is a simple script that reads dimensions from your Excel sheet and automatically draws a polyline rectangle in AutoCAD.
This is a perfect starting point for structural engineers or surveyors looking to automate beam sections, plot boundaries, or layout drafting.
The VBA Code
Copy and paste the code below into a standard module in your Excel VBA Editor.
Note: This code uses “Late Binding,” so you do not need to manually add AutoCAD references to your Excel tools—it just works!
Option Explicit
Sub DrawRectange()
Dim AutocadApp As Object
Dim SectionCoord(0 To 9) As Double
Dim Topbar As Integer
Dim BottomBar As Integer
Dim Cover As Integer
Dim Rectang As Object
Dim ActDoc As Object
Dim InsertP(2) As Double
Dim CirObj As Object
Dim i As Long
'****** Launch Autocad application****
On Error Resume Next
Set AutocadApp = GetObject(, "Autocad.application")
On Error GoTo 0
If AutocadApp Is Nothing Then
Set AutocadApp = CreateObject("Autocad.application")
AutocadApp.Visible = True
End If
''****Read Input****
SectionCoord(0) = 0: SectionCoord(1) = 0
SectionCoord(2) = ActiveSheet.Range("f5").Value: SectionCoord(3) = 0
SectionCoord(4) = ActiveSheet.Range("f5").Value: SectionCoord(5) = ActiveSheet.Range("f6").Value
SectionCoord(6) = 0: SectionCoord(7) = ActiveSheet.Range("f6").Value
SectionCoord(8) = 0: SectionCoord(9) = 0
Topbar = ActiveSheet.Range("f8").Value
BottomBar = ActiveSheet.Range("f9").Value
Cover = ActiveSheet.Range("f10").Value
''****Draw rectangle****
Set ActDoc = AutocadApp.ActiveDocument
If ActDoc Is Nothing Then
Set ActDoc = AutocadApp.Documents.Add
End If
Set Rectang = ActDoc.ModelSpace.AddLightWeightPolyline(SectionCoord)
AutocadApp.ZoomExtents
Set AutocadApp = Nothing
Set ActDoc = Nothing
Set Rectang = Nothing
End SubHow to Use This Program
Prepare Excel:
Open a new Excel Workbook.
Enter the Width of your rectangle in cell F5.
Enter the Height/Depth of your rectangle in cell F6.
Open VBA:
Press
ALT + F11to open the VBA Editor.Go to
Insert > Module.Paste the code provided above into the window.
Run the Script:
Click anywhere inside the code and press
F5(or click the Green Play button).AutoCAD will open (or activate), and your rectangle will be drawn automatically!
Watch video tutorial HERE ON YouTube
See beam detailing from Excel here
See this post on how to draw beam section in AutoCAD from Excel here

Hi, very intresting thing to create rectangle by vba. But I have a question, how using vba create rectangle with Mtext inside into rectagle and after creation move them or put them in place by my coordinates which are into excel file. For example: I have an excel file which contained 10 different Text, dimentions of rectangle, coordinaties value for rectangle with text I have to put. I gess i need to use some loop for my 10 examples but I don’t know how i can to combined all those things in to vba code. If you can help me, this is will be awsome. Thanks