Avatar of Andreas Hermle
Andreas HermleFlag for Germany

asked on 

Cut out rows that contain a specific term and re-insert them below the fourth row using VBA

Dear Experts:

I would like to run a macro code that performs the following actions:

Loop thru all the tables in the current document and then ...

a) If the first cell in the first row (cell 1,1) contains the term 'SET' this first row is to be cut out and inserted below the fourth row of that table.
b) The cells of the newly inserted row should also be merged.

Help is very much appreciated. Thank you very much in advance.

Regards, Andreas


VBAMicrosoft Word

Avatar of undefined
Last Comment
Flyster
Avatar of Flyster
Flyster
Flag of United States of America image

Hi Andreas,

Here's a modified code from the last one I submitted that I believe accomplishes what you're asking for:
Sub CutInsertRows()
Dim tbl As Table
Dim nr As Row
Dim i As Integer
Dim cv As String
Dim rc As Long

For i = 1 To ActiveDocument.Tables.Count
  Set tbl = ActiveDocument.Tables(i)
  rc = tbl.Rows.Count
    If rc < 4 Then
      GoTo Nexttbl
    End If
  tbl.Cell(1, 1).Range.Select
  cv = Selection.Text
  If InStr(1, cv, "Set") > 0 Then
    tbl.Rows(1).Select
    Selection.Cut
    With tbl
     .Cell(4, 1).Range.Select
    End With
     Selection.InsertRowsBelow 1
     Selection.Cells.Merge
     Selection.Paste
  End If
Nexttbl:
Next i

End Sub


 

Open in new window

Paul
Avatar of Norie
Norie

Andreas

Could you attach a sample document?
Avatar of Andreas Hermle

ASKER

Hi Flyster,

that's it. Thank you very much for it. There is one thing I would like to ask from you.

The inserted row should have an exact height of 2.5 cm. Could you please tweak your macro accordingly.

Thank you very much for it.

Regards, Andreas
Avatar of Andreas Hermle

ASKER

As per your Request, Norie, please find the attached document as sample file.

Thank your, Regards, Andreas

sample-file-insert-row-vba.docx
ASKER CERTIFIED SOLUTION
Avatar of Flyster
Flyster
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Andreas Hermle

ASKER

Thank you very much Flyster, works like a charm. Thank you. Regards, Andreas
Avatar of Flyster
Flyster
Flag of United States of America image

You're welcome. Hope you enjoy your weekend!
Microsoft Word
Microsoft Word

Microsoft Word is a commercial document editing program that is part of the Microsoft Office suite. It features numerous text-editing tools for creating richly formatted documents, along with tools for the use of macros in Word documents. Word's native file formats are denoted either by a .doc or .docx file extension. Plugins permitting the Windows versions of Word to read and write formats it does not natively support, such as the OpenDocument format (ODF) are available. Word can import and display images in common bitmap formats such as JPG and GIF. It can also be used to create and display simple line-art.

30K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo