atuck88
02-05-2022, 06:51 AM
I have two tables in Word.
One table contains laboratory data and its corresponding analytical method code/value in column one.
The second table (which lists the accredited methods used) has the method code/value in column two.
I need the VBA code so it searches the second column in table two and if it is not present in table one (column one), delete the row from table two.
The method codes generally consist of things like; INORG-L01, METALS-L, ORG-L1.
The code so far consist of:
If Selection.Information(wdWithInTable) = FalseThen MsgBox ("Put cursor inside a table first!")
Exit Sub
Else
With Selection.Tables(1)
nRows = .Rows.Count
For nRowIndex = nRows To 1 Step -1
varCellEmpty = True
For nColumns = 2 To .Columns.Count
Set objCell = .Rows(nRowIndex).Cells(nColumns).Range
objCell.End = objCell.End - 1
If Len(objCell) > 0 And Not objCell.Text = "-" Then
varCellEmpty = False
Exit For
End If
Next nColumns
If varCellEmpty = True Then .Rows(nRowIndex).Delete
Next nRowIndex
End With
End If
Set objCell = Nothing
Application.ScreenUpdating = True
This can search through tables and delete the rows (when "-" is replaced with the method codes) but I don’t know how to get it to work like that described above. I’m a bit lost…
One table contains laboratory data and its corresponding analytical method code/value in column one.
The second table (which lists the accredited methods used) has the method code/value in column two.
I need the VBA code so it searches the second column in table two and if it is not present in table one (column one), delete the row from table two.
The method codes generally consist of things like; INORG-L01, METALS-L, ORG-L1.
The code so far consist of:
If Selection.Information(wdWithInTable) = FalseThen MsgBox ("Put cursor inside a table first!")
Exit Sub
Else
With Selection.Tables(1)
nRows = .Rows.Count
For nRowIndex = nRows To 1 Step -1
varCellEmpty = True
For nColumns = 2 To .Columns.Count
Set objCell = .Rows(nRowIndex).Cells(nColumns).Range
objCell.End = objCell.End - 1
If Len(objCell) > 0 And Not objCell.Text = "-" Then
varCellEmpty = False
Exit For
End If
Next nColumns
If varCellEmpty = True Then .Rows(nRowIndex).Delete
Next nRowIndex
End With
End If
Set objCell = Nothing
Application.ScreenUpdating = True
This can search through tables and delete the rows (when "-" is replaced with the method codes) but I don’t know how to get it to work like that described above. I’m a bit lost…