Help with table, macro?

S

Sesquipedalian Sam

I have a large table (up to several thousand rows, 7 columns).

One of the columns (column 4) contains codes for parts of speech from
a corpus tagger (scanner/parser). The tagger has ~150 tags: 31 verb
tags, 22 noun tags, 19 pronoun tags, etc.

The tags for each major part of speech (verb, noun, adjective, etc.)
all start with the same letter:

The verb tags all start with "v" (vv0, vvi, vv0_nn1, ...)
The noun tags all start with "n" (nn0, nn1, nn1_np1, ...)

I would like to convert each of the tags to just the first letter. Any
string in that column that startes with "v" would become just "v".

Can I use Replace to do that somehow?

If not, could someone get me started with the basic of a macro?

Thanks
 
G

Graham Mayor

If I understand you correctly, you want to search each cell in column 4 of
your Word table and if the text it contains begins with one of a series of
letters (n and v identified) then you want that cell just to contain the
first letter? That being the case, the following will do the trick.
The line
vList = Split("n,v", Chr(44))
defines the letters you are searching for separated by commas - here n and
v. You can add to that list eg
vList = Split("a,p,n,v", Chr(44))
With several thousand rows to check it could take a while to run.


Dim oTable As Table
Dim oRow As Row
Dim vList As Variant
Dim i As Long
Set oTable = ActiveDocument.Tables(1)
vList = Split("n,v", Chr(44))
For Each oRow In oTable.Rows
For i = 0 To UBound(vList)
If oRow.Cells(4).Range.Characters(1) _
= vList(i) Then
oRow.Cells(4).Range.Text = vList(i)
End If
Next i
Next oRow

http://www.gmayor.com/installing_macro.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

On further reflection, if you simply want the first character of each cell
in column 4 to be the only letter of the revised content, then the following
would be faster

Dim oTable As Table
Dim oRow As Row
Set oTable = ActiveDocument.Tables(1)
For Each oRow In oTable.Rows
oRow.Cells(4).Range.Text = _
oRow.Cells(4).Range.Characters(1)
Next oRow


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Sesquipedalian Sam

On further reflection, if you simply want the first character of each cell
in column 4 to be the only letter of the revised content, then the following
would be faster

Dim oTable As Table
Dim oRow As Row
Set oTable = ActiveDocument.Tables(1)
For Each oRow In oTable.Rows
oRow.Cells(4).Range.Text = _
oRow.Cells(4).Range.Characters(1)
Next oRow

Thanks, I'll give that a try.

I did a little checking on wildcard characters and found a couple of
articles written by you. But when I tried them, they didn't work.

I copied that one column of data to a temporary document, then tried
using wildcards in Replace to change "v*" to "v". I had Use wildcards
checked and I said it made 108 changes, but nothing changed.

I even did it one change at a time. It stopped at each string with a
"v", but didn't change anything.

I had "v*" in the Find what field and "v" in the Replacve with field
(without the quotes).

Should this work? What did I do wrong?
 
S

Sesquipedalian Sam

On further reflection, if you simply want the first character of each cell
in column 4 to be the only letter of the revised content, then the following
would be faster

Dim oTable As Table
Dim oRow As Row
Set oTable = ActiveDocument.Tables(1)
For Each oRow In oTable.Rows
oRow.Cells(4).Range.Text = _
oRow.Cells(4).Range.Characters(1)
Next oRow

This works great. I made one small change to make the column settable:

Sub POSTagConvert()

Dim oTable As Table
Dim oRow As Row
Const nCol As Integer = 2 'Do the work in column 2

Set oTable = ActiveDocument.Tables(1)
For Each oRow In oTable.Rows
oRow.Cells(nCol).Range.Text = _
oRow.Cells(nCol).Range.Characters(1)
Next oRow

End Sub

Is there a way to make it work in whatever column the cursor is in?

It always works on the first table in the document. Is there a way to
make it work on the table that the cursor is in?

Thanks
 
G

Graham Mayor

Sub POSTagConvert()
Dim oTable As Table
Dim oRow As Row
Dim nCol As Integer
Set oTable = Selection.Range.Tables(1)
nCol = Selection.Information(wdEndOfRangeColumnNumber)
For Each oRow In oTable.Rows
oRow.Cells(nCol).Range.Text = _
oRow.Cells(nCol).Range.Characters(1)
Next oRow
End Sub

will operate on the column of the table the cursor is in.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

There is some odd behaviour in a table when wildcards are used. Use instead
(v)[a-z0-9]{1,}
replace with \1

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Sesquipedalian Sam

Sub POSTagConvert()
Dim oTable As Table
Dim oRow As Row
Dim nCol As Integer
Set oTable = Selection.Range.Tables(1)
nCol = Selection.Information(wdEndOfRangeColumnNumber)
For Each oRow In oTable.Rows
oRow.Cells(nCol).Range.Text = _
oRow.Cells(nCol).Range.Characters(1)
Next oRow
End Sub

will operate on the column of the table the cursor is in.

Thank you very much. It works perfectly. That wold have taken me 2
days to figure out on my own.

Where do I send the pizza and beer? Or wine and cheese? Or champagne
and caviar?
 
S

Suzanne S. Barnhill

Prepare to pay a lot of postage unless you send virtual refreshments: Graham
lives on/in Cyprus.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
http://word.mvps.org
 
S

Sesquipedalian Sam

There are donation links on each page of my web site :)

What? You'd rather have something as plain and unimaginative as cash?

I did notice that you referred to those who take without donating as
leeches. Have you found that that encourages contributions? ;-)
 
G

Graham Mayor

Cash? Absolutely - running a web site isn't cheap, particularly when I
believe we have the highest internet charges in Europe :(

'Leeching' seems a suitably descriptive term for those who take what they
want and widely used on download sites :)

It has not harmed the generosity of those users who gain value from what
they find there. What would you call it? ;)
 
S

Suzanne S. Barnhill

I think it would be preferable either to freely give what you can in a
sincere desire to help others or straightforwardly sell your products and
consulting advice. If you freely give and those who are grateful choose to
make a donation, then that is all to the good, but I do think that
insulting, berating, or shaming those who don't (perhaps can't) is
counterproductive.

--
Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
http://word.mvps.org
 
S

Sesquipedalian Sam

Cash? Absolutely - running a web site isn't cheap,

OK, I'll make a donation. ;-)
particularly when I
believe we have the highest internet charges in Europe :(

Well, that's what you get for living in paradise. ;-)

Move to Greece? I understand they are nearly bankrupt. Maybe there are
some deals to be had. ;-)
'Leeching' seems a suitably descriptive term for those who take what they
want and widely used on download sites :)

Not to me. You offered it for free. Seems a tad harsh to chastise
those who make use of it and are not breaking any rules, even your
own. A leech is someone, like some of my relatives, who latches onto a
hapless host without being invited.
It has not harmed the generosity of those users who gain value from what
they find there.

How do you know? I had a negative reaction when I read it. It seemed
like an insult that I did not deserve. If I had no other contact with
you other than the web site, I would be disinclined to contribute
thinking you were just a cranky old curmudgeon.
What would you call it? ;)

If you mean taking stuff from your website without donating anything,
the harshest term I can come up with that seems to fit is
"opportunist".
 
S

Sesquipedalian Sam

I think it would be preferable either to freely give what you can in a
sincere desire to help others or straightforwardly sell your products and
consulting advice. If you freely give and those who are grateful choose to
make a donation, then that is all to the good, but I do think that
insulting, berating, or shaming those who don't (perhaps can't) is
counterproductive.

Yeah, what she said.
 
D

Doug Robbins - Word MVP

The word "leech" is used in the following context on Graham's website:

Many people access the material from this web site daily. Most just leech
what they want and run. That's OK, provided they are not selling on the
material as their own; however if your productivity gains from the material
you have used, a donation from the money you have saved, however small,
would help to ensure the continued availability of this resource.

I don't really thing that really makes someone, who takes from the site and
runs, a leech. Sure, the words "take" or "grab" could be used in place of
"leech", or as I would spell it when the word is used as a verb - leach,
though I note most dictionaries give leech as an alternative spelling.
 
G

Graham Mayor

Whether or not you choose to donate is entirely a matter for you. You
brought it up. I merely offered the means. :)
I do not answer user questions or provide information on my web site with
the aim of making money from it.
Although I was entirely happy with the use of the word 'leech' in the
context it was employed on my web site, I have changed it to 'take', to
avoid offending the sensibilities of those who take material from my web
site.
 

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