Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-21-2021, 06:14 PM
Cendrinne's Avatar
Cendrinne Cendrinne is offline How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2019
Competent Performer
How to script most table column in word document?
 
Join Date: Aug 2019
Location: Montreal Quebec Canada
Posts: 190
Cendrinne is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 03-21-2021, 08:18 PM
Guessed's Avatar
Guessed Guessed is online now How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 03-22-2021, 06:28 AM
Cendrinne's Avatar
Cendrinne Cendrinne is offline How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2019
Competent Performer
How to script most table column in word document?
 
Join Date: Aug 2019
Location: Montreal Quebec Canada
Posts: 190
Cendrinne is on a distinguished road
Default

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
I'm back at work now, so I'll try maybe tonight, unless you find a reasons why it keeps saying 5. What ever I find, I'll let you know.

Thanks a million, it's a great start At least I'm going forward with your script
Reply With Quote
  #4  
Old 03-22-2021, 07:27 AM
gmaxey gmaxey is offline How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 03-22-2021, 12:38 PM
Cendrinne's Avatar
Cendrinne Cendrinne is offline How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2019
Competent Performer
How to script most table column in word document?
 
Join Date: Aug 2019
Location: Montreal Quebec Canada
Posts: 190
Cendrinne is on a distinguished road
Default 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.
Reply With Quote
  #6  
Old 03-22-2021, 12:39 PM
Cendrinne's Avatar
Cendrinne Cendrinne is offline How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2019
Competent Performer
How to script most table column in word document?
 
Join Date: Aug 2019
Location: Montreal Quebec Canada
Posts: 190
Cendrinne is on a distinguished road
Default

by the way, how can I imput an image if I don't have an url? It's in my computer
Reply With Quote
  #7  
Old 03-22-2021, 12:46 PM
gmaxey gmaxey is offline How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Nancy,


Step through the code in a sample document, containing just a few tables and you will see what it means in short order.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 03-22-2021, 02:18 PM
Guessed's Avatar
Guessed Guessed is online now How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

The macro I created told you WHICH table has the most columns. Surely that was your original question.

Quote:
Originally Posted by Cendrinne View Post
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''...
If my macro was telling you 5 then that means the 5th table has the most columns. I also included that column count in the MsgBox title - although Greg thought that was an error and he moved it to the body of the MsgBox.

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
Reply With Quote
  #9  
Old 03-22-2021, 04:07 PM
gmaxey gmaxey is offline How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,427
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Andrew, you are absolutely correct. I didn't even read the msgbox title.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #10  
Old 03-22-2021, 06:02 PM
Cendrinne's Avatar
Cendrinne Cendrinne is offline How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2019
Competent Performer
How to script most table column in word document?
 
Join Date: Aug 2019
Location: Montreal Quebec Canada
Posts: 190
Cendrinne is on a distinguished road
Default

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
I didn't really need to know about the others, so I've played with it, until I didn't see Red, or an Error message.

OMG Thank you so much.

Million appreciations for your guidance and help.

What I've put, does it makes sense to you both?

Cendrinne
Reply With Quote
  #11  
Old 03-22-2021, 06:15 PM
Cendrinne's Avatar
Cendrinne Cendrinne is offline How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2019
Competent Performer
How to script most table column in word document?
 
Join Date: Aug 2019
Location: Montreal Quebec Canada
Posts: 190
Cendrinne is on a distinguished road
Default

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
Reply With Quote
  #12  
Old 03-22-2021, 06:26 PM
macropod's Avatar
macropod macropod is offline How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #13  
Old 03-22-2021, 06:28 PM
Guessed's Avatar
Guessed Guessed is online now How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #14  
Old 03-22-2021, 06:29 PM
Cendrinne's Avatar
Cendrinne Cendrinne is offline How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2019
Competent Performer
How to script most table column in word document?
 
Join Date: Aug 2019
Location: Montreal Quebec Canada
Posts: 190
Cendrinne is on a distinguished road
Default Oh great thinking, Paul

OK, I'll copy that and test it

Thanks, Paul.
Reply With Quote
  #15  
Old 03-22-2021, 06:36 PM
Cendrinne's Avatar
Cendrinne Cendrinne is offline How to script most table column in word document? Windows 10 How to script most table column in word document? Office 2019
Competent Performer
How to script most table column in word document?
 
Join Date: Aug 2019
Location: Montreal Quebec Canada
Posts: 190
Cendrinne is on a distinguished road
Default OK thank you Andrew...

Quote:
Originally Posted by Guessed View Post
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."
I don't know if you know, but I'm fully bilingual (French and English), but not an expert in either language. I manage, and do my best. But I try to learn from other people, so I take your suggestions

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
Reply With Quote
Reply

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
How to script most table column in word document? two column word document. plato Word 1 08-06-2010 04:02 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:34 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft