Word Macro Remove Rows from Table If Cell Empty












1















I am trying to run a Macro that will check a selected table for empty cells in column 2, and if there are empty cells, delete that row.



Sub DeleteEmptyRows()
Set Tbl = Selected.Tables(1)
With Tbl
noOfCol = Tbl.Range.Rows(1).Cells.Count
With .Range
For i = .Cells.Count To 1 Step -1
On Error Resume Next
If Len(.Cells(i).Range) = 2 Then
.Rows(.Cells(i).RowIndex).Delete
j = i Mod noOfCol
If j = 0 Then j = noOfCol
End If
Next i
End With
End With

End Sub


And it's really close to what I want, but I'm just not sure how to specify empty cells in column 2.
I also tried changing the noOfCol line to:



Selection.SetRange Selection.Tables(1).Rows(2).Cells(2).Range.Start, _
Selection.Tables(1).Rows.Last.Cells(2).Range.End


But that still deletes rows where any column is empty. I need it to delete only rows where column 2 is empty.
Thanks










share|improve this question























  • So right now you're doing For i = .Cells.Count which is all cells in the table. Try doing With .Columns(2) on the preceding line, instead of With .Range. Untested, and you made need to modify the Delete line to Tbl.Rows(i).Delete.

    – David Zemens
    Nov 15 '18 at 16:23
















1















I am trying to run a Macro that will check a selected table for empty cells in column 2, and if there are empty cells, delete that row.



Sub DeleteEmptyRows()
Set Tbl = Selected.Tables(1)
With Tbl
noOfCol = Tbl.Range.Rows(1).Cells.Count
With .Range
For i = .Cells.Count To 1 Step -1
On Error Resume Next
If Len(.Cells(i).Range) = 2 Then
.Rows(.Cells(i).RowIndex).Delete
j = i Mod noOfCol
If j = 0 Then j = noOfCol
End If
Next i
End With
End With

End Sub


And it's really close to what I want, but I'm just not sure how to specify empty cells in column 2.
I also tried changing the noOfCol line to:



Selection.SetRange Selection.Tables(1).Rows(2).Cells(2).Range.Start, _
Selection.Tables(1).Rows.Last.Cells(2).Range.End


But that still deletes rows where any column is empty. I need it to delete only rows where column 2 is empty.
Thanks










share|improve this question























  • So right now you're doing For i = .Cells.Count which is all cells in the table. Try doing With .Columns(2) on the preceding line, instead of With .Range. Untested, and you made need to modify the Delete line to Tbl.Rows(i).Delete.

    – David Zemens
    Nov 15 '18 at 16:23














1












1








1








I am trying to run a Macro that will check a selected table for empty cells in column 2, and if there are empty cells, delete that row.



Sub DeleteEmptyRows()
Set Tbl = Selected.Tables(1)
With Tbl
noOfCol = Tbl.Range.Rows(1).Cells.Count
With .Range
For i = .Cells.Count To 1 Step -1
On Error Resume Next
If Len(.Cells(i).Range) = 2 Then
.Rows(.Cells(i).RowIndex).Delete
j = i Mod noOfCol
If j = 0 Then j = noOfCol
End If
Next i
End With
End With

End Sub


And it's really close to what I want, but I'm just not sure how to specify empty cells in column 2.
I also tried changing the noOfCol line to:



Selection.SetRange Selection.Tables(1).Rows(2).Cells(2).Range.Start, _
Selection.Tables(1).Rows.Last.Cells(2).Range.End


But that still deletes rows where any column is empty. I need it to delete only rows where column 2 is empty.
Thanks










share|improve this question














I am trying to run a Macro that will check a selected table for empty cells in column 2, and if there are empty cells, delete that row.



Sub DeleteEmptyRows()
Set Tbl = Selected.Tables(1)
With Tbl
noOfCol = Tbl.Range.Rows(1).Cells.Count
With .Range
For i = .Cells.Count To 1 Step -1
On Error Resume Next
If Len(.Cells(i).Range) = 2 Then
.Rows(.Cells(i).RowIndex).Delete
j = i Mod noOfCol
If j = 0 Then j = noOfCol
End If
Next i
End With
End With

End Sub


And it's really close to what I want, but I'm just not sure how to specify empty cells in column 2.
I also tried changing the noOfCol line to:



Selection.SetRange Selection.Tables(1).Rows(2).Cells(2).Range.Start, _
Selection.Tables(1).Rows.Last.Cells(2).Range.End


But that still deletes rows where any column is empty. I need it to delete only rows where column 2 is empty.
Thanks







vba ms-word






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 16:19









SylvieSylvie

115




115













  • So right now you're doing For i = .Cells.Count which is all cells in the table. Try doing With .Columns(2) on the preceding line, instead of With .Range. Untested, and you made need to modify the Delete line to Tbl.Rows(i).Delete.

    – David Zemens
    Nov 15 '18 at 16:23



















  • So right now you're doing For i = .Cells.Count which is all cells in the table. Try doing With .Columns(2) on the preceding line, instead of With .Range. Untested, and you made need to modify the Delete line to Tbl.Rows(i).Delete.

    – David Zemens
    Nov 15 '18 at 16:23

















So right now you're doing For i = .Cells.Count which is all cells in the table. Try doing With .Columns(2) on the preceding line, instead of With .Range. Untested, and you made need to modify the Delete line to Tbl.Rows(i).Delete.

– David Zemens
Nov 15 '18 at 16:23





So right now you're doing For i = .Cells.Count which is all cells in the table. Try doing With .Columns(2) on the preceding line, instead of With .Range. Untested, and you made need to modify the Delete line to Tbl.Rows(i).Delete.

– David Zemens
Nov 15 '18 at 16:23












1 Answer
1






active

oldest

votes


















0














Working with all the cells in column is a problem because it's not possible to set a Range to a column. A Range must be a continguous set of characters in the document. While a column looks contiguous, behind the scenes its content is actually not. The characters of a table run from top-left to the right, and top-to-bottom (the rows).



The closest code can get is to select the column then work in the Selection object. Or loop the rows.



The following code sample demonstrates how to "loop the rows" - similar to what the code in the question uses. The key here is to use the Table.Cells for the loop, with the For counter designating the row index and the column number (2) designating the column index.



Sub ProcessColTwo()
Dim tbl As Word.Table
Dim nrRows As Long, ColToCheck As Long, i As Long
Dim cellRange As Word.Range

Set tbl = ActiveDocument.Tables(1)
nrRows = tbl.Rows.Count
ColToCheck = 2

For i = nrRows To 1 Step -1
Set cellRange = tbl.Cell(i, ColToCheck).Range
If Len(cellRange.text) = 2 Then
cellRange.Rows(1).Delete
End If
Next i
End Sub


And here's code that demonstrates using Selection



Sub ProcessColTwo()
Dim tbl As Word.Table
Dim ColToCheck As Long
Dim cel As Word.Cell

Set tbl = ActiveDocument.Tables(1)
ColToCheck = 2
tbl.Columns(ColToCheck).Select
For Each cel In Selection.Cells
If Len(cel.Range.text) = 2 Then
cel.Range.Rows(1).Delete
End If
Next
End Sub





share|improve this answer
























  • thanks! I Went with the second code and changed it to Selection.Tables(1), because it will be called as part of another Sub to refer to a bookmarked table. Related - I tried to tweak it a bit for another table with values so that if a cell in Column 4 = 0 , delete the row. I just changed 'Len(cel.Range.Text) = 2 Then' To: 'If cel.Range.Text = 0 Then' But it didn't work. Help?

    – Sylvie
    Nov 17 '18 at 21:15













  • @Sylvie The problem is that a cell still contains those two characters, plus any you add AND content in Word is always a string, never a number. This should actually be a new question, due to these very basic principles, but try: ="0" & vbCr & Chr(7) If you want more explanation or a more elegant way to do it, please do post a new question.

    – Cindy Meister
    Nov 18 '18 at 6:50













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323710%2fword-macro-remove-rows-from-table-if-cell-empty%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Working with all the cells in column is a problem because it's not possible to set a Range to a column. A Range must be a continguous set of characters in the document. While a column looks contiguous, behind the scenes its content is actually not. The characters of a table run from top-left to the right, and top-to-bottom (the rows).



The closest code can get is to select the column then work in the Selection object. Or loop the rows.



The following code sample demonstrates how to "loop the rows" - similar to what the code in the question uses. The key here is to use the Table.Cells for the loop, with the For counter designating the row index and the column number (2) designating the column index.



Sub ProcessColTwo()
Dim tbl As Word.Table
Dim nrRows As Long, ColToCheck As Long, i As Long
Dim cellRange As Word.Range

Set tbl = ActiveDocument.Tables(1)
nrRows = tbl.Rows.Count
ColToCheck = 2

For i = nrRows To 1 Step -1
Set cellRange = tbl.Cell(i, ColToCheck).Range
If Len(cellRange.text) = 2 Then
cellRange.Rows(1).Delete
End If
Next i
End Sub


And here's code that demonstrates using Selection



Sub ProcessColTwo()
Dim tbl As Word.Table
Dim ColToCheck As Long
Dim cel As Word.Cell

