Merge cells in table with VBA macro

I have a table that I want to have repeat on pages of a report. Rather than copy and paste I would like to use a macro.

I can create the macro with the table, formatting etc., but cannot find guidance to merge cells.

eg., In my three column, multiple rows table

I need to merge cells as below

   
  
     
     

I am almost there, but as I need this for a report I thought I would ask for some expert help :)

Regards Jan

In the following code, replace Selection.Tables(1) with an appropriate reference to your table

Dim mrgrng As Range
With Selection.Tables(1)
    For i = 1 To 2
        Set mrgrng = .Cell(i, 1).Range
        mrgrng.End = .Cell(i, 2).Range.End
        mrgrng.Cells.Merge
    Next i
End With

Hope this helps,
Doug Robbins - MVP Office Apps & Services (Word)
dougrobbinsmvp@gmail.com
It's time to replace ‘Diversity, Equity & Inclusion’ with ‘Excellence, Opportunity & Civility’ - V Ramaswamy

2 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

An alternative ploy would be to use building blocks. Select the blank table. Press ALT+F3 and save it as an autotext entry in the document template with the name (say) 'myTable'. Then run the following macro to insert the stored table at the cursor position.

Sub Insert_Table()
    Application.Templates(ActiveDocument.AttachedTemplate). _
            BuildingBlockEntries("myTable").Insert _
            Where:=Selection.Range, _
            RichText:=True
End Sub

If prompted to save the template, do so.

Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

This works!

However, just after Selection Tables (1) I have the formatting:

Style = "Light List - Accent 5"

I so sorry to be a pest but where do I place this code now???

ty

Jan

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Tried this (building blocks new to me - I am learning though).

Got bug error when running the macro.

I had a solution from Doug and that's almost fixed the issue.

ty

Jan

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Doug is based in Australia, so I guess he won't be here for a few hours yet.

Your line of code can still be placed between the With Selection... line and the For i = line, but it needs a dot (full stop) before the word Style:

    With Selection.Tables(1)
        .Style = "Light List - Accent 5"
        For i = 1 To 2

The dot indicates to VBA that the style applies to the table named in the With statement. Without it, VBA would assume that the word Style represents an undeclared string variable with the value "Light List - Accent 5", which is useless in the context of this macro.

_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Thanks,

I've not quite got it right yet but will soldier on. All replies are helping me understand VBA a little more.

Can I be really cheeky? I've copied the whole macro below: , where am I going wrong????

Sub Table()
'
' Table Macro
'
'
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=5, NumColumns:= _
        3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
        wdAutoFitFixed
        
    With Selection.Tables(1)
    
        If .Style <> "Table Grid" Then
            .Style = "Table Grid"
        End If
        .ApplyStyleHeadingRows = True
        .ApplyStyleLastRow = False
        .ApplyStyleFirstColumn = True
        .ApplyStyleLastColumn = False
        .ApplyStyleRowBands = True
        .ApplyStyleColumnBands = False
    End With
    Dim mrgrng As Range
With Selection.Tables(1)
    For i = 1 To 2
        Set mrgrng = .Cell(i, 1).Range
        mrgrng.End = .Cell(i, 2).Range.End
        mrgrng.Cells.Merge
    Next i
End With

    Selection.TypeText Text:="texte"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="e"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="e"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="e"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="e"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="e"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="e"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="e"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="e"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="e"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="e"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="e"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="e"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="e"
    Selection.MoveRight Unit:=wdCell
    Selection.TypeText Text:="e"
End Sub

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I'm not sure what effect you're trying to achieve, so I can't say for certain what, if anything, is wrong. From the setting of the various ApplyStyle properties, I suspect you want shading across the top and down the first column, and alternating light/dark shading on the other rows. If that's the case, you can either apply background shading explicitly to the rows and column, or you can apply one of the named styles that has the right shading (instead of applying Table Grid, which is the default for new tables, and instead of setting the ApplyStyle properties).
_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

I'll try amending the style.

Thanks for your help and time.

Jan

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Tried this (building blocks new to me - I am learning though).

Got bug error when running the macro.

What was the error?

The two obvious possibilities are that the name called does not match the name of the autotext entry; and you did not save the entry in the document template, so the macro doesn't find it.

If you open the document template, you can use the building blocks organizer both to check the name and to ensure that it is in the correct template, and use the edit function to correct whichever is wrong.

For code that will find the entry wherever you have stored it (though if you plan to distribute the template, the entry must be in that template) see the example at http://www.gmayor.com/word_vba_examples_3.htm

Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 31, 2023 Views 9,733 Applies to: