PDA

View Full Version : Delete colums in a table using a macro if 2 column is empty



Pontos
11-25-2013, 07:53 AM
Hi,


I have found this macro which is working, but not entirely as I want.
I have large document (on average with 500 pages) in which I have to delete every row in tables that have in second column no value.
Document is made using Mail Merge, so I have table on 1 page, than on 3, 6, 8,...


So far is macro deleting all empty rows when in 2 columns is no value but only in table in which is cursor, and not in all other tables.



Public Sub DeleteEmptyColums()
Dim Table As Table, Row As Range, Cell As Cell, Counter As Long, _
NumRows As Long, TextInRow As Boolean
' Specify which table you want to work on.
For Each Table In ActiveDocument.Tables
' Set a range variable to the first row's range
Set Row = Table.Rows(1).Range
NumRows = Table.Rows.Count
Application.ScreenUpdating = False
For Counter = 1 To NumRows
StatusBar = "Row " & Counter
TextInRow = False
With Selection.Tables(1)
For i = .Rows.Count To 1 Step -1
If Len(.Cell(i, 2).Range.Text) = 2 Then
.Rows(i).Delete
End If
Next i
End With
Next Counter
Next Table
Application.ScreenUpdating = True
End Sub


Could somebody help me.


Kind regards,
Rok

macropod
11-25-2013, 02:09 PM
Hi Pontos,

Your post is confusing - do you want to delete columns or rows?

PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.

Pontos
11-25-2013, 02:26 PM
Hi Pontos,

Your post is confusing - do you want to delete columns or rows?

PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab.

Yes, I realize that.

I want that all rows that have no value in 2nd column to be deleted. Example: in this table, rows 2., 4.& 5. must be deleted.



1.
Test


2.



3.
Another test


4.



5.




Macro is doing that, but I can't fix it that will go through all tables that are in one Word document.

Thanks,
Rok

macropod
11-25-2013, 03:07 PM
Try:

Sub DelBlankCol2Rows()
Application.ScreenUpdating = False
Dim i As Long, j As Long, bFit As Boolean
With ActiveDocument
For i = .Tables.Count To 1 Step -1
With .Tables(i)
bFit = .AllowAutoFit
.AllowAutoFit = False
For j = .Rows.Count To 1 Step -1
If Len(Trim(.Rows(j).Cells(2).Range.Text)) = 2 Then .Rows(j).Delete
Next
.AllowAutoFit = bFit
End With
Next
End With
Application.ScreenUpdating = True
End Sub

macropod
11-26-2013, 01:59 AM
Cross-posted at: http://www.msofficeforums.com/word-vba/18779-delete-row-when-2-column-has-no.html
For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184