PDA

View Full Version : Solved: Check for empty values in a word table



white_flag
02-25-2013, 02:59 AM
Hello
I have a table imported from excel. And I like to check if the values from the first column and the second are empty. If there are empty to delete entire row: The code is like this:


Option Explicit

Sub table()
Dim i As Integer, j
For i = 1 To ActiveDocument.Tables.Count
With ActiveDocument.Tables(i).Range
.Select
For j = ActiveDocument.Tables(i).Rows.Count To 1 Step -1
If (ActiveDocument.Tables(i).Cell(j, 1).Range.Text) = "" And (ActiveDocument.Tables(i).Cell(j, 2).Range.Text) = "" Then
ActiveDocument.Tables(i).Rows(j).Delete
End If
Next j
End With
Next
End Sub


but it is not working. I try with len function but also is not going... any idea?

macropod
02-25-2013, 03:17 AM
The 'empty' cells retain an end-of-cell marker, which accounts for two characters. Try:
Sub Table()
Application.ScreenUpdating = False
Dim i As Long, j As Long
With ActiveDocument
For i = .Tables.Count To 1 Step -1
With .Tables(i)
For j = .Rows.Count To 1 Step -1
With .Rows(j)
If Len(.Cells(1).Range.Text) = 2 Then _
If Len(.Cells(2).Range.Text) = 2 Then .Delete
End With
Next j
End With
Next
End With
Application.ScreenUpdating = True
End Sub

white_flag
02-25-2013, 03:18 AM
I try also this:


Sub table1()
Dim i As Integer, j
For i = 1 To ActiveDocument.Tables.Count
With ActiveDocument.Tables(i)
.Select
For j = .Rows.Count To 1 Step -1
If Left(.Cell(j, 1).Range.Text, Len(.Cell(j, 1).Range.Text) - 2) = "" And Left(.Cell(j, 2).Range.Text, Len(.Cell(j, 2).Range.Text) - 2) = "" Then
ActiveDocument.Tables(i).Rows(j).Delete
End If
Next j
End With
Next

End Sub


but nothing

white_flag
02-25-2013, 03:33 AM
Hello macropod

Thank you for your time. I try your code but nothing.

the len for the values from cell return like this:

len(TOTAL) = 7 - logic
len(10) = 4 - logic
len(1) = 3 - logic
len() = 3 - not logic

white_flag
02-25-2013, 03:46 AM
I forget the doc.

macropod
02-25-2013, 03:47 AM
The code I posted does work. In all likelihood, the code doesn't work for you because your cells are not empty. Try:
Sub Table()
Application.ScreenUpdating = False
Dim i As Long, j As Long
With ActiveDocument
For i = .Tables.Count To 1 Step -1
With .Tables(i)
For j = .Rows.Count To 1 Step -1
With .Rows(j)
If Len(Trim(.Cells(1).Range.Text)) = 2 Then _
If Len(Trim(.Cells(2).Range.Text)) = 2 Then .Delete
End With
Next j
End With
Next
End With
Application.ScreenUpdating = True
End Sub

white_flag
02-25-2013, 03:58 AM
Yes you are correct (thank you for your code)

but I do not understand why the copy paste, from excel to word will add "one space" in columns that are empty.. any idea?

macropod
02-25-2013, 04:46 AM
I do not understand why the copy paste, from excel to word will add "one space" in columns that are empty.. any idea?
Actually, depending on the cell, it can paste either an ordinary space or a non-breaking space for empty cells. So you might need to use:
Sub Table()
Application.ScreenUpdating = False
Dim i As Long, j As Long
With ActiveDocument
For i = .Tables.Count To 1 Step -1
With .Tables(i)
For j = .Rows.Count To 1 Step -1
With .Rows(j)
If Len(Trim(Replace(.Cells(1).Range.Text, Chr(160), ""))) = 2 Then _
If Len(Trim(Replace(.Cells(2).Range.Text, Chr(160), ""))) = 2 Then .Delete
End With
Next j
End With
Next
End With
Application.ScreenUpdating = True
End Sub

white_flag
02-25-2013, 04:50 AM
Yes, it was a non breaking space chr(160) (not just space chr(13)). But I got the idea that the cells are not empty.

thank you very much (for coding and time) :)