PDA

View Full Version : Delete table row containing cells with "-"but not "85-100"



AshZM
12-04-2020, 06:15 PM
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

gmaxey
12-04-2020, 07:01 PM
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

AshZM
12-05-2020, 02:29 PM
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

gmaxey
12-06-2020, 08:41 AM
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.