Consulting

Results 1 to 4 of 4

Thread: Delete table row containing cells with "-"but not "85-100"

  1. #1
    VBAX Regular
    Joined
    Nov 2020
    Posts
    15
    Location

    Delete table row containing cells with "-"but not "85-100"

    Hi,
    Argh. I have been trying to figure this out forever...
    But, it's time to throw in the towel and ask you guys.

    Basically, I want to find cells that only have a dash, and delete the row. The problem is, rows that contain a dash between numbers also get deleted, and I do not want that. Also, some tables may not show "-" until the fourth column. I kept trying to find something that would only delete it if there was one (thing - not sure what a dash is - text? Character?) in the cell.
    Oh, and this is only in Word. Mac.

    Test Score CI
    Subtest 1 100 90-110
    Subtest 2 100 110 When I run macro, only this row remains
    Subtest 3 - -

    Macro -
    I've made different ones, but here's an example.


    Sub Delete_Dash_Rows()
    With ActiveDocument.Range
    With .find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "-"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .format = False
    .Execute
    End With
    Do While .find.Found
    If .Information(wdWithInTable) = True Then
    .Rows.Delete
    End If
    .Collapse wdCollapseEnd
    .find.Execute
    Loop
    End With
    End Sub

    Thank you!
    -Ashley

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    Sub ScratchMacro()
    'A basic Word macro coded by Greg Maxey
    Dim oTbl As Table
    Dim oRng As Range
    Dim lngIndex As Long
      For Each oTbl In ActiveDocument.Tables
        Set oRng = oTbl.Range
        With oRng.Find
          .Text = "-"
          While .Execute
            If Len(oRng.Cells(1).Range.Text) = 3 And Asc(oRng.Cells(1).Range.Characters(1)) = 45 Then
              oRng.Cells(1).Row.Delete
            End If
          Wend
        End With
      Next
    lbl_Exit:
      Exit Sub
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Nov 2020
    Posts
    15
    Location
    Thank you!
    If you have time, would you mind explaining what 3 is?
    I think the (1) means that there is only one character in the cell. Does that mean that changing it to 5 would delete 45-50 - nope, tried it. Just curious.
    45 is for dash
    I really appreciate this forum! I love reading what doesn't work for people too.

    Don't know how to mark solved.
    Thanks,
    -Ashley

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,335
    Location
    An empty table cell isn't really empty, it contains "Cell Marker" which has a text length of 2. So if the text length of a cell is = 3 that means it has one character of text. If that character = Asc(45) then it is a dash and we delete the row.
    Last edited by gmaxey; 12-07-2020 at 07:18 AM.
    Greg

    Visit my website: http://gregmaxey.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •