Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88835

how to delete the last rows of a table in word from excel vba

$
0
0

I've populated a table in word with data from excel, through Find&Replace method. I have to delete the rows where the data has not been replace, because all the data from Excel has been replaced in Word.

The code is this:

Sub test1()

Dim x As Integer
Dim y As Integer
Dim TagValue As String
Dim TagName As String
Dim Wapp As Word.Application
Dim Wdoc As Word.Document
Dim nColumns As Integer
Dim nRows As Integer
Dim wdTable
Dim i As Integer

Range("a1").CurrentRegion.Name = "data"
nColumns = Range("data").Columns.Count
nRows = Range("data").Rows.Count

Set Wapp = CreateObject("Word.Application")
Set Wdoc = Wapp.Documents.Open("\\###########\test\AuditTest.docx")
Wapp.Visible = True
For y = 3 To nRows
    For x = 1 To nColumns
        On Error Resume Next
        TagName = Sheets("Sheet1").Cells(2, x).Value
        TagValue = Sheets("Sheet1").Cells(y, x).Value

        With Wdoc.Content.Find
            .Text = TagName
            .Replacement.Text = TagValue
            .Wrap = wdFindContinue
            .Execute Replace:=wdReplaceOne
        End With

    Next x
Next y

Set wdTable = Wdoc.Tables(1)

wdTable.Select
' THE PART DOES THAT NOT WORK IS THIS. I HAVE THE LAST 7 ROWS IN WHICH IN THE FIRST CELL 
'THERE IS ".auditcode." BUT THE ROUTINE IS NOT DELETING IT
For i = wdTable.Rows.Count To 2 Step -1
    If wdTable.Cell(i, 1).Range.Text = ".auditcode." Then `enter code here`wdTable.Rows(i).Delete
Next i

'Wdoc.SaveAs ("\\#############\test\AuditTest.docx")

End Sub

Viewing all articles
Browse latest Browse all 88835


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>