Random selection in table

D

Dave Neve

Hi

I've never managed to master VBA so I was wondering if sm could give me a
few lines of code.

I have a table with four columns.

Each cell has a word/phrase in various languages.

I'd like to be able to 'test' myself with a macro that selects a word/phrase
in the first column (English) at random.

Would anyone be kind enough to do this for me?

Thanks in advance

Dave Neve
 
H

Howard Kaikow

You can use the Rnd function to get a random number <= 0 and < 1.
Multiply that result by the number of rows in the column, truncate to an
integer and select the content of that table cell in the column of
interest.
 
D

Dave Neve

Hi Howard

I did say that I don't really know VBA.

I don't want to take advantage of anyone as I do manage to do a lot of
things with my computer by myself but VBA aint one of 'em.(abacus
generation)

Any chance of sm giving me the code which does what Howard says.

Also, in passing, the number of rows changes as words are added every day to
the list.

Thanks

Dave
 
G

Greg Maxey

Dave,

Something like:

Sub RandomPick()
Dim i As Long
Dim j As Long

i = ActiveDocument.Tables(1).Rows.Count
Randomize
j = Int((i * Rnd) + 1)
ActiveDocument.Tables(1).Cell(j, 1).Select
End Sub
 
D

Dave Neve

Thanks. I'll try to run it tomorrow.

Dave Neve
Greg Maxey said:
Dave,

Something like:

Sub RandomPick()
Dim i As Long
Dim j As Long

i = ActiveDocument.Tables(1).Rows.Count
Randomize
j = Int((i * Rnd) + 1)
ActiveDocument.Tables(1).Cell(j, 1).Select
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
 
D

Dave Neve

Hi

I tested this macro and it works in general but not in my special document.

The reason seems to be that I have a table followed by a section jump and
then another table followed by another section jump.

My main table that I want the macro to work in is in section 3 but the macro
takes me back to section one.(first table)

I have honestly tried to solve this myself by adding such things as 'table
(3)' or 'section (3).table (1) but I am useless at VBA.

How do I select the third table (the only table in section 3) for the macro?

Thanks in advance

Dave Neve
 
H

Helmut Weber

Hi Dave,

Sub RandomPick()
Dim i As Long
Dim j As Long
i = ActiveDocument.Tables(3).Rows.Count ' table 3
Randomize
j = Int((i * Rnd) + 1)
ActiveDocument.Tables(3).Cell(j, 1).Select ' table 3
End Sub

If you add more tables, you might have to decide,
which table you really want. The last, the second last?
Can all be done.

Helmut Weber, MVP
 

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