Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select, Compare, Delete? Need help w/ Macro

Status
Not open for further replies.

jthomas666

Technical User
Sep 5, 2001
74
US
This will be a macro for Word 07. I've got some code that extracts info from Access and puts it into a table in word:


Row-----heading------text
1----------A-----------------yada yada
2----------A-----------------whatever
3----------A-----------------yawn
4----------B------------------hello
5----------C-----------------goodbye
6----------C-----------------get on with it
etc.

What I would like is a macro that will start in row 1, and compare the heading value with the heading value in row B. If they are identical, delete the row 2 value, and proceed to row three. If the values differ, then take the row two value and compare it to row three, etc.

So that the finished result would be

1----------A-----------------yada yada
2-----------------------------whatever
3-----------------------------yawn
4----------B------------------hello
5----------C-----------------goodbye
6-----------------------------get on with it
etc.

I have a bookmark set in the first row, and I already have the number of rows stored as a variable. It'll be an if-then nested in a For loop, but that's about the extent of my VBA skills.


thanks,
 
If you're writing the table line by line then you can do it in your loop. It'll depend on how big your table in Word is going to be, but that would be the best solution.

Do Until rs.EOF
If rs("Heading") <> LastHead Then
wd.writeText rs("Heading")
End If
'Other lines of code...

LastHead = rs("Heading")
rs.MoveNext
Loop
 
The size of the table will vary--anywhere from 20-200 rows. In addition, it will not be the only thing in the document--it's part of a template, and the table will be in between other document elements. That's why I was thinking of the For loop.
 
Here is the basic logic I need--I just don't know VBA:

Goto Bookmark1 (which is in a table cell)
Select cell contents
selection=Group1
for N=1 to x (the x variable is set earlier in the macro)
move down one cell
select cell contents
selection-Group2
IF Group2=Group1, then delete selection
ELSE set Group1=Group2
Next N


The other part of the macro that's giving me trouble is removing duplicate lines (which occur in the source database. If I can get the above code working, a variant of it will let me ID and delete the duplicate lines.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top