Set tbl = ActiveDocument.Tables(1)
ColToCheck = 2
tbl.Columns(ColToCheck).Select
For Each cel In Selection.Cells
If Len(cel.Range.text) = 2 Then
cel.Range.Rows(1).Delete
End If
Next
End Sub





share|improve this answer
























  • thanks! I Went with the second code and changed it to Selection.Tables(1), because it will be called as part of another Sub to refer to a bookmarked table. Related - I tried to tweak it a bit for another table with values so that if a cell in Column 4 = 0 , delete the row. I just changed 'Len(cel.Range.Text) = 2 Then' To: 'If cel.Range.Text = 0 Then' But it didn't work. Help?

    – Sylvie
    Nov 17 '18 at 21:15













  • @Sylvie The problem is that a cell still contains those two characters, plus any you add AND content in Word is always a string, never a number. This should actually be a new question, due to these very basic principles, but try: ="0" & vbCr & Chr(7) If you want more explanation or a more elegant way to do it, please do post a new question.

    – Cindy Meister
    Nov 18 '18 at 6:50


















0














Working with all the cells in column is a problem because it's not possible to set a Range to a column. A Range must be a continguous set of characters in the document. While a column looks contiguous, behind the scenes its content is actually not. The characters of a table run from top-left to the right, and top-to-bottom (the rows).



The closest code can get is to select the column then work in the Selection object. Or loop the rows.



The following code sample demonstrates how to "loop the rows" - similar to what the code in the question uses. The key here is to use the Table.Cells for the loop, with the For counter designating the row index and the column number (2) designating the column index.



Sub ProcessColTwo()
Dim tbl As Word.Table
Dim nrRows As Long, ColToCheck As Long, i As Long
Dim cellRange As Word.Range

Set tbl = ActiveDocument.Tables(1)
nrRows = tbl.Rows.Count
ColToCheck = 2

For i = nrRows To 1 Step -1
Set cellRange = tbl.Cell(i, ColToCheck).Range
If Len(cellRange.text) = 2 Then
cellRange.Rows(1).Delete
End If
Next i
End Sub


And here's code that demonstrates using Selection



Sub ProcessColTwo()
Dim tbl As Word.Table
Dim ColToCheck As Long
Dim cel As Word.Cell

Set tbl = ActiveDocument.Tables(1)
ColToCheck = 2
tbl.Columns(ColToCheck).Select
For Each cel In Selection.Cells
If Len(cel.Range.text) = 2 Then
cel.Range.Rows(1).Delete
End If
Next
End Sub





share|improve this answer
























  • thanks! I Went with the second code and changed it to Selection.Tables(1), because it will be called as part of another Sub to refer to a bookmarked table. Related - I tried to tweak it a bit for another table with values so that if a cell in Column 4 = 0 , delete the row. I just changed 'Len(cel.Range.Text) = 2 Then' To: 'If cel.Range.Text = 0 Then' But it didn't work. Help?

    – Sylvie
    Nov 17 '18 at 21:15













  • @Sylvie The problem is that a cell still contains those two characters, plus any you add AND content in Word is always a string, never a number. This should actually be a new question, due to these very basic principles, but try: ="0" & vbCr & Chr(7) If you want more explanation or a more elegant way to do it, please do post a new question.

    – Cindy Meister
    Nov 18 '18 at 6:50
















0












0








0







Working with all the cells in column is a problem because it's not possible to set a Range to a column. A Range must be a continguous set of characters in the document. While a column looks contiguous, behind the scenes its content is actually not. The characters of a table run from top-left to the right, and top-to-bottom (the rows).



The closest code can get is to select the column then work in the Selection object. Or loop the rows.



The following code sample demonstrates how to "loop the rows" - similar to what the code in the question uses. The key here is to use the Table.Cells for the loop, with the For counter designating the row index and the column number (2) designating the column index.



Sub ProcessColTwo()
Dim tbl As Word.Table
Dim nrRows As Long, ColToCheck As Long, i As Long
Dim cellRange As Word.Range

Set tbl = ActiveDocument.Tables(1)
nrRows = tbl.Rows.Count
ColToCheck = 2

For i = nrRows To 1 Step -1
Set cellRange = tbl.Cell(i, ColToCheck).Range
If Len(cellRange.text) = 2 Then
cellRange.Rows(1).Delete
End If
Next i
End Sub


And here's code that demonstrates using Selection



Sub ProcessColTwo()
Dim tbl As Word.Table
Dim ColToCheck As Long
Dim cel As Word.Cell

Set tbl = ActiveDocument.Tables(1)
ColToCheck = 2
tbl.Columns(ColToCheck).Select
For Each cel In Selection.Cells
If Len(cel.Range.text) = 2 Then
cel.Range.Rows(1).Delete
End If
Next
End Sub





