How to get values from table?

A

avkokin

Hello. There is one table with 5 columns and many rows. All cells of
the table has the values. And there is the form with ComboBox which
has list of values from first column of this table, and one button.
When user select some element from list of ComboBox and press the
button the macro should find this element into the table, and next get
the values from all other cells this row and save they to variables
(value of every cell into some variable). How to do it?
Thank you.
 
H

Helmut Weber

Hi Anton,

Me.ComboBox1.SelText gets you the selected text from the combobox.
sTmp = Me.ComboBox1.SelText
then you strip the end-of-cell mark from that text.

Then the question is, wether that text can appear in
other columns as the first column. If not so, it is fairly easy.

Search for the text in the range of the table.
if found, the range shrinks to the found spot.
This gives you the row, in which the search item was found.

Option Explicit
Dim oTbl As Table
Dim sTmp As String
Dim rTmp As Range
Dim lCll As Long
Dim lRow As Long
Dim Arr(2 To 5) As String
' ------------------------------------

Private Sub CommandButton1_Click()
Set oTbl = ActiveDocument.Tables(1)
Set rTmp = oTbl.Range
sTmp = Me.ComboBox1.SelText

With rTmp.Find
.Text = sTmp
If .Execute Then
For lCll = 2 To 5
sTmp = rTmp.Rows(1).Cells(lCll).Range.Text
sTmp = Left(sTmp, Len(sTmp) - 2)
Arr(lCll) = sTmp
MsgBox Arr(lCll)
Next
End If
End With

End Sub

More questions?

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
A

avkokin

Hi Helmut. Thank you very much for your help. Yes, it works. And I get
one advise:

vKal = Val(Replace(SourceTable.Rows(cmbFood1.ListIndex + 1).Cells
(2).Range.Text, ",", "."))

Thank you.
 

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