#1
|
||||
|
||||
How to script most table column in word document?
Hello VBA Pros,
I need your guidance. I wish to have a macro to count for me the table with the most columns. So among my 100's of tables, have a macro that would tell me ''the table with the most columns is 10 in this currant Word document'' for example. I'm trying to find a way, and nothing is comming to me. Any insights? Cendrinne |
#2
|
||||
|
||||
I haven't done rigorous testing but this should get you started
Code:
Sub TestTables() Dim i As Integer, x As Integer, iTbl As Integer, sOthers As String For i = 1 To ActiveDocument.Tables.Count If ActiveDocument.Tables(i).Columns.Count > x Then x = ActiveDocument.Tables(i).Columns.Count iTbl = i sOthers = "" ElseIf ActiveDocument.Tables(i).Columns.Count = x Then sOthers = sOthers & i & ", " End If Next i If sOthers <> "" Then sOthers = "Other tables with same column count are: " & sOthers MsgBox "The table with the most columns is " & iTbl & vbCr & sOthers, , x & " Columns" End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
||||
|
||||
Thanks, Andrew, It's a great Start.
i've tried it, I don't know why, it gives me 5, when the most columns I had was 9. I've created some mini tables, from 2 to 9, and still say's 5. Well your code us much better than mine, cause I've taken parts from other macro's, and what ever I've done, but I keep getting error message. I was thinking it can't be done until your answer. Code:
Sub TST_Msg_eCount_Tables_Columns() '98 Dim Tbl As Table If ActiveDocument.Tables.Count >= 1 Then 'MsgBox ActiveDocument.Tables(1).Columns.Count 'MsgBox Selection.Information(wdMaximumNumberOfColumns) MsgBox Information(wdMaximumNumberOfColumns) End If End Sub Thanks a million, it's a great start At least I'm going forward with your script |
#4
|
|||
|
|||
Nancy,
I think Andrew had a slight glitch in his msgbox string. Try: Code:
Sub TestTables() Dim i As Integer, x As Integer, iTbl As Integer, sOthers As String For i = 1 To ActiveDocument.Tables.Count If ActiveDocument.Tables(i).Columns.Count > x Then x = ActiveDocument.Tables(i).Columns.Count iTbl = i sOthers = "" ElseIf ActiveDocument.Tables(i).Columns.Count = x Then sOthers = sOthers & i & ", " End If Next i If sOthers <> "" Then sOthers = "Other tables with same column count are: " & sOthers MsgBox "The table with the most columns is " & iTbl & vbCr + vbCr _ & x & " Columns." & vbCr + vbCr & sOthers End Sub |
#5
|
||||
|
||||
Thanks Greg (it's getting better).....
Gregggggggg, Hello
So glad to hear from you And thank you for providing a little twik. However, for the love of me, I don't know why, it say's on the first line: The Table with the most columns is 5 space 9 Columns. I'm wondering, where is it getting the 5 from??? But it's ok, it will be a challenge for me to try to figure it out. Like a puzzle. I want to thank you both from the bottom of my heart, cause it was a pain in the butt to scroll, to see which one that had the most columns. So already, this will help. If I figure out the ''5'', I'll let you know. But please don't give me the answer, but maybe just ''hint'' where to look to fix it up, if it's taking too long for me to answer But give me time, I'm still at work. Was just curious what it was said about the 5 Have a great one Last edited by Cendrinne; 03-22-2021 at 06:08 PM. |
#6
|
||||
|
||||
by the way, how can I imput an image if I don't have an url? It's in my computer
|
#7
|
|||
|
|||
Nancy,
Step through the code in a sample document, containing just a few tables and you will see what it means in short order. |
#8
|
||||
|
||||
The macro I created told you WHICH table has the most columns. Surely that was your original question.
Quote:
Did you also want to scroll to that (first) table with the most columns? If so, add another line at the bottom of the code Code:
If iTbl > 0 Then ActiveDocument.Tables(iTbl).Range.Select
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#9
|
|||
|
|||
Andrew, you are absolutely correct. I didn't even read the msgbox title.
|
#10
|
||||
|
||||
Eurrrrrrrrrrrrrrrreka
I found it Not certain what I'm doing, but I've played with it so much, it was Trial and Error. Code:
Dim i As Integer, x As Integer, iTbl As Integer, sOthers As String For i = 1 To ActiveDocument.Tables.Count If ActiveDocument.Tables(i).Columns.Count > x Then x = ActiveDocument.Tables(i).Columns.Count iTbl = i sOthers = "" ElseIf ActiveDocument.Tables(i).Columns.Count = x Then sOthers = sOthers & "" End If Next i MsgBox "The table with the most columns is " & x & " Columns." On Error GoTo 0 OMG Thank you so much. Million appreciations for your guidance and help. What I've put, does it makes sense to you both? Cendrinne |
#11
|
||||
|
||||
Andrew,
You wrote ''The macro I created told you WHICH table has the most columns. Surely that was your original question.'' No, OMG, I'm so sorry, if I'm not more clear when I write. I truly appologize. What I was trying to do, is to only know, among my 100's of tables, give me the highest number of columns. See, that was more clearer. OK lesson learn. Again, I'm sorry, but I do thank you for your wonderful collaboration. I like to learn and discuss with smart people as yourselves Cendrinne |
#12
|
||||
|
||||
If your document could potentially have tables with horizontally-merged cells, uneven cell widths in a column, or vertically-split cells, you need something like:
Code:
Sub TestTables() Dim t As Long, c As Long, x As Long, y As Long, z As Long, StrTbls As String With ActiveDocument For t = 1 To .Tables.Count With .Tables(t) If .Uniform = True Then z = .Columns.Count If z = x Then StrTbls = StrTbls & ", " & t If z > x Then x = z: StrTbls = t Else z = 0 With .Range For c = 1 To .Cells.Count y = .Cells(c).ColumnIndex If y >= x Then z = y Next End With If z = x Then StrTbls = StrTbls & ", " & t If z > x Then x = z: StrTbls = t End If End With Next End With MsgBox "The most columns (" & x & ") are found in table(s): " & StrTbls End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#13
|
||||
|
||||
No problem. The wording of your msgbox is still not exactly clear for me so I will suggest a minor modification to make it crystal clear. I would word the output as...
MsgBox "The table with the most columns contains " & x & " columns."
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#14
|
||||
|
||||
Oh great thinking, Paul
OK, I'll copy that and test it
Thanks, Paul. |
#15
|
||||
|
||||
OK thank you Andrew...
Quote:
I'm just so happy, to finally have something that was annoying to me for so long. Thanks to this forum I'm sooooo grateful nice people as yourselves, helps people like me Cendrinne |
Tags |
help please, most table column |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
VBA to add final touches to merged document, after running MailMergeToDoc script | NicB | Word VBA | 1 | 09-02-2020 05:47 PM |
How to generate a word document with a table-script | valdavaux | Word | 1 | 09-17-2017 03:32 PM |
Is it possible to put a formula in a table column header to define the name of the column? | JacquesW | Excel | 3 | 05-08-2017 08:00 AM |
VBA to search each row of a word table column | Marrick13 | Word VBA | 7 | 11-17-2014 04:33 AM |
two column word document. | plato | Word | 1 | 08-06-2010 04:02 AM |