share|improve this answer













Working with all the cells in column is a problem because it's not possible to set a Range to a column. A Range must be a continguous set of characters in the document. While a column looks contiguous, behind the scenes its content is actually not. The characters of a table run from top-left to the right, and top-to-bottom (the rows).



The closest code can get is to select the column then work in the Selection object. Or loop the rows.



The following code sample demonstrates how to "loop the rows" - similar to what the code in the question uses. The key here is to use the Table.Cells for the loop, with the For counter designating the row index and the column number (2) designating the column index.



Sub ProcessColTwo()
Dim tbl As Word.Table
Dim nrRows As Long, ColToCheck As Long, i As Long
Dim cellRange As Word.Range

Set tbl = ActiveDocument.Tables(1)
nrRows = tbl.Rows.Count
ColToCheck = 2

For i = nrRows To 1 Step -1
Set cellRange = tbl.Cell(i, ColToCheck).Range
If Len(cellRange.text) = 2 Then
cellRange.Rows(1).Delete
End If
Next i
End Sub


And here's code that demonstrates using Selection



Sub ProcessColTwo()
Dim tbl As Word.Table
Dim ColToCheck As Long
Dim cel As Word.Cell

Set tbl = ActiveDocument.Tables(1)
ColToCheck = 2
tbl.Columns(ColToCheck).Select
For Each cel In Selection.Cells
If Len(cel.Range.text) = 2 Then
cel.Range.Rows(1).Delete
End If
Next
End Sub






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 17:32









Cindy MeisterCindy Meister

15.7k102337




15.7k102337













  • thanks! I Went with the second code and changed it to Selection.Tables(1), because it will be called as part of another Sub to refer to a bookmarked table. Related - I tried to tweak it a bit for another table with values so that if a cell in Column 4 = 0 , delete the row. I just changed 'Len(cel.Range.Text) = 2 Then' To: 'If cel.Range.Text = 0 Then' But it didn't work. Help?

    – Sylvie
    Nov 17 '18 at 21:15













  • @Sylvie The problem is that a cell still contains those two characters, plus any you add AND content in Word is always a string, never a number. This should actually be a new question, due to these very basic principles, but try: ="0" & vbCr & Chr(7) If you want more explanation or a more elegant way to do it, please do post a new question.

    – Cindy Meister
    Nov 18 '18 at 6:50





















  • thanks! I Went with the second code and changed it to Selection.Tables(1), because it will be called as part of another Sub to refer to a bookmarked table. Related - I tried to tweak it a bit for another table with values so that if a cell in Column 4 = 0 , delete the row. I just changed 'Len(cel.Range.Text) = 2 Then' To: 'If cel.Range.Text = 0 Then' But it didn't work. Help?

    – Sylvie
    Nov 17 '18 at 21:15













  • @Sylvie The problem is that a cell still contains those two characters, plus any you add AND content in Word is always a string, never a number. This should actually be a new question, due to these very basic principles, but try: ="0" & vbCr & Chr(7) If you want more explanation or a more elegant way to do it, please do post a new question.

    – Cindy Meister
    Nov 18 '18 at 6:50



















thanks! I Went with the second code and changed it to Selection.Tables(1), because it will be called as part of another Sub to refer to a bookmarked table. Related - I tried to tweak it a bit for another table with values so that if a cell in Column 4 = 0 , delete the row. I just changed 'Len(cel.Range.Text) = 2 Then' To: 'If cel.Range.Text = 0 Then' But it didn't work. Help?

– Sylvie
Nov 17 '18 at 21:15







thanks! I Went with the second code and changed it to Selection.Tables(1), because it will be called as part of another Sub to refer to a bookmarked table. Related - I tried to tweak it a bit for another table with values so that if a cell in Column 4 = 0 , delete the row. I just changed 'Len(cel.Range.Text) = 2 Then' To: 'If cel.Range.Text = 0 Then' But it didn't work. Help?

– Sylvie
Nov 17 '18 at 21:15















@Sylvie The problem is that a cell still contains those two characters, plus any you add AND content in Word is always a string, never a number. This should actually be a new question, due to these very basic principles, but try: ="0" & vbCr & Chr(7) If you want more explanation or a more elegant way to do it, please do post a new question.

– Cindy Meister
Nov 18 '18 at 6:50







@Sylvie The problem is that a cell still contains those two characters, plus any you add AND content in Word is always a string, never a number. This should actually be a new question, due to these very basic principles, but try: ="0" & vbCr & Chr(7) If you want more explanation or a more elegant way to do it, please do post a new question.

– Cindy Meister
Nov 18 '18 at 6:50






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323710%2fword-macro-remove-rows-from-table-if-cell-empty%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine