Share via

Word visual basic code - working with a table

Anonymous
2014-03-13T16:03:56+00:00

I have a big table in Word that I need to crop, it looks something like this:

asdf asdf asdf asdf asdf
asdf asdf asdf asdf asdf
asdf asdf asdf asdf asdf
asdf asdf sadf Closed Captions Slide number
asdf sadf asdf Hello, how are you? 1
asdf asdf asdf I am fine. 2

What I need is a macro that will delete every row from the top until the row contains either "closed captions" or "slide number" So that the table looks like this:

asdf asdf asdf Closed Captions Slide number
asdf asdf asdf Hello, how are you? 1
asdf asdf asdf I am fine. 2

I should be able to figure out the rest...

It would be nice to add a header to the document that includes the name of the word document. So if the document is "Hello World.doc" the header will say "Hello World"

Thanks a bunch!

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Paul Edstein 82,861 Reputation points Volunteer Moderator
2014-03-14T00:47:12+00:00

For the table, you could use code like:

With ActiveDocument.Tables(1)

  While (InStr(.Rows(1).Range.Text, "Closed Captions") = 0) And (InStr(.Rows(1).Range.Text, "Slide number") = 0)

    .Rows(1).Delete

  Wend

End With

For the filename, you could put a FILENAME field in the header. This will show "Hello World.doc". No macros required. If you really don't want the extension, then code will be required, including checking to make sure nothing else in the header is affected.

Was this answer helpful?

0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Anonymous
    2015-02-13T15:49:11+00:00

    Thank you, Paul, I really appreciate your help.

    Was this answer helpful?

    0 comments No comments
  2. Paul Edstein 82,861 Reputation points Volunteer Moderator
    2015-02-12T23:16:03+00:00

    You could use a macro like:

    Sub Demo()

    Application.ScreenUpdating = False

    Dim i As Long, Rng As Range

    With ActiveDocument.Tables(1)

      .Columns.Add

      For i = 1 To .Rows.Count

        Set Rng = .Cell(i, 1).Range

        With Rng

          .End = .End - 1

          If Len(.Text) > 6 Then

            .Start = .End - 6

          End If

        End With

        .Cell(i, 3).Range.Text = Rng.Text

      Next

      .Sort ExcludeHeader:=False, FieldNumber:="Column 3", CaseSensitive:=False, _

        SortFieldType:=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderAscending

      .Columns(3).Delete

    End With

    Application.ScreenUpdating = True

    End Sub

    If the table has a header row, change:

    ExcludeHeader:=False

    to:

    ExcludeHeader:=True

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-02-12T17:35:08+00:00

    Hi Paul,

    I am not a programmer, and I would appreciate if you can help me too.

    I have a Word 2013 table with two columns and I would like to sort the table by the last 6 characters of the text in column A?

    Is there a way to do this?

    Thanks,

    Mike

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2014-03-14T14:17:52+00:00

    Awesome thanks! That is exactly what I needed!!

    Was this answer helpful?

    0 comments No comments