How to change or add the table cell values in a drawing using VBA?

How to change or add the table cell values in a drawing using VBA?

shuaib_cad
Advocate Advocate
4,050 Views
14 Replies
Message 1 of 15

How to change or add the table cell values in a drawing using VBA?

shuaib_cad
Advocate
Advocate

I have a table in my drawing and I want to change or add the cell values using VBA.

The following is my requirement:

Existing "A1 cell" value: XXXXX

New "A1 cell" value needed: YYYYY

 

Please help me with the code.

 

 

Regards,

Mohammed Shuaib

0 Likes
Accepted solutions (1)
4,051 Views
14 Replies
Replies (14)
Message 2 of 15

grobnik
Collaborator
Collaborator

First of all you have to point to your table object inside the drawing:

For Each Object in thisdrawing.modelspace
  If Object.ObjectName="AcadTable" then
    Set MyTable=Object
  end if
Next
.... Apply here the example for cells manipulation ... 

Later you can use the online help related to Cells managing inside the drawing, removing AddTable.

Or If you want to add a Table and manipulate the cells use the entire code below coming from online help.

Sub Example_CellManipulation()
    ' This example adds a table in model space and sets and gets a column name

    Dim MyModelSpace As AcadModelSpace
    Set MyModelSpace = ThisDrawing.modelSpace
    Dim pt(2) As Double
    Dim MyTable As AcadTable
    Dim cName As String
   ' Set MyTable = MyModelSpace.AddTable(pt, 5, 5, 10, 30) ' here you add Table
    
    Call MyTable.SetCellDataType(2, 2, acLong, acUnitDistance)
    Call MyTable.SetCellFormat(1, 3, "testFormat")
    Call MyTable.SetCellState(4, 3, acCellStateContentLocked)
    Call MyTable.SetCellValue(1, 4, 5)
    
    MsgBox MyTable.GetCellValue(1, 4) & " is the test cell's value "

    ZoomExtents
    
End Sub

 

0 Likes
Message 3 of 15

shuaib_cad
Advocate
Advocate

Hi Grobnik!

 

Thanks for your response.

I already have a table in my drawing which has some values in each cell. I want to replace those values with new ones.

 

For example:

Existing "A1 cell" value: XXXXX

New "A1 cell" value needed: YYYYY

Existing "A2 cell" value: AAAAA

New "A1 cell" value needed: BBBBB

 

Please help with the code to exactly match my requirement as I am new to AutoCAD VBA.

Thanks in advance!

 

 

Regards,

Mohammed Shuaib

0 Likes
Message 4 of 15

grobnik
Collaborator
Collaborator

Here

Sub ChangeTableCell()
For Each Object in thisdrawing.modelspace
  If Object.ObjectName="AcadTable" then
    Set MyTable=Object
    exit for
  end if
Next
'object.SetCellValue row, col, val <----Function for modify cell value
    ' Object is the Table object in your drawing,
    ' row is the Table row, 
    ' col is the Table column,
    ' val is the value.
    Call MyTable.SetCellValue(1, 1, "YYYY") 
    Call MyTable.SetCellValue(2, 1, "ZZZZ")
end sub

If your Table have an heading row, the number of first row could change, and shifted by 1 unit.

Try an let us know.

As second issue I'm suggesting you to read some ebook also available on line for free about Autocad VBA.

0 Likes
Message 5 of 15

shuaib_cad
Advocate
Advocate

Hi  Grobnik,

 

The code which you have given is not working for me.

Tried changing the rows & columns numbers also. Please try and give me a different code.

 

 

Regards,

Mohammed Shuaib

0 Likes
Message 6 of 15

grobnik
Collaborator
Collaborator
Accepted solution

 

Here the code,

In any case when you say " is not working for me" usually it's better to clarify where the code hangs, or message error showed, this can help the code debug, for you and for the other.

Sub ChangeTableCell()
For Each Object In ThisDrawing.ModelSpace
  If Object.ObjectName = "AcDbTable" Then
    Set MyTable = Object
    Exit For
  End If
Next
'object.SetCellValue row, col, val <----Function for modify cell value
    ' Object is the Table object in your drawing,
    ' row is the Table row,
    ' col is the Table column,
    ' val is the value.
    Call MyTable.SetCellValue(1, 1, "YYYY")
    Call MyTable.SetCellValue(2, 1, "ZZZZ")
End Sub

 

0 Likes
Message 7 of 15

shuaib_cad
Advocate
Advocate

Hi Grobnik,

 

The code which you have given is the same as the previous one. As I already told you this code isn't working for me.

There is no error as such when I try to run it. It make the drawing and table unchanged.

 

 

Regards,

Mohammed Shuaib

 

 

 

0 Likes
Message 8 of 15

shuaib_cad
Advocate
Advocate

Can anyone help me with the VBA code please?

 

 

Regards,

Mohammed Shuaib

0 Likes
Message 9 of 15

grobnik
Collaborator
Collaborator
The code it's different from previous one check in deep before to judge.
The test of type of object it's differents.
0 Likes
Message 10 of 15

grobnik
Collaborator
Collaborator

How many tables do you have in the drawing? Which is the cad version do you have ?

Run step by step the code, debug the object name variable inside the for next loop.

Share the drawing.

0 Likes
Message 11 of 15

shuaib_cad
Advocate
Advocate

Hi Grobnik,

 

Sorry I didn't notice that change. But still it isn't working for me. The code passes through, without affecting the table.

Is there any code to find and replace the cell values?

 

 

Regards,

Mohammed Shuaib

0 Likes
Message 12 of 15

grobnik
Collaborator
Collaborator

The code is that I showed you.
On my Autocad it's working fine.
Did you been able to debug step by step the variable "Object.ObjectName" ?

In editing mode highlight the above part of code, click right mouse button
on it, select ADD WATCH, then on menu click on "view" and "Watch Window".
Run the code step by step with F8 function key, and look at watch windows to
Object.ObjectName value, check if object name will contain "Table" word,
could be AcDbTable, or AcadTable value.
On the opposite table in the drawing is not a table but it's another type of
object, perhaps a block or an ole object such as excel sheet imported.
Share the DWG.

0 Likes
Message 13 of 15

shuaib_cad
Advocate
Advocate

Hi Grobnik,

 

Yes, its working fine now. Had to remove the excel data reference from the table in my template drawing.

Now its perfect. Thanks to you!

 

 

Regards,

Mohammed Shuaib

 

 

0 Likes
Message 14 of 15

grobnik
Collaborator
Collaborator

@shuaib_cad 

I'm happy that you solved your issue, but it seems that you are in a hurry to do things and judge too quickly the support that is provided to you, without trying to understand why certain procedures in VBA, even if you are at the beginning (from what I heard).

When I pointed you to the second version of the code in order to change the contents of a table cell, you immediately sought support elsewhere, asking the forum for help again, instead of understanding why it wasn't working properly. The problem was actually within your drawing.

This not the correct approach.

Bye

0 Likes
Message 15 of 15

shuaib_cad
Advocate
Advocate

Thanks for your support!

 

 

Regards,

Mohammed Shuaib

0 Likes