Amend a VBA delete empty rows

dslhs

Board Regular
Joined
Apr 4, 2022
Messages
52
Office Version
  1. 2019
Platform
  1. Windows
Hello!

I was using the below VBA with documents and it was working like a charm...


Sub DelBlankRows()
Dim Pwd As String, pState As Boolean, Tbl As Table, oRow As Row
Dim oCel As Cell, Rng As Range, Fld As FormField, Str As String
With ActiveDocument
pState = False
If .ProtectionType <> wdNoProtection Then
Pwd = InputBox("Please enter the Password", "Password")
pState = True
.Unprotect Pwd
End If
For Each Tbl In .Tables
For Each oRow In Tbl.Rows
Str = vbNullString
For Each oCel In oRow.Cells
Set Rng = oCel.Range
With Rng
.End = .End - 1
If .FormFields.Count > 0 Then
For Each Fld In Rng.FormFields
Str = Str & Trim(Fld.Result)
Next
Else
Str = Str & Trim(.Text)
End If
End With
Next
If Str = vbNullString Then
On Error Resume Next 'skip vertically merged cells
oRow.Delete
End If
Next oRow
Next Tbl
If pState = True Then .Protect Type:=wdAllowOnlyFormFields, Noreset:=True, Password:=Pwd
pState = False: Pwd = vbNullString: Set Rng = Nothing
End With
End Sub

However, I slightly modified the documents (included tables created from a different source) and now I get an error which highlights the below line:

For Each oRow In Tbl.Rows

How do I modify the code so that if it can't identify a table/row properly, it just skips until the next one? I don't need the VBA to run on the new tables, just on the previous ones.

Many thanks!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Does anyone have an idea on how to fix this? Would be really appreciated
 
Upvote 0

Forum statistics

Threads
1,226,527
Messages
6,191,575
Members
453,665
Latest member
WaterWorks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top