selecting parts of a table

D

De Vo

Hi

I working on my first vba project at the moment please note novice who
is becoming self taught and learning by example.


it basically revolves around a table.

the first row is a headings
the second row and below are dynamic / dynamically added /deleted by
the user.

I need to clear some of the data out of the columns 3,5 and 6 on all
rows.

I need advise on basically how i go around select and clearing the
cells.

How to detect how many rows I have considering my first row of
headings. (ok i understand i will be able to do that with row count -1
or starting my range with that variable with +1)

Please can the word vba guru pass their wisdom

many thanks

devo
 
P

Peter Hewett

Hi Devo

Try this is deletes the contents of the cells in the columns (excluding the
header column) rather than the entire column!

Public Sub DeleteDataInRows()
Dim tblToUpdate As Word.Table
Dim lngFirstUsableRow As Long
Dim ilngRows As Long
Dim ilngCols As Long

' This codes assumes (which is not always safe) the following:
' The table contains no merged columns or rows (it will fail
' if there is). If a header row is present there is only 1 header
' row (there can be more!)

' Select the table you want to use here, this uses #1
Set tblToUpdate = ActiveDocument.Tables(1)

With tblToUpdate

' Does table have a header Row
lngFirstUsableRow = 1
If .Rows.HeadingFormat Then
lngFirstUsableRow = lngFirstUsableRow + 1
End If

' First column = 3, last is 6
For ilngCols = 3 To 6

' Skip column 4
If ilngCols <> 4 Then
' remove the cell contents
For ilngRows = lngFirstUsableRow To .Rows.Count
.Cell(ilngRows, ilngCols).Range.Text = vbNullString
Next
End If
Next
End With
End Sub

Please note the assumptions this code makes, it's documented in the code.

HTH + Cheers - Peter
 
D

De Vo

Hi peter

thanks for the reply..

2 questions if you wouldn't mind

firstly if i wanted to change the tblupdate to refer to a bookmark i
presume i change

Dim tblToUpdate As Word.Table to Dim tblToUpdate As Bookmark
and
Set tblToUpdate = ActiveDocument.Tables(1) to Set tblToUpdate =
ActiveDocument.Bookmark("DataEntryTable")

AS you placed remarks in the code there is some potential fall over
point in the way my document is laid out and the code I need to run to
do what is required, how would other techniques could be used to bullet
proof code can be used or is it even most word advanced tasks / vba
allows a little bit of sloppy coding to cover most of solutions out
there...

I look forward to you wealth of knowledge.

Regards

DeVo
 
P

Peter Hewett

Hi De Vo

I'm not sure what you're actually trying to do! Is the bookmark in a table?
The changes you propose will not work. A bookmark is a bookmark, it can
return a range object. But it does not have to be in or contain either
fully or partially a table. If the bookmark is in or contains a table then
you can use code such as:

Dim tblToUpdate As Word.Table
Set tblToUpdate = ActiveDocument.Bookmarks("DataEntryTable") _
.Range.Tables(1)

The above code always returns the first table in the bookmarked range.

The code I supplied specifically refers to a table, so it makes no sense at
all to try to use it outside a table.

When I write commercial quality code I specifically check for certain
potential errors. I also implement error handlers to handle errors as
necessary and provide sensible feedback.

The code will work well unless you change the table significantly.
Obviously it will fail if the table contains less than 6 rows, or contains
merged columns or rows. You can check for these errors if you want.

If you need any further answers you may need to make a new topic post as
I'm away for a few days.

HTH + Cheers - Